zerolog

package module
v0.0.0-...-4bd94c0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Kitex zerolog (This is a community driven project)

Introduction

This is a logger library that uses zerolog to implement the Kitex logger interface, work together with kitex obs-opentelemetry

Usage

Download and install it:

go get github.com/kitex-contrib/obs-opentelemetry/logging/zerolog

Import it in your code:

import kitexzerolog github.com/kitex-contrib/obs-opentelemetry/logging/zerolog
Set logger impl
package main

import (
    "github.com/rs/zerolog/log"
    "github.com/cloudwego/kitex/pkg/klog"
    kitexzerolog "github.com/kitex-contrib/obs-opentelemetry/logging/zerolog"
)

func main() {
    logger := kitexzerolog.NewLogger()
    klog.SetLogger(logger)
    klog.SetLevel(klog.LevelDebug)

    // OR / AND using global logger
    log.Logger = *logger.Logger()
}

We provide some methods to help you customize logger

Configuration Description
WithLogger Logger
WithTraceErrorSpanLevel trace error span level option
WithRecordStackTraceInSpan record stack track option
Log with context
// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
    klog.CtxDebugf(ctx, "echo called: %s", req.GetMessage())
    return &api.Response{Message: req.Message}, nil
}
Log with global logger

In case your code base has already used global logger from zerolog

import (
    "github.com/rs/zerolog/log"
)

// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
    log.Debug().Ctx(ctx).Msgf("echo called: %s", req.GetMessage())
    return &api.Response{Message: req.Message}, nil
}
View log
{
  "level": "info",
  "ts": 1667619647.1459548,
  "msg": "hello world",
  "trace_id": "c77e46c0fb590ee80b6d78ed6682768e",
  "span_id": "b42c96c6dd01ceaf",
  "trace_flags": "01"
}

For some reason, zerolog will not log extra context info.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OtelSeverityText

func OtelSeverityText(lv zerolog.Level) string

OtelSeverityText convert zerolog level to otel severityText ref to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#severity-fields

Types

type ExtraKey

type ExtraKey string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(opts ...Option) *Logger

func (*Logger) CtxDebugf

func (l *Logger) CtxDebugf(ctx context.Context, format string, v ...any)

CtxDebugf logs a message at debug level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxErrorf

func (l *Logger) CtxErrorf(ctx context.Context, format string, v ...any)

CtxErrorf logs a message at error level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxFatalf

func (l *Logger) CtxFatalf(ctx context.Context, format string, v ...any)

CtxFatalf logs a message at fatal level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxInfof

func (l *Logger) CtxInfof(ctx context.Context, format string, v ...any)

CtxInfof logs a message at info level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxLogf

func (l *Logger) CtxLogf(level klog.Level, ctx context.Context, format string, kvs ...any)

CtxLogf log with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxNoticef

func (l *Logger) CtxNoticef(ctx context.Context, format string, v ...any)

CtxNoticef logs a message at notice level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxTracef

func (l *Logger) CtxTracef(ctx context.Context, format string, v ...any)

CtxTracef logs a message at trace level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) CtxWarnf

func (l *Logger) CtxWarnf(ctx context.Context, format string, v ...any)

CtxWarnf logs a message at warn level with logger associated with context. If no logger is associated, DefaultContextLogger is used, unless DefaultContextLogger is nil, in which case a disabled logger is used.

func (*Logger) Debug

func (l *Logger) Debug(v ...any)

Debug logs a message at debug level.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...any)

Debugf logs a formatted message at debug level.

func (*Logger) Error

func (l *Logger) Error(v ...any)

Error logs a message at error level.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...any)

Errorf logs a formatted message at error level.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...any)

Fatal logs a message at fatal level.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...any)

Fatalf logs a formatted message at fatal level.

func (*Logger) Info

func (l *Logger) Info(v ...any)

Info logs a message at info level.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...any)

Infof logs a formatted message at info level.

func (*Logger) Log

func (l *Logger) Log(level klog.Level, kvs ...any)

Log log using zerolog logger with specified level

func (*Logger) Logf

func (l *Logger) Logf(level klog.Level, format string, kvs ...any)

Logf log using zerolog logger with specified level and formatting

func (*Logger) Logger

func (l *Logger) Logger() *zerolog.Logger

func (*Logger) Notice

func (l *Logger) Notice(v ...any)

Notice logs a message at notice level.

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...any)

Noticef logs a formatted message at notice level.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level klog.Level)

func (*Logger) SetOutput

func (l *Logger) SetOutput(writer io.Writer)

func (*Logger) Trace

func (l *Logger) Trace(v ...any)

Trace logs a message at trace level.

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...any)

Tracef logs a formatted message at trace level.

func (*Logger) Warn

func (l *Logger) Warn(v ...any)

Warn logs a message at warn level.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...any)

Warnf logs a formatted message at warn level.

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithLogger

func WithLogger(logger *zerolog.Logger) Option

WithLogger configures logger

func WithRecordStackTraceInSpan

func WithRecordStackTraceInSpan(recordStackTraceInSpan bool) Option

WithRecordStackTraceInSpan record stack track option

func WithTraceErrorSpanLevel

func WithTraceErrorSpanLevel(level zerolog.Level) Option

WithTraceErrorSpanLevel trace error span level option

Jump to

Keyboard shortcuts

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