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
4
votes
1 answer

How to configure a custom zap logger that writes to output only at error level?

I’m trying to build a customized logger that keeps log messages below Error level in a buffer and flushes the buffer only after encountering an Error. The problem is that I don’t know how to trigger the flushing of the logs to the output (Sync…
Efim Efim
  • 41
  • 2
  • 6
4
votes
3 answers

How to custom log format when use zap?

Currently, our project's log format is like: www.abcdef.com`3`1s I want to use Go to rewrite project and import zap as log tool. By zap the log's format is like: {"url": "www.abcdef.com", "attempt": 3, "backoff": "1s"} I google its usage, but I…
shinwu
  • 93
  • 1
  • 1
  • 10
3
votes
1 answer

Testing logs printed using other external library in golang

My application uses other external library (which uses zap library) to print audit-logs and now I want to test the printed audit-logs in my golang testing file. Can someone help me with it.
saisree
  • 43
  • 3
3
votes
0 answers

How do you create a basic database logger for uber-go.zap?

I have been searching for examples of how to implement a database logger for uber-go.zip that will insert the message and fields into a database table for querying. (So no encoding) No luck so far finding some examples, so I am starting with the…
Jay
  • 19,649
  • 38
  • 121
  • 184
2
votes
0 answers

How can I write logs to a file mounted by Docker?

I'm trying to get logs into Grafana Loki via Promtail. Both services are running in their own Docker container. Because Promtail collects logs from a filepath, my actual application is writing logs to a file on the host. This file gets mounted into…
ryan s
  • 31
  • 2
2
votes
1 answer

Go Gin: Pass Zap Logger

I'm working in a project using Gin and recently started implementing a logger through the project. I saw a couple libraries but decided to go with uber's Zap. I've got controllers that look like this: package controllers func ListAllArtists(c…
MrCujo
  • 1,218
  • 3
  • 31
  • 56
2
votes
1 answer

How to simply read from stdout with zap.logger (and without creating files)

For test purposes now I have this configuration of zapLogger with writing to "errors.log". In test I read the file, compare some needed texts and delete it when test is finished. l := logger.New(zap.Config{Level: level, Encoding: "json",…
muinh
  • 535
  • 6
  • 14
2
votes
2 answers

What's purpose of initializing a struct as a pointer?

Zap initializes its default option struct as follows in its grpc interceptor: var ( defaultOptions = &options{ levelFunc: DefaultCodeToLevel, shouldLog: grpc_logging.DefaultDeciderMethod, codeFunc: …
user159566
  • 81
  • 4
2
votes
1 answer

How does Go unmarshall a string into a wrapped atomc int?

Uber's logging library Zap has a Config struct, in which the log level is defined as follows: type Config struct { Level AtomicLevel `json:"level" yaml:"level"` } Where the type AtomicLevel is a struct that wraps Go's atomic.Int32: type…
user159566
  • 81
  • 4
2
votes
0 answers

Is there a way to use Zap structured logger in Go and export it to be called in a C script using cgo?

I am trying to use Cgo to export a function that initializes a zap logger. The function is written in Go but I am working towards the goal of being able to call this function in a C script and actually use the function in the C script. For example,…
2
votes
1 answer

uber zap logging to apm server

I'm trying to send logs to the APM server using the uber zap logging library. I've tried their instrumentation module (https://www.elastic.co/guide/en/apm/agent/go/1.x/builtin-modules.html#builtin-modules-apmzap) to do so but it's not working for…
MHY
  • 83
  • 6
2
votes
1 answer

Why custom encoding is lost after calling logger.With in Uber Zap?

(based on this question: Uber Zap Logger: how to prepend every log entry with a string) I replaced the Encoder of my uber-zap logger with a custom one to prepend every log entry with a SystemD-friendly error level (), but now after I use the…
IvanD
  • 2,728
  • 14
  • 26
2
votes
0 answers

Making a logger available for use with a JSON struct

I have a JSON struct that looks something like this in pkg/response/foobar.go type Foo struct { RequestID string `json:"request_id"` Response string `json:"response"` } func (resp *Foo)…
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
2
votes
1 answer

What is the correspondence between go-logr and uber's zap verbosity levels?

I saw that there is log level in Uber Zap implementation: const ( // DebugLevel logs are typically voluminous, and are usually disabled in // production. DebugLevel Level = iota - 1 // InfoLevel is…
user1365697
  • 5,819
  • 15
  • 60
  • 96
2
votes
2 answers

How to use Sentry with go.uber.org/zap/zapcore logger

I am using go.uber.org/zap/zapcore for logging in my Go app. package logger import ( "go.uber.org/zap" "go.uber.org/zap/zapcore" "log" ) var l *zap.Logger func Get() *zap.Logger { return l } func Init() { conf :=…
Viktor
  • 1,532
  • 6
  • 22
  • 61