zap

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: 9

README

Kitex zap (This is a community driven project)

Introduction

This is a logger library that uses zap 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/zap

Import it in your code:

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

import (
    "github.com/cloudwego/kitex/pkg/klog"
    kitexzap "github.com/kitex-contrib/obs-opentelemetry/logging/zap"
)

func main() {
    klog.SetLogger(kitexzap.NewLogger())
    klog.SetLevel(klog.LevelDebug)
}

We provide some methods to help you customize logger

Configuration Description
WithCoreEnc zapcore Encoder
WithCoreWs zapcore WriteSyncer
WithCoreLevel zap AtomicLevel
WithZapOptions zap Option
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
}
View log
{
  "level": "info",
  "ts": 1667619647.1459548,
  "msg": "hello world",
  "trace_id": "c77e46c0fb590ee80b6d78ed6682768e",
  "span_id": "b42c96c6dd01ceaf",
  "trace_flags": "01"
}

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OtelSeverityText

func OtelSeverityText(lv zapcore.Level) string

OtelSeverityText convert zapcore 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 {
	*zap.SugaredLogger
	// 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 ...interface{})

func (*Logger) CtxErrorf

func (l *Logger) CtxErrorf(ctx context.Context, format string, v ...interface{})

func (*Logger) CtxFatalf

func (l *Logger) CtxFatalf(ctx context.Context, format string, v ...interface{})

func (*Logger) CtxInfof

func (l *Logger) CtxInfof(ctx context.Context, format string, v ...interface{})

func (*Logger) CtxKVLog

func (l *Logger) CtxKVLog(ctx context.Context, level klog.Level, format string, kvs ...interface{})

func (*Logger) CtxLogf

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

func (*Logger) CtxNoticef

func (l *Logger) CtxNoticef(ctx context.Context, format string, v ...interface{})

func (*Logger) CtxTracef

func (l *Logger) CtxTracef(ctx context.Context, format string, v ...interface{})

func (*Logger) CtxWarnf

func (l *Logger) CtxWarnf(ctx context.Context, format string, v ...interface{})

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

func (*Logger) GetExtraKeys

func (l *Logger) GetExtraKeys() []ExtraKey

GetExtraKeys get extraKeys from logger config

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

func (*Logger) Log

func (l *Logger) Log(level klog.Level, kvs ...interface{})

func (*Logger) Logf

func (l *Logger) Logf(level klog.Level, format string, kvs ...interface{})

func (*Logger) Logger

func (l *Logger) Logger() *zap.Logger

Logger is used to return an instance of *zap.Logger for custom fields, etc.

func (*Logger) Notice

func (l *Logger) Notice(v ...interface{})

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{})

func (*Logger) PutExtraKeys

func (l *Logger) PutExtraKeys(keys ...ExtraKey)

PutExtraKeys add extraKeys after init

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 ...interface{})

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

type Option

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

func WithCoreEnc

func WithCoreEnc(enc zapcore.Encoder) Option

WithCoreEnc zapcore encoder

func WithCoreLevel

func WithCoreLevel(lvl zap.AtomicLevel) Option

WithCoreLevel zapcore log level

func WithCoreWs

func WithCoreWs(ws zapcore.WriteSyncer) Option

WithCoreWs zapcore write syncer

func WithCustomFields

func WithCustomFields(kv ...interface{}) Option

WithCustomFields record log with the key-value pair.

func WithExtraKeyAsStr

func WithExtraKeyAsStr() Option

WithExtraKeyAsStr convert extraKey to a string type when retrieving value from context Not recommended for use, only for compatibility with certain situations

For more information, refer to the documentation at `https://pkg.go.dev/context#WithValue`

func WithExtraKeys

func WithExtraKeys(keys []ExtraKey) Option

WithExtraKeys allow you log extra values from context

func WithRecordStackTraceInSpan

func WithRecordStackTraceInSpan(recordStackTraceInSpan bool) Option

WithRecordStackTraceInSpan record stack track option

func WithTraceErrorSpanLevel

func WithTraceErrorSpanLevel(level zapcore.Level) Option

WithTraceErrorSpanLevel trace error span level option

func WithZapOptions

func WithZapOptions(opts ...zap.Option) Option

WithZapOptions add origin zap option

Jump to

Keyboard shortcuts

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