Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
Expand Down
18 changes: 18 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 19 additions & 2 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)})
Expand Down Expand Up @@ -383,6 +393,13 @@ func configFromEnv() Config {
}
}

CallerFormat := os.Getenv(envLoggingCallerFormat)
if CallerFormat == "full" {
cfg.CallerFormat = "full"
} else {
cfg.CallerFormat = "short"
}

return cfg
}

Expand Down