Skip to content

Commit 1e76540

Browse files
committed
Add os package import and include pid in initial fields
1 parent d01e30b commit 1e76540

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

internal/logger/loggerconfig.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package logger
44
// Ref: https://betterstack.com/community/guides/logging/go/zap/#logging-errors-with-zap
55

66
import (
7+
"os"
8+
79
"go.uber.org/zap"
810
"go.uber.org/zap/zapcore"
911
)
@@ -12,6 +14,7 @@ import (
1214
// It uses JSON formatting for log messages and sets the initial log level to Info. If the logger cannot
1315
// be initialized, the function panics to indicate a critical setup failure.
1416
func BuildLogger(logLevel LogLevel) Logger {
17+
1518
// Set up custom encoder configuration
1619
encoderCfg := zap.NewProductionEncoderConfig()
1720
encoderCfg.TimeKey = "timestamp"
@@ -32,21 +35,22 @@ func BuildLogger(logLevel LogLevel) Logger {
3235
OutputPaths: []string{
3336
"stdout", // Log info and above to standard output
3437
},
35-
ErrorOutputPaths: []string{
38+
ErrorOutputPaths: []string{ // is similar to OutputPaths but is used for Zap's internal errors only, not those generated or logged by your application (such as the error from mismatched loosely-typed key/value pairs).
3639
"stderr", // Log internal Zap errors to standard error
3740
},
38-
InitialFields: map[string]interface{}{
39-
"application": "your-application-name", // Customize this field to suit your needs
41+
InitialFields: map[string]interface{}{ // specifies global contextual fields that should be included in every log entry produced by each logger created from the Config object
42+
"pid": os.Getpid(),
43+
"application": "your-application-name",
4044
},
4145
}
4246

4347
// Build the logger from the configuration
4448
logger := zap.Must(config.Build())
4549

46-
// Wrap the Zap logger in your defaultLogger struct, which implements your Logger interface
50+
// Wrap the Zap logger in your defaultLogger struct, which implements the Logger interface
4751
return &defaultLogger{
4852
logger: logger,
49-
logLevel: LogLevelInfo, // Assuming LogLevelInfo maps to zap.InfoLevel
53+
logLevel: logLevel,
5054
}
5155
}
5256

internal/version/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// version.go
2+
package version
3+
4+
// AppName holds the name of the application
5+
var AppName = "go-api-http-client"
6+
7+
// Version holds the current version of the application
8+
var Version = "1.0.0"
9+
10+
// GetAppName returns the name of the application
11+
func GetAppName() string {
12+
return AppName
13+
}
14+
15+
// GetVersion returns the current version of the application
16+
func GetVersion() string {
17+
return Version
18+
}

0 commit comments

Comments
 (0)