-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#14 Inited Glide logging based on Zap
- Loading branch information
1 parent
86c624c
commit 1c16e4b
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package telemetry | ||
|
||
import "go.uber.org/zap" | ||
|
||
type TelemetryConfig struct { | ||
LogConfig *zap.Config `json:"logs" yaml:"logs"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package telemetry | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type LogConfig struct { | ||
// Level is the minimum enabled logging level. | ||
Level zap.AtomicLevel `yaml:"level"` | ||
|
||
// Encoding sets the logger's encoding. Valid values are "json", "console" | ||
Encoding string `yaml:"encoding"` | ||
|
||
// DisableCaller stops annotating logs with the calling function's file name and line number. | ||
// By default, all logs are annotated. | ||
DisableCaller bool `yaml:"disable_caller"` | ||
|
||
// DisableStacktrace completely disables automatic stacktrace capturing. By | ||
// default, stacktraces are captured for WarnLevel and above logs in | ||
// development and ErrorLevel and above in production. | ||
DisableStacktrace bool `yaml:"disable_stacktrace"` | ||
|
||
// OutputPaths is a list of URLs or file paths to write logging output to. | ||
OutputPaths []string `yaml:"output_paths"` | ||
|
||
// InitialFields is a collection of fields to add to the root logger. | ||
InitialFields map[string]interface{} `yaml:"initial_fields"` | ||
} | ||
|
||
func NewLogConfig() *LogConfig { | ||
return &LogConfig{ | ||
Level: zap.NewAtomicLevelAt(zap.InfoLevel), | ||
Encoding: "json", | ||
OutputPaths: []string{"stdout"}, | ||
} | ||
} | ||
|
||
func (c *LogConfig) ToZapConfig() *zap.Config { | ||
zapConfig := zap.NewProductionConfig() | ||
|
||
if c.Encoding == "console" { | ||
zapConfig = zap.NewDevelopmentConfig() | ||
|
||
// Human-readable timestamps for console format of logs. | ||
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
// Colorized plain console logs | ||
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
} | ||
|
||
zapConfig.Level = c.Level | ||
zapConfig.DisableCaller = c.DisableCaller | ||
zapConfig.DisableStacktrace = c.DisableStacktrace | ||
zapConfig.OutputPaths = c.OutputPaths | ||
zapConfig.InitialFields = c.InitialFields | ||
|
||
return &zapConfig | ||
} | ||
|
||
func NewLogger(cfg *LogConfig) (*zap.Logger, error) { | ||
zapConfig := cfg.ToZapConfig() | ||
|
||
return zapConfig.Build() | ||
} |