log

package
v0.0.0-...-94c11f6 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 15 Imported by: 0

README

说明

本logger主要是对zap库的封装,便于使用。当然也可以根据接口使用其他日志库,比如 logrus

日志功能

  • 将日志信息记录到日志文件里
  • 日志切割-能够根据日志文件大小或时间间隔进行切割
  • 支持不同的日志级别(eg:info,debug,warn,error,fatal)
  • 支持按日志级别分类输出到不同日志文件
  • 能够打印基本信息,如调用文件/函数名和行号,日志时间,IP等

使用方法

log.Info("user_id is 1")
log.Warn("user is not exist")
log.Error("params error")

log.Warnf("params is empty")
...

原则

日志尽量不要在 model, repository, service中打印输出,最好使用 errors.Wrapf 将错误和消息返回到上层,然后在 handler 层中处理错误, 也就是通过日志打印出来。

这样做的好处是:避免相同日志在多个地方打印,让排查问题更简单。

Reference

Documentation

Overview

Package log span logger for trace reference: https://github.com/jaegertracing/jaeger/tree/master/examples/hotrod/pkg/log

Index

Constants

View Source
const (
	// WriterConsole console输出
	WriterConsole = "console"
	// WriterFile 文件输出
	WriterFile = "file"
)
View Source
const (
	// RotateTimeDaily 按天切割
	RotateTimeDaily = "daily"
	// RotateTimeHourly 按小时切割
	RotateTimeHourly = "hourly"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logger

func Debugf

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

Debugf logger

func Error

func Error(args ...interface{})

Error logger

func Errorf

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

Errorf logger

func GetZapLogger

func GetZapLogger() *zap.Logger

GetZapLogger return raw zap logger

func Info

func Info(args ...interface{})

Info logger

func Infof

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

Infof logger

func Warn

func Warn(args ...interface{})

Warn logger

func Warnf

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

Warnf logger

Types

type Config

type Config struct {
	Development       bool
	DisableCaller     bool
	DisableStacktrace bool
	Encoding          string
	Level             string
	Name              string
	Writers           string
	LoggerFile        string
	LoggerWarnFile    string
	LoggerErrorFile   string
	LogFormatText     bool
	LogRollingPolicy  string
	LogBackupCount    uint
}

Config log config

type Fields

type Fields map[string]interface{}

Fields Type to pass when we want to call WithFields for structured logging

type Logger

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

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

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

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

	WithFields(keyValues Fields) Logger
}

Logger is a contract for the logger

func GetLogger

func GetLogger() Logger

GetLogger return a log

func Init

func Init() Logger

Init init log

func WithContext

func WithContext(ctx context.Context) Logger

WithContext is a logger that can log msg and log span for trace

func WithFields

func WithFields(keyValues Fields) Logger

WithFields logger output more field, eg:

contextLogger := log.WithFields(log.Fields{"key1": "value1"})
contextLogger.Info("print multi field")

or more sample to use:

log.WithFields(log.Fields{"key1": "value1"}).Info("this is a test log")
log.WithFields(log.Fields{"key1": "value1"}).Infof("this is a test log, user_id: %d", userID)

Jump to

Keyboard shortcuts

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