Categorygithub.com/go-eden/slf4go-zap
modulepackage
0.0.0-20230228024123-c74fa79d28f0
Repository: https://github.com/go-eden/slf4go-zap.git
Documentation: pkg.go.dev

# README

slf4go-zap

This is a default Driver implementation for bridging slf4go and zap.

Install

slf4go-zap dependents on slf4go and zap.

go get github.com/go-eden/slf4go-zap

Usage

slf4go-zap focuses on bridging logs, you should configure zap according to your needs.

package main

import (
 slog "github.com/go-eden/slf4go"
 slogzap "github.com/go-eden/slf4go-zap"
 "go.uber.org/zap"
)

func main() {

  zapcfg = zap.Config{
  Level:       zap.NewAtomicLevelAt(zap.DebugLevel),
  Development: false,
  // DisableCaller:     true,
  DisableStacktrace: true,
  Encoding:          "console",
  EncoderConfig:     zap.NewDevelopmentEncoderConfig(),
  OutputPaths:       []string{"stdout"},
  ErrorOutputPaths:  []string{"stdout"},
  InitialFields:     map[string]interface{}{"foo": "bar"},
 }

 cfg = slogzap.Config{
  ZapConfig: &zapcfg,
  ZapOptions: []zap.Option{
   zap.AddCallerSkip(slogzap.SkipUntilTrueCaller), // 3
  },
 }

 slogzap.Init(&cfg)

 // use the global logger
 slog.Debug("zap")

 // or create a new one and use it
 l := slog.GetLogger()
 l.Errorf("default logger name=%s", l.Name())

}

Further examples can be seen in the zap_driver_test.go file.

Using zap.Logger directly

Sometimes it maybe useful to use an instance of the zap.Logger directly. You could use slogzap.Driver struct for that:

package main

import (
 slog "github.com/go-eden/slf4go"
 slogzap "github.com/go-eden/slf4go-zap"
 "go.uber.org/zap/zaptest"
)

func TestLogger(t *testing.T) {
 slog.SetDriver(&slogzap.Driver{
  Logger: zaptest.NewLogger(t)
 })

 // logs will be captured by Go tests
 l := slog.GetLogger()
 l.Error("report error, without failing the test")
}

Notice

Only support zap.SugaredLogger, so this library don't have lots of features currently.

zap.Option is now supported.

Hope you can help me improve this library, any Pull Request will be very welcomed.

Contributor

@phenix3443 @mikeychowy

# Functions

Init initializes the driver using the provided config wrapper.

# Constants

SkipUntilTrueCaller is the skip level which prints out the actual caller instead of slf4go or slf4go-zap wrappers.

# Structs

Config wraps zap config with some custom options.
ZapDriver is the wrapper around zap logger and its config.