slf4gozap

package module
v0.0.0-...-c74fa79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 3 Imported by: 1

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

Documentation

Index

Constants

View Source
const SkipUntilTrueCaller = 3

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

Variables

This section is empty.

Functions

func Init

func Init(cfg *Config)

Init initializes the driver using the provided config wrapper

Types

type Config

type Config struct {
	ZapConfig  *zap.Config
	ZapOptions []zap.Option
}

Config wraps zap config with some custom options

type ZapDriver

type ZapDriver struct {
	*zap.Logger
	// contains filtered or unexported fields
}

ZapDriver is the wrapper around zap logger and its config

func (*ZapDriver) GetLevel

func (d *ZapDriver) GetLevel(logger string) (sl slog.Level)

GetLevel returns the current level of the logger

func (*ZapDriver) Name

func (d *ZapDriver) Name() string

Name returns the driver's name, which in this case, "slf4go-zap"

func (*ZapDriver) Print

func (d *ZapDriver) Print(l *slog.Log)

Print specifies how the driver will actually printout the log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL