nazalog

package
v0.0.0-...-931a278 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 12 Imported by: 34

Documentation

Overview

package nazalog 日志库

Index

Constants

This section is empty.

Variables

View Source
var Clock = mock.NewStdClock()
View Source
var ErrLog = errors.New("naza.log:fxxk")

Functions

func Assert

func Assert(expected interface{}, actual interface{}, extInfo ...string)

func Debug

func Debug(v ...interface{})

func Debugf

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

func Error

func Error(v ...interface{})

func Errorf

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

func Fatal

func Fatal(v ...interface{})

func Fatalf

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

func Fatalln

func Fatalln(v ...interface{})

func Info

func Info(v ...interface{})

func Infof

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

func Init

func Init(modOptions ...ModOption) error

Init 初始化全局Logger

注意,全局Logger在不需要特殊配置时,可以不显示调用 Init 函数 注意,该方法不会修改global指针指向,而是操作global指针指向的对象

func Out

func Out(level Level, calldepth int, s string)

func Output

func Output(calldepth int, s string) error

func Panic

func Panic(v ...interface{})

func Panicf

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

func Panicln

func Panicln(v ...interface{})

func Print

func Print(v ...interface{})

func Printf

func Printf(format string, v ...interface{})

func Println

func Println(v ...interface{})

func SetGlobalLogger

func SetGlobalLogger(l Logger)

SetGlobalLogger 更换全局Logger

注意,更换后,之前调用 GetGlobalLogger 获取的全局Logger和当前的全局Logger将是两个对象

TODO(chef): [refactor] 在已经提供 Init 的前提下,是否应该删除掉该函数

func Sync

func Sync()

func Trace

func Trace(v ...interface{})

func Tracef

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

func Warn

func Warn(v ...interface{})

func Warnf

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

Types

type AssertBehavior

type AssertBehavior uint8
const (
	AssertError AssertBehavior // 1
	AssertFatal
	AssertPanic
)

func (AssertBehavior) ReadableString

func (a AssertBehavior) ReadableString() string

type Level

type Level uint8
const (
	LevelTrace Level = iota // 0
	LevelDebug              // 1
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
	LevelPanic
	LevelLogNothing
)

func (Level) ReadableString

func (l Level) ReadableString() string

type Logger

type Logger interface {
	Tracef(format string, v ...interface{})
	Debugf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Fatalf(format string, v ...interface{}) // 打印日志并退出程序
	Panicf(format string, v ...interface{})

	Trace(v ...interface{})
	Debug(v ...interface{})
	Info(v ...interface{})
	Warn(v ...interface{})
	Error(v ...interface{})
	Fatal(v ...interface{})
	Panic(v ...interface{})

	Out(level Level, calldepth int, s string)

	// Assert 断言失败后的行为由配置项Option.AssertBehavior决定
	// 注意,expected和actual的类型必须相同,比如int(1)和int32(1)是不相等的
	//
	// @param expected 期望值
	// @param actual   实际值
	// @param extInfo  期望值和实际值不相等时打印的补充信息,如果没有,可以不填
	//
	Assert(expected interface{}, actual interface{}, extInfo ...string)

	// Sync flush to disk, typically
	//
	Sync()

	// WithPrefix 添加前缀,新生成一个Logger对象,如果老Logger也有prefix,则老Logger依然打印老prefix,新Logger打印多个prefix
	//
	WithPrefix(s string) Logger

	// Output Print ... 下面这些打印接口是为兼容标准库,让某些已使用标准库日志的代码替换到nazalog方便一些
	//
	Output(calldepth int, s string) error
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	Fatalln(v ...interface{})
	Panicln(v ...interface{})

	// GetOption 获取配置项
	//
	// 注意,作用是只读,非修改配置
	//
	GetOption() Option

	// Init 初始化配置
	//
	// 注意,正常情况下,应在调用 New 函数生成Logger对象时进行配置, Init 方法提供了在已有Logger对象上配置的机会,
	// 但是,出于性能考虑,操作logger对象内部成员时没有加锁,调用方需自行保证该函数不和其他函数并发调用,也即在使用Logger对象前(比如程序启动时)
	//
	Init(modOptions ...ModOption) error
}
var DummyLogger Logger

func GetGlobalLogger

func GetGlobalLogger() Logger

GetGlobalLogger 获取全局Logger

func New

func New(modOptions ...ModOption) (Logger, error)

func WithPrefix

func WithPrefix(s string) Logger

type ModOption

type ModOption func(option *Option)

type Option

type Option struct {
	Level Level `json:"level"` // 日志级别,大于等于该级别的日志才会被输出

	// 文件输出和控制台输出可同时打开
	// 控制台输出主要用做开发时调试,打开后level字段使用彩色输出
	Filename   string `json:"filename"`     // 输出日志文件名,如果为空,则不写日志文件。可包含路径,路径不存在时,将自动创建
	IsToStdout bool   `json:"is_to_stdout"` // 是否以stdout输出到控制台 TODO(chef): 再增加一个stderr的配置

	IsRotateDaily bool `json:"is_rotate_daily"` // 日志按天翻转

	ShortFileFlag       bool `json:"short_file_flag"`        // 是否在每行日志尾部添加源码文件及行号的信息
	TimestampFlag       bool `json:"timestamp_flag"`         // 是否在每行日志首部添加时间戳的信息
	TimestampWithMsFlag bool `json:"timestamp_with_ms_flag"` // 时间戳是否精确到毫秒
	LevelFlag           bool `json:"level_flag"`             // 日志是否包含日志级别字段

	AssertBehavior AssertBehavior `json:"assert_behavior"` // 断言失败时的行为
}

func GetOption

func GetOption() Option

Jump to

Keyboard shortcuts

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