log

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: MIT Imports: 11 Imported by: 0

README

log

中文/Chinese

A simple encapsulation of zap logger is made,it supports getting Logger from context to using in goroutine A golang logger

Description

including Debug Debugf Info Infof Warn Warnf Error Errorf Fatal Fatalf Panic Panicf and With Named Sync StdLogger function.

description
G returns global Logger extends from zap.Logger.
ReplaceG replace the global Logger with the one passed by the parameter.
C is getting Logger from context.Context,if the context does not contain a logger, the G() logger will return.
AssociateC returns a copy of context.Context in which the Logger associated.

Synopsis

package main

import (
	"context"
	"github.com/baishan-development-guizhou/golang-library/log"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(1)
	logger := log.Configure().WithOutputEncoder(log.ConsoleOutputEncoder).WithCallerEncoder(log.ShortRoutineCallerEncoder).
		WithStacktrace(false).WithLevel(log.DebugLevel).
		Init()
	logger = logger.With("dsd", "sdd")
	ctx, _ := log.AssociateC(context.Background(), logger)
	go func() {
		iLogger := log.C(ctx)
		iLogger.Info("second")
		wg.Done()
	}()
	wg.Wait()
}

Documentation

Index

Constants

View Source
const (
	DebugLevel = Level(zapcore.DebugLevel)
	InfoLevel  = Level(zapcore.InfoLevel)
	WarnLevel  = Level(zapcore.WarnLevel)
	ErrorLevel = Level(zapcore.ErrorLevel)
	FatalLevel = Level(zapcore.FatalLevel)
	PanicLevel = Level(zapcore.PanicLevel)
)

Variables

This section is empty.

Functions

func Configure

func Configure() *options

func Debug

func Debug(args ...interface{})

Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Debugf

func Debugf(format string, args ...interface{})

Debugf uses fmt.Sprintf to log a templated message.

func Error

func Error(args ...interface{})

Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Errorf

func Errorf(format string, args ...interface{})

Errorf uses fmt.Sprintf to log a templated message.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message.

func Info

func Info(args ...interface{})

Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Infof

func Infof(format string, args ...interface{})

Infof uses fmt.Sprintf to log a templated message.

func Panic

func Panic(args ...interface{})

Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Panicf

func Panicf(format string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message.

func ReplaceG

func ReplaceG(logger Logger)

ReplaceG replace the _global logger with the one passed by the parameter

func Sync

func Sync() error

Sync flushing any buffered log entries. Applications should take care to call Sync before exiting.

func Warn

func Warn(args ...interface{})

Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the _global logger.

func Warnf

func Warnf(format string, args ...interface{})

Warnf uses fmt.Sprintf to log a templated message.

Types

type CallerEncoder

type CallerEncoder zapcore.CallerEncoder
var (
	FullCallerEncoder        CallerEncoder = zapcore.FullCallerEncoder
	FullRoutineCallerEncoder CallerEncoder = func(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
		enc.AppendString(fmt.Sprintf("%d#%d %s", os.Getegid(), oid.ID(), caller.String()))
	}
	ShortCallerEncoder        CallerEncoder = zapcore.ShortCallerEncoder
	ShortRoutineCallerEncoder CallerEncoder = func(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
		enc.AppendString(fmt.Sprintf("%d#%d %s", os.Getegid(), oid.ID(), caller.TrimmedPath()))
	}
)

type Level

type Level zapcore.Level

type LevelEncoder

type LevelEncoder zapcore.LevelEncoder
var (
	LowercaseLevelEncoder      LevelEncoder = zapcore.LowercaseLevelEncoder
	LowercaseColorLevelEncoder LevelEncoder = zapcore.LowercaseColorLevelEncoder
	CapitalLevelEncoder        LevelEncoder = zapcore.CapitalLevelEncoder
	CapitalColorLevelEncoder   LevelEncoder = zapcore.CapitalColorLevelEncoder
	BracketLevelEncoder        LevelEncoder = func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
		encoder.AppendString("[" + level.String() + "]")
	}
)

type Logger

type Logger interface {
	Debug(i ...interface{})
	Debugf(format string, args ...interface{})

	Info(i ...interface{})
	Infof(format string, args ...interface{})

	Warn(i ...interface{})
	Warnf(format string, args ...interface{})

	Error(i ...interface{})
	Errorf(format string, args ...interface{})

	Fatal(i ...interface{})
	Fatalf(format string, args ...interface{})

	Panic(i ...interface{})
	Panicf(format string, args ...interface{})

	With(fields ...interface{}) Logger

	Named(named string) Logger

	Sync() error

	StdLogger() *log.Logger
}

func AssociateC

func AssociateC(ctx context.Context, logger Logger) (context.Context, Logger)

AssociateC returns a copy of context.Context in which the Logger associated

func C

func C(ctx context.Context) Logger

C returns Logger from context.Context func. You should pass the Context,So that we can get the Logger from the context

func G

func G() Logger

G returns _global Logger extends from zap.Logger

func Named

func Named(named string) Logger

Named adds a sub-scope to the logger's name.

func With

func With(fields ...interface{}) Logger

With adds a variadic number of fields to the logging context.

type OutputEncoder

type OutputEncoder func(cfg zapcore.EncoderConfig) zapcore.Encoder
var (
	JsonOutputEncoder    OutputEncoder = zapcore.NewJSONEncoder
	ConsoleOutputEncoder OutputEncoder = zapcore.NewConsoleEncoder
)

type ZapLoggerAdapter

type ZapLoggerAdapter struct {
	*zap.SugaredLogger
}

ZapLoggerAdapter adapter for Logger

func (*ZapLoggerAdapter) Named

func (z *ZapLoggerAdapter) Named(named string) Logger

func (*ZapLoggerAdapter) StdLogger

func (z *ZapLoggerAdapter) StdLogger() *log.Logger

func (*ZapLoggerAdapter) With

func (z *ZapLoggerAdapter) With(fields ...interface{}) Logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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