logfactory

package
v0.0.0-...-0a45852 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CallerNone none
	CallerNone = 0
	// CallerShortFile short file name
	CallerShortFile = 1
	// CallerLongFile long file name
	CallerLongFile = 1 << 1
	// CallerFileMask long file name
	CallerFileMask = 3
	// CallerShortFunc caller short func
	CallerShortFunc = 1 << 2
	// CallerLongFunc caller long func
	CallerLongFunc = 1 << 3
	// CallerSimpleFunc caller simple func
	CallerSimpleFunc = 1 << 4
	// CallerFuncMask func mask
	CallerFuncMask = 7 << 2
)
View Source
const (
	AutoColor = iota

	DisableColor
)
View Source
const (
	// TimestampKey LogTime
	TimestampKey = "LogTime"
	// LevelKey LogLevel
	LevelKey = "LogLevel"
	// CallerKey LogCaller
	CallerKey = "LogCaller"
	// ContentKey LogContent
	ContentKey = "LogContent"
	// NameKey LogName
	NameKey = "LogName"
)

Variables

View Source
var (
	//前景色
	ColorGreen   = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
	ColorWhite   = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
	ColorYellow  = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
	ColorRed     = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
	ColorBlue    = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
	ColorMagenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
	ColorCyan    = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
	ColorReset   = string([]byte{27, 91, 48, 109})

	ForeGreen   = "\033[97;32m"
	ForeWhite   = "\033[90;37m"
	ForeYellow  = "\033[90;33m"
	ForeRed     = "\033[97;31m"
	ForeBlue    = "\033[97;34m"
	ForeMagenta = "\033[97;35m"
	ForeCyan    = "\033[97;36m"

	//背景色
	BackGreen   = "\033[97;42m"
	BackWhite   = "\033[90;47m"
	BackYellow  = "\033[90;43m"
	BackRed     = "\033[97;41m"
	BackBlue    = "\033[97;44m"
	BackMagenta = "\033[97;45m"
	BackCyan    = "\033[97;46m"

	ResetColor = "\033[0m"
)
View Source
var (
	DefaultColorFlag     = DisableColor
	DefaultPrintFileFlag = CallerShortFile
	DefaultFatalNoTrace  = false
	DefaultLevel         = INFO
	DefaultWriters       = map[Level]io.Writer{
		DEBUG: os.Stdout,
		INFO:  os.Stdout,
		WARN:  os.Stdout,
		ERROR: os.Stderr,
		PANIC: os.Stderr,
		FATAL: os.Stderr,
	}
)

默认值

View Source
var LogTag = map[Level]string{
	DEBUG: "DEBUG",
	INFO:  "INFO",
	WARN:  "WARN",
	ERROR: "ERROR",
	PANIC: "PANIC",
	FATAL: "FATAL",
}

级别及名称映射

Functions

func FramesToCaller

func FramesToCaller() int

func GetLogging

func GetLogging() util.Value

func ResetFactory

func ResetFactory(fac LoggerFactoryI)

ResetFactory 重新配置全局的默认LoggerFactory,该方法同时会重置全局的默认Logging 由于线程安全性受defaultLogging、defaultFactory初始化(调用InitOnce)的Value决定, 所以需要确定是否确实需要调用该方法重置Logging,并保证Value线程安全

func ResetLogging

func ResetLogging(logging Logging)

// ResetLogging 重新配置全局的默认Logging,该方法同时会重置全局的默认LoggerFactory的Logging // 由于线程安全性受defaultLogging、defaultFactory初始化(调用InitOnce)的Value决定, // 所以需要确定是否确实需要调用该方法重置Logging,并保证Value线程安全

func SetCallerFlag

func SetCallerFlag(flag int) func(*logging)

SetCallerFlag 配置内置Logging实现的文件输出标志,有ShortFile、LongFile

func SetCallerFormatter

func SetCallerFormatter(f func(file string, line int, funcName string) string) func(*logging)

SetCallerFormatter 配置内置Logging实现的时间格式化函数

func SetColorFlag

func SetColorFlag(flag int) func(*logging)

SetColorFlag 配置内置Logging实现的颜色的标志,有AutoColor、DisableColor、ForceColor

func SetExitFunc

func SetExitFunc(f ExitFunc) func(*logging)

SetExitFunc 配置内置Logging Fatal退出处理函数

func SetFatalNoTrace

func SetFatalNoTrace(noTrace bool) func(*logging)

SetFatalNoTrace 配置内置Logging实现是否在发生致命错误时打印堆栈,默认打印

func SetLogLevel

func SetLogLevel(level Level) func(*logging)

SetLogLevel

func SetPanicFunc

func SetPanicFunc(f PanicFunc) func(*logging)

SetPanicFunc 配置内置Logging Panic处理函数

func SetTimeFormatter

func SetTimeFormatter(f func(t time.Time) string) func(*logging)

SetTimeFormatter 配置内置Logging实现的时间格式化函数

func SimplifyNameFirstLetter

func SimplifyNameFirstLetter(s string) string

Types

type ExitFunc

type ExitFunc func(code int)

type Level

type Level = int32
const (
	FATAL Level = 0
	PANIC Level = 1
	ERROR Level = 2
	WARN  Level = 3
	INFO  Level = 4
	DEBUG Level = 5
)

type LogDebug

type LogDebug interface {
	DebugEnabled() bool
	Debug(args ...interface{})
	DebugLn(args ...interface{})
	DebugF(fmt string, args ...interface{})
}

LogDebug interface

type LogError

type LogError interface {
	ErrorEnabled() bool
	Error(args ...interface{})
	ErrorLn(args ...interface{})
	ErrorF(fmt string, args ...interface{})
}

LogError interface

type LogFatal

type LogFatal interface {
	FatalEnabled() bool
	Fatal(args ...interface{})
	FatalLn(args ...interface{})
	FatalF(fmt string, args ...interface{})
}

LogFatal interface

type LogInfo

type LogInfo interface {
	InfoEnabled() bool
	Info(args ...interface{})
	InfoLn(args ...interface{})
	InfoF(fmt string, args ...interface{})
}

LogInfo interface

type LogPanic

type LogPanic interface {
	PanicEnabled() bool
	Panic(args ...interface{})
	PanicLn(args ...interface{})
	PanicF(fmt string, args ...interface{})
}

LogPanic interface

type LogWarn

type LogWarn interface {
	WarnEnabled() bool
	Warn(args ...interface{})
	WarnLn(args ...interface{})
	WarnF(fmt string, args ...interface{})
}

LogWarn interface

type Logger

type Logger interface {
	LogDebug
	LogInfo
	LogWarn
	LogError
	// LogPanic Panic level log interface. Note that panic will be triggered
	LogPanic
	// LogFatal Fatal level log interface, please note that it will trigger the program exit
	LogFatal

	// WithName 附加日志名称,注意会附加父Logger的名称,格式为:父Logger名称 + '.' + name
	WithName(name string) Logger

	// WithFields 附加日志信息,注意会附加父Logger的附加信息,如果相同则会覆盖
	WithFields(keyAndValues ...interface{}) Logger

	// WithDepth 配置日志的调用深度,注意会在父Logger的基础上调整深度
	WithDepth(depth int) Logger
}

Logger interface 实现了常用的日志方法

func GetLogger

func GetLogger(o ...interface{}) Logger

GetLogger 通过全局默认LoggerFactory获取Logger Param:根据默认实现,o可不填,直接返回一个没有名称的Logger。 如果o有值,则只取第一个值,且当:

o为string时,使用string值作为Logger名称
o为其他类型时,取package path + type name作为Logger名称,以"."分隔,如g.x.x.t.TestStructInTest

type LoggerFactory

type LoggerFactory struct {
	Value            util.Value
	SimplifyNameFunc func(string) string
}

func NewDefaultFactory

func NewDefaultFactory(opts ...LoggingOpt) *LoggerFactory

func NewFactory

func NewFactory(logging Logging) *LoggerFactory

func NewFactoryWithValue

func NewFactoryWithValue(v util.Value) *LoggerFactory

func (*LoggerFactory) GetLogger

func (fac *LoggerFactory) GetLogger(o ...interface{}) Logger

func (*LoggerFactory) GetLogging

func (fac *LoggerFactory) GetLogging() Logging

func (*LoggerFactory) Reset

func (fac *LoggerFactory) Reset(logging Logging) LoggerFactoryI

type LoggerFactoryI

type LoggerFactoryI interface {
	// GetLogger 根据参数获得Logger
	// Param:根据默认实现,o可不填,直接返回一个没有名称的Logger。
	// 如果o有值,则只取第一个值,且当:
	// 		o为string时,使用string值作为Logger名称
	//		o为其他类型时,取package path + type name作为Logger名称,以"."分隔,如g.x.x.t.TestStructInTest
	GetLogger(o ...interface{}) Logger

	// Reset 重置Factory的Logging(线程安全)
	Reset(logging Logging) LoggerFactoryI

	// GetLogging 获得Factory的Logging(线程安全),可用来配置Logging
	// 也可以通过wrap Logging达到控制日志级别、日志输出格式的目的
	GetLogging() Logging
}

type Logging

type Logging interface {
	LogF(level Level, depth int, keyValues util.KeyValues, format string, args ...interface{})

	Log(level Level, depth int, keyValues util.KeyValues, args ...interface{})

	LogLn(level Level, depth int, keyValues util.KeyValues, args ...interface{})

	// SetFormatter setting Formatter
	SetFormatter(f util.Formatter)

	// SetLogLevel 设置日志严重级别,低于该级别的将不被输出(线程安全)
	SetLogLevel(severityLevel Level)

	// IsEnabled 判断参数级别是否会输出(线程安全)
	IsEnabled(severityLevel Level) bool

	// SetOutput 设置输出的Writer,注意该方法会将所有级别都配置为参数writer(线程安全)
	SetOutput(w io.Writer)

	// SetOutputBySeverity 设置对应日志级别的Writer(线程安全)
	SetOutputBySeverity(severityLevel Level, w io.Writer)

	// GetOutputBySeverity 获得对应日志级别的Writer(线程安全)
	GetOutputBySeverity(severityLevel Level) io.Writer

	// Clone 获得一个clone的对象(线程安全)
	Clone() Logging
}

Logging base

func DefaultLogging

func DefaultLogging() Logging

DefaultLogging 获得默认Logging

func NewLogging

func NewLogging(opts ...LoggingOpt) Logging

type LoggingOpt

type LoggingOpt func(l *logging)

type PanicFunc

type PanicFunc func(interface{})

Jump to

Keyboard shortcuts

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