Zap Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
m (Ovidiu moved page Zap Programming Model and Operations to Zap Operations without leaving a redirect)
No edit summary
Tag: Manual revert
 
Line 5: Line 5:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
go get -u go.uber.org/zap
go get -u go.uber.org/zap
</syntaxhighlight>
=Typical Usage=
<syntaxhighlight lang='go'>
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()
</syntaxhighlight>
=Disable Stack Trace Rendering=
Stack trace on Warning is probably not necessary. However, this disable stack trace rendering altogether:
<syntaxhighlight lang='go'>
var config zap.Config
config = zap.NewDevelopmentConfig()
config.DisableStacktrace = true
</syntaxhighlight>
=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 // use the instance, updating the level on that instance will dynamically update the logging level
logger, err := config.Build()
...
// this changes the logging level dynamically
atomicLevel.SetLevel(zap.InfoLevel)
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 20:37, 14 March 2024

Internal

Installation

In the directory that contains go.mod:

go get -u go.uber.org/zap