Zap Operations: Difference between revisions
Jump to navigation
Jump to search
Tag: Reverted |
Tag: Reverted |
||
Line 39: | Line 39: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Changing Log Level Dynamically= | =Changing Log Level Dynamically= | ||
Create the logger instance with an external <code>zap.AtomicLevel</code> instance. Updating the level on that instance updates it dynamically for the logger: | |||
<syntaxhighlight lang='go'> | |||
atomicLevel := zap.NewAtomicLevelAt(zap.DebugLevel) | |||
var config zap.Config | |||
config = zap.NewDevelopmentConfig() | |||
config.Level = atomicLevel | |||
logger, err := config.Build() | |||
... | |||
// this changes the logging level dynamically | |||
atomicLevel.SetLevel(zap.InfoLevel) | |||
</syntaxhighlight> | |||
=Workbench= | =Workbench= | ||
{{Internal|Zap Workbench|Zap Workbench}} | {{Internal|Zap Workbench|Zap Workbench}} |
Revision as of 20:14, 22 January 2024
Internal
Installation
In the directory that contains go.mod
:
go get -u go.uber.org/zap
Typical Usage
import "go.uber.org/zap"
...
var config zap.Config
var logger *zap.Logger
var sugaredLogger *zap.SugaredLogger
outputPath := "stderr" // ... or a local path
config = zap.NewDevelopmentConfig()
config.OutputPaths = []string{outputPath}
config.ErrorOutputPaths = []string{outputPath}
logger, err := config.Build()
if err != nil {
panic(err)
}
sugaredLogger = logger.Sugar()
sugaredLogger.Infof("somehting")
...
logger = sugaredLogger.Desugar()
Disable Stack Trace Rendering
Stack trace on Warning is probably not necessary. However, this disable stack trace rendering altogether:
var config zap.Config
config = zap.NewDevelopmentConfig()
config.DisableStacktrace = true
Changing Log Level Dynamically
Create the logger instance with an external zap.AtomicLevel
instance. Updating the level on that instance updates it dynamically for the logger:
atomicLevel := zap.NewAtomicLevelAt(zap.DebugLevel)
var config zap.Config
config = zap.NewDevelopmentConfig()
config.Level = atomicLevel
logger, err := config.Build()
...
// this changes the logging level dynamically
atomicLevel.SetLevel(zap.InfoLevel)