zap

package module
v0.0.0-...-1db7dc6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

Go2sky with zap (v1.16.0)

Installation

go get -u github.com/SkyAPM/go2sky-plugins/zap

Usage

package main

import (
	"context"
	
	zapplugin "github.com/SkyAPM/go2sky-plugins/zap"
	"go.uber.org/zap"
)

func main() {
	ctx := context.Background()
	logger := zap.NewExample()
	
	// You have two way to adopt
	// 1. Addition fields before logging
	logger.With(zapplugin.TraceContext(ctx)...).Info("test")
	
	// 2. Wrap logger and correlate context at logging
	logger = zapplugin.WrapWithContext(logger)
	logger.Info(ctx, "test")
}

See more.

Documentation

Overview

Package zap is a plugin that can be transmit trace context to the log framework.

Index

Examples

Constants

View Source
const HeaderKeyCtx = "SW_CTX"

HeaderKeyCtx export zap field key name

Variables

This section is empty.

Functions

func TraceContext

func TraceContext(ctx context.Context) []zap.Field

TraceContext build all SkyWalking Trace Context to zap fields

Example
package main

import (
	"context"

	zapplugin "github.com/SkyAPM/go2sky-plugins/zap"
	"go.uber.org/zap"
)

func main() {
	ctx := context.Background()

	logger := zap.NewExample()
	logger.With(zapplugin.TraceContext(ctx)...).Info("test")
}
Output:

{"level":"info","msg":"test","SW_CTX":"[,,N/A,N/A,-1]"}

Types

type WrapLogger

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

WrapLogger is wrap the zap logger, also contains all method at zap logger, correlate the context before logging

func WrapWithContext

func WrapWithContext(logger *zap.Logger) *WrapLogger

WrapWithContext original zap logger(not sugar)

Example
package main

import (
	"context"

	zapplugin "github.com/SkyAPM/go2sky-plugins/zap"
	"go.uber.org/zap"
)

func main() {
	ctx := context.Background()

	logger := zapplugin.WrapWithContext(zap.NewExample())
	logger.Info(ctx, "test")
}
Output:

{"level":"info","msg":"test","SW_CTX":"[,,N/A,N/A,-1]"}

func (*WrapLogger) Check

func (log *WrapLogger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry

Check wrap the zap logging function

func (*WrapLogger) Core

func (log *WrapLogger) Core() zapcore.Core

Core wrap the zap logging function

func (*WrapLogger) DPanic

func (log *WrapLogger) DPanic(ctx context.Context, msg string, fields ...zap.Field)

DPanic wrap the zap logging function and relate the context

func (*WrapLogger) Debug

func (log *WrapLogger) Debug(ctx context.Context, msg string, fields ...zap.Field)

Debug wrap the zap logging function and relate the context

func (*WrapLogger) Error

func (log *WrapLogger) Error(ctx context.Context, msg string, fields ...zap.Field)

Error wrap the zap logging function and relate the context

func (*WrapLogger) Fatal

func (log *WrapLogger) Fatal(ctx context.Context, msg string, fields ...zap.Field)

Fatal wrap the zap logging function and relate the context

func (*WrapLogger) Info

func (log *WrapLogger) Info(ctx context.Context, msg string, fields ...zap.Field)

Info wrap the zap logging function and relate the context

func (*WrapLogger) Named

func (log *WrapLogger) Named(s string) *WrapLogger

Named wrap the zap logging function

func (*WrapLogger) Panic

func (log *WrapLogger) Panic(ctx context.Context, msg string, fields ...zap.Field)

Panic wrap the zap logging function and relate the context

func (*WrapLogger) Sync

func (log *WrapLogger) Sync() error

Sync wrap the zap logging function

func (*WrapLogger) Warn

func (log *WrapLogger) Warn(ctx context.Context, msg string, fields ...zap.Field)

Warn wrap the zap logging function and relate the context

func (*WrapLogger) With

func (log *WrapLogger) With(fields ...zap.Field) *WrapLogger

With wrap the zap logging function

func (*WrapLogger) WithOptions

func (log *WrapLogger) WithOptions(opts ...zap.Option) *WrapLogger

WithOptions wrap the zap logging function

Jump to

Keyboard shortcuts

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