Questions tagged [go-zap]

Zap is the logging framework for Go developed by Uber. It offers a powerful API for building structured loggers. Use this tag for questions related to the usage of the zap library in Go.

The package uber-go/zap is a logging framework used to add structured, leveled logging to a Go application.

The most frequently used packages are:

  • zap that contains the main abstractions
  • zapcore that contains the logic and types for constructing bespoke loggers

A simple example of how to use Zap:

logger, _ := zap.NewDevelopment()
logger.Info("hello world", zap.String("foo", "bar"))

Useful packages that extend Zap functionality:

58 questions
25
votes
4 answers

How to initialize a zap logger once and reuse it in other Go files?

I'm trying to migrate my application from the beautiful Logrus (very helpful for debug) and introducing the Uber log framework Zap. With Logrus, I can initialize the logger only once and reuse it from other Go file, an example: package main import( …
alessiosavi
  • 2,753
  • 2
  • 19
  • 38
8
votes
2 answers

How to test logging of a zap Logger built from custom Config?

I have a Zap logger that is generated from a custom Config (i.e. config.Build()). I would like to test the logger by calling, for example, logger.Info() in the test method and assert the result to see if it is according to the config set. How can I…
Freddie Sunarso
  • 113
  • 1
  • 6
8
votes
2 answers

How to add new columns/fields to a ZAP log entry?

I have the following logging structure: [STDERR] 2018-07-09 11:06:16.003 INFO some_pkg/main.go:232 Logging message 1 {"pid": 8842, "process": "some_process"} [STDERR] 2018-07-09 11:06:16.006 DEBUG some_pkg/main.go:291 Logging…
kbenda
  • 430
  • 5
  • 15
7
votes
1 answer

How to put the "real" stack trace under the "stacktrace" key, instead of the "msg" key?

Given the following code (copied from here): 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/pkg/errors" 7 8 "go.uber.org/zap" 9 ) 10 11 func main() { 12 …
quanta
  • 3,960
  • 4
  • 40
  • 75
7
votes
1 answer

How can I use go.uber.org/zap lib to print different color with different log level and append log to different file depend on the log level?

I started using the zap log library for my Go project. I want to print different colors to tty console based on the log level. I find the zap/internal/color package can display different colors for strings, but I want to change the log level with…
savior
  • 802
  • 6
  • 14
6
votes
2 answers

Is it possible to update the log level of a zap logger at runtime?

I created a logger with kubebuilder, it is based on zap logger: import ( "flag" "github.com/gin-gonic/gin" "net/http" "os" "go.uber.org/zap/zapcore" uzap "go.uber.org/zap" // Import all Kubernetes client auth plugins…
user1365697
  • 5,819
  • 15
  • 60
  • 96
6
votes
1 answer

Uber-go/zap and kafka-go race condition

I'm creating a custom logger where we can log to std out and std err, but also adding the possibility to log to kafka (the code example is here: https://github.com/roppa/kafka-go). We have multiple topics, so we need multiple loggers, but when we…
Mark Robson
  • 1,298
  • 3
  • 15
  • 40
6
votes
4 answers

Zap logger print both to console and to log file

I have integrated Zap with my go application, we have logs getting printed in two log files and i am also using Lumberjack for log rotation. But i am trying to display the logs in console as well, but no luck for this case. Following is my code in…
Nidhi Sharma
  • 431
  • 1
  • 5
  • 17
6
votes
3 answers

How to configure uber-go/zap logger for rolling filesystem log?

How to configure uber-go/zap logger api to append logs to a specified file-path. Can it be made to work like rolling file-appender (based on file-size or date) without affecting performance?
johndoe
  • 131
  • 1
  • 2
  • 6
5
votes
1 answer

Uber Zap Logger: how to prepend every log entry with a string

I am using my app as a SystemD service and need to prepend every message with an entry level for JournalD like: <6> this is info <7> this is debug <4> this is warning Otherwise, JournalD treats all the entries the same level and I want to…
IvanD
  • 2,728
  • 14
  • 26
5
votes
0 answers

Sending designated logs to the Kafka sink using Uber-Zap logger in Go

I am trying to use zap logger package to create a core with file, console and Kafka sinks. I have some really specific INFO level logs that I want to send to a Kafka topic for processing by a downstream consumer. However, with the current…
aru_sha4
  • 368
  • 2
  • 11
5
votes
1 answer

Uber Zap logger function name in logs

How to get function name printed in logs from Uber Zap logging ? This is the PR request with which they seemed to have added the functionality to output function names in log. I am using golang version 1.15 and go.uber.org/zap v1.16.0 This is my…
Ganesh kudva
  • 990
  • 3
  • 13
  • 34
4
votes
1 answer

How to access Fields in zap Hooks?

How can I access the full information about the logging event in uber-zap's hooks? For example, I am trying to add a zapcore.Field to the logging event, but it does not show up in the zapcore.Entry. If it is not possible, can I at least have the…
IvanD
  • 2,728
  • 14
  • 26
4
votes
1 answer

How to log to stdout or stderr based on log level using uber-go/zap?

I am trying to set up logging using this package github.com/uber-go/zap. I need to write: Info logs to stdout Error and Fatal logs to stderr I tried to do this by setting up and building zap.Config like this: cfg = &zap.Config{ …
Yury Zakharov
  • 73
  • 1
  • 4
4
votes
2 answers

Correctly log protobuf messages as unescaped JSON with zap logger

I have a Go project where I'm using Zap structured logging to log the contents of structs. That's how I initialise the logger: zapLog, err := zap.NewProductionConfig().Build() if err != nil { panic(err) } Initially I started with my own structs…
VHristov
  • 1,059
  • 2
  • 13
  • 25
1
2 3 4