diff --git a/README.md b/README.md index c996715..5025b4b 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,16 @@ pairs. For example, the following add `{"app": "example_app", "dc": "sjc-1"}` to export GOLOG_LOG_LABELS="app=example_app,dc=sjc-1" ``` +#### `GOLOG_LOG_CALLER_FORMAT` + +Specifies the file line format. + +- `full` Serializes a caller in /full/path/to/package/file:line. +- `short` serializes a caller in package/file:line format, trimming all but the final directory from the full path. +```bash +export GOLOG_LOG_CALLER_FORMAT="full" +``` + ## Contribute Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/go-log/issues)! diff --git a/core.go b/core.go index 56672d3..e0f9a44 100644 --- a/core.go +++ b/core.go @@ -104,7 +104,25 @@ func (l *lockedMultiCore) ReplaceCore(original, replacement zapcore.Core) { func newCore(format LogFormat, ws zapcore.WriteSyncer, level LogLevel) zapcore.Core { encCfg := zap.NewProductionEncoderConfig() encCfg.EncodeTime = zapcore.ISO8601TimeEncoder + var encoder zapcore.Encoder + switch format { + case PlaintextOutput: + encCfg.EncodeLevel = zapcore.CapitalLevelEncoder + encoder = zapcore.NewConsoleEncoder(encCfg) + case JSONOutput: + encoder = zapcore.NewJSONEncoder(encCfg) + default: + encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder + encoder = zapcore.NewConsoleEncoder(encCfg) + } + + return zapcore.NewCore(encoder, ws, zap.NewAtomicLevelAt(zapcore.Level(level))) +} +func newCoreWithFullCallerEncCfg(format LogFormat, ws zapcore.WriteSyncer, level LogLevel) zapcore.Core { + encCfg := zap.NewProductionEncoderConfig() + encCfg.EncodeTime = zapcore.ISO8601TimeEncoder + encCfg.EncodeCaller = zapcore.FullCallerEncoder var encoder zapcore.Encoder switch format { case PlaintextOutput: diff --git a/setup.go b/setup.go index e0d321e..1747cbc 100644 --- a/setup.go +++ b/setup.go @@ -37,6 +37,9 @@ const ( envLoggingOutput = "GOLOG_OUTPUT" // possible values: stdout|stderr|file combine multiple values with '+' envLoggingLabels = "GOLOG_LOG_LABELS" // comma-separated key-value pairs, i.e. "app=example_app,dc=sjc-1" + // full: /full/path/to/package/file:line format. + // short: package/file:line format, trimming all but the final directory from the full path. + envLoggingCallerFormat = "GOLOG_LOG_CALLER_FORMAT" // possible values: full|short ) type LogFormat int @@ -71,6 +74,9 @@ type Config struct { // Labels is a set of key-values to apply to all loggers Labels map[string]string + + // Format for Caller line. possible values: full|short. default short. + CallerFormat string } // ErrNoSuchLogger is returned when the util pkg is asked for a non existant logger @@ -138,8 +144,12 @@ func SetupLogging(cfg Config) { if err != nil { panic(fmt.Sprintf("unable to open logging output: %v", err)) } - - newPrimaryCore := newCore(primaryFormat, ws, LevelDebug) // the main core needs to log everything. + var newPrimaryCore zapcore.Core + if cfg.CallerFormat == "short" { + newPrimaryCore = newCore(primaryFormat, ws, LevelDebug) // the main core needs to log everything. + } else { + newPrimaryCore = newCoreWithFullCallerEncCfg(primaryFormat, ws, LevelDebug) // the main core needs to log everything. + } for k, v := range cfg.Labels { newPrimaryCore = newPrimaryCore.With([]zap.Field{zap.String(k, v)}) @@ -383,6 +393,13 @@ func configFromEnv() Config { } } + CallerFormat := os.Getenv(envLoggingCallerFormat) + if CallerFormat == "full" { + cfg.CallerFormat = "full" + } else { + cfg.CallerFormat = "short" + } + return cfg }