hlog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: MIT Imports: 9 Imported by: 6

README

hlog - High-performance logger for Go

Logger 的需求

  • 统一要求
    1. 基于 go.uber.org/zap 框架
    2. 文件记录基于 gopkg.in/natefinch/lumberjack.v2 包
    3. 支持 debug 开关,即对于 DPanic 有效
    4. 支持 console 和文件作为输出目标
    5. 可甜可咸
  • 关于 level
    1. 支持 level 的统一控制,即低于 level 均不输出到任何目标
    2. logger 的 level 在初始化后,可以动态调整
    3. logger 可以派生,派生的 logger 和 root 对输出目标并发写无冲突,level 可以自由独立定义
  • Console 日志
    1. 日志一定会输出到 console
    2. 三种格式可选:彩色 tab 分隔、单色 tab 分隔、JSON
    3. 满足 level 的前提下,Error 或以上级别输出到 stderr,以下输出到 stdout
  • 文件日志
    1. 日志可选配输出到指定路径
    2. 两种格式可选:单色 tab 分隔、JSON
    3. 文件 rotation 策略可指定:超过多少切割,是否压缩,是否删除最旧的

e.g.

console 格式

2022-04-14T23:03:25.858+0800	WARN	onelog/main.go:53	Hello Go W World from 2 ~~~~~	{"num": 2}
2022-04-14T23:03:25.961+0800	DEBUG	onelog/main.go:58	Hello Go D World from 2 Now1	{"num": 2}

json 格式

{"level":"warn","time":"2022-04-14T23:04:18.485+0800","caller":"onelog/main.go:53","msg":"Hello Go W World from 2 ~~~~~","num":2}
{"level":"debug","time":"2022-04-14T23:04:18.585+0800","caller":"onelog/main.go:58","msg":"Hello Go D World from 2 Now1","num":2}

Documentation

Index

Constants

View Source
const (
	// LogFormatConsole indicates the console format, i.e. [time] [level] [caller] [message] [fields*]
	LogFormatConsole = "console"
	// LogFormatJSON indicates the JSON format.
	LogFormatJSON = "json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LogFormat

type LogFormat string

LogFormat is the format of the log output.

type Logger

type Logger struct {
	*zap.SugaredLogger
	// contains filtered or unexported fields
}

Logger is a wrapper of zap.SugaredLogger with some extra features.

func NewJSONLogger

func NewJSONLogger(fileName string, debug bool) *Logger

NewJSONLogger returns a logger which outputs in JSON format only and keeps 10 recent log archives.

func NewLogger

func NewLogger(options ...Option) *Logger

NewLogger creates a new Logger with the given options.

func NewNoopLogger

func NewNoopLogger() *Logger

NewNoopLogger returns a no-op logger which does nothing except panicking when calling Fatal or Panic.

func NewPersistentLogger

func NewPersistentLogger(fileName string, debug bool) *Logger

NewPersistentLogger returns a logger which keeps all the log file in JSON format and never cleans up.

func NewSimpleLogger

func NewSimpleLogger() *Logger

NewSimpleLogger returns a simple logger which writes to stdout/stderr only with process id in development mode.

func (*Logger) Fork

func (l *Logger) Fork() *Logger

Fork creates a new Logger with the same options and fields.

func (*Logger) GetLevel

func (l *Logger) GetLevel() zapcore.Level

GetLevel returns the current log level.

func (*Logger) Named

func (l *Logger) Named(name string) *Logger

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

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl zapcore.Level)

SetLevel sets the log level.

func (*Logger) SetLevelString

func (l *Logger) SetLevelString(text string) error

SetLevelString sets the log level by string.

func (*Logger) With

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

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

func (*Logger) WithOptions added in v0.1.2

func (l *Logger) WithOptions(_ ...Option) *Logger

type Option

type Option func(*config)

Option is a function that configures the logger.

func WithConsoleLogFormat

func WithConsoleLogFormat(format LogFormat) Option

WithConsoleLogFormat configures the format of the console log output.

func WithDevelopmentMode

func WithDevelopmentMode() Option

WithDevelopmentMode configures the development mode.

func WithErrorLogOutput

func WithErrorLogOutput(w io.Writer) Option

WithErrorLogOutput configures the io.Writer for logs at or above error level.

func WithLevel added in v0.1.2

func WithLevel(level zapcore.Level) Option

func WithMonochrome

func WithMonochrome(b bool) Option

WithMonochrome configures the monochrome mode of console output.

func WithNormalLogOutput

func WithNormalLogOutput(w io.Writer) Option

WithNormalLogOutput configures the io.Writer for logs under error level.

func WithOutputFile

func WithOutputFile(p string) Option

WithOutputFile configures the output file path.

func WithOutputFileCompress

func WithOutputFileCompress(b bool) Option

WithOutputFileCompress configures whether to compress the output file.

func WithOutputFileLogFormat

func WithOutputFileLogFormat(format LogFormat) Option

WithOutputFileLogFormat configures the format of the output file log output.

func WithOutputFileMaxSizeInMB

func WithOutputFileMaxSizeInMB(s uint) Option

WithOutputFileMaxSizeInMB configures the max size of the output file in MB.

func WithOutputFileRetainNum

func WithOutputFileRetainNum(n uint) Option

WithOutputFileRetainNum configures the number of output files to retain, 0 means unlimited.

func WithStacktraceLevel

func WithStacktraceLevel(lvl zapcore.Level) Option

WithStacktraceLevel configures a logger to record a stack trace for all messages at or above a given level.

func WithStacktraceLevelString

func WithStacktraceLevelString(text string) Option

WithStacktraceLevelString configures a logger to record a stack trace for all messages at or above a given level, if the level string is invalid, it will be ignored.

func WithZapOptions

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

WithZapOptions wraps the zap.Options to configure the logger.

Jump to

Keyboard shortcuts

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