Documentation ¶
Index ¶
- Constants
- Variables
- func AttachFlagSet(flagSet *flag.FlagSet)
- func IsTerminal(w io.Writer) bool
- func ParseFlag() error
- func SetBufferSize(size int)
- func SetGlobalAppID(appID string)
- func SetGlobalLevel(lv LevelType)
- func Wait()
- type AsyncedLogger
- type BaseFormatter
- func (f *BaseFormatter) AddFuncMap(funcMap template.FuncMap)
- func (f *BaseFormatter) AddTags(tags ...string)
- func (f *BaseFormatter) Colored() bool
- func (f *BaseFormatter) Format(record Record) []byte
- func (f *BaseFormatter) Paint(lv LevelType, s string) string
- func (f *BaseFormatter) ParseFormat(format string) error
- func (f *BaseFormatter) SetColored(colored bool)
- type BaseRecord
- type Debugger
- type Errorer
- type Fataler
- type Formatter
- type Handler
- type Infoer
- type LevelType
- type Leveler
- type Logger
- func (l *Logger) AddHandler(h Handler)
- func (l *Logger) Debug(a ...interface{})
- func (l *Logger) Debugf(format string, a ...interface{})
- func (l *Logger) Error(a ...interface{})
- func (l *Logger) Errorf(f string, a ...interface{})
- func (l *Logger) Fatal(a ...interface{})
- func (l *Logger) Fatalf(f string, a ...interface{})
- func (l *Logger) Handlers() []Handler
- func (l *Logger) Info(a ...interface{})
- func (l *Logger) Infof(f string, a ...interface{})
- func (l *Logger) Level() LevelType
- func (l *Logger) Name() string
- func (l *Logger) Output(record Record)
- func (l *Logger) Print(a ...interface{})
- func (l *Logger) Printf(f string, a ...interface{})
- func (l *Logger) Println(a ...interface{})
- func (l *Logger) RecordFactory() RecordFactory
- func (l *Logger) RemoveHandler(h Handler)
- func (l *Logger) SetAsync(async bool)
- func (l *Logger) SetLevel(lv LevelType)
- func (l *Logger) SetRecordFactory(recordFactory RecordFactory)
- func (l *Logger) Warn(a ...interface{})
- func (l *Logger) Warnf(f string, a ...interface{})
- type MultiHandler
- type NamedLeveler
- type Namer
- type Printer
- type RPCLogger
- type Record
- type RecordFactory
- type SimpleLogger
- type StreamHandler
- type Warner
Constants ¶
const ( // TplDefault is the default log format. TplDefault = "{{level}} {{datetime}} {{name}} {{}}" // TplSyslog is the log format for syslog. TplSyslog = "[{{app_id}} {{rpc_id}} {{request_id}}] ## {{}}" )
const ( // LDate is the layout of date LDate = "2006-01-02" // LTime is the laytou of time LTime = "15:04:05" // LDatetime is the layout of datetime LDatetime = LDate + " " + LTime + ".000" )
Variables ¶
var ( // Level returns the level of default logger Level = defaultLogger.Level // SetLevel sets the level of default logger SetLevel = defaultLogger.SetLevel // Print calls Output to print to the default logger. // Arguments are handled in the manner of fmt.Print. Print = defaultLogger.Print // Printf calls Output to print to the default logger. // Arguments are handled in the manner of fmt.Printf. Printf = defaultLogger.Printf // Println calls Output to print to the default logger. // Arguments are handled in the manner of fmt.Println. Println = defaultLogger.Println // Fatal is equivalent to Print() followed by a call to os.Exit(1). Fatal = defaultLogger.Fatal // Fatalf is equivalent to Print() followed by a call to os.Exit(1). Fatalf = defaultLogger.Fatalf )
var LevelName = map[LevelType]string{ DEBUG: "DEBUG", INFO: "INFO", WARN: "WARN", ERRO: "ERRO", FATA: "FATA", }
LevelName maps LevelType to human-readable string
Functions ¶
func AttachFlagSet ¶
AttachFlagSet attaches a flag to the given FlagSet indicating the global log level
Passing nil flagSet for default FlagSet(flag.CommandLine)
func IsTerminal ¶
IsTerminal returns true if the given writer supports colored output
func SetBufferSize ¶
func SetBufferSize(size int)
SetBufferSize sets the default buffer size for async logging Default to 1024 NOTE: Call this before logging anything
Types ¶
type BaseFormatter ¶
type BaseFormatter struct {
// contains filtered or unexported fields
}
BaseFormatter describes the format of outputting log
func NewBaseFormatter ¶
func NewBaseFormatter(colored bool) *BaseFormatter
NewBaseFormatter creates a BaseFormatter with given colored whether to color the output
func (*BaseFormatter) AddFuncMap ¶
func (f *BaseFormatter) AddFuncMap(funcMap template.FuncMap)
AddFuncMap used to add template.FuncMap in formater.
func (*BaseFormatter) AddTags ¶
func (f *BaseFormatter) AddTags(tags ...string)
AddTags add replacer oldnew tags in formatter.
func (*BaseFormatter) Format ¶
func (f *BaseFormatter) Format(record Record) []byte
Format formats a Record with set format
func (*BaseFormatter) Paint ¶
func (f *BaseFormatter) Paint(lv LevelType, s string) string
Paint used to Paint the log.
func (*BaseFormatter) ParseFormat ¶
func (f *BaseFormatter) ParseFormat(format string) error
ParseFormat parse the format of outputting log
The default format is "{{ level }} {{ date }} {{ time }} {{ name }} {{}}"
{{this is a placeholder}} which will be replaced by the actual content
Available placeholders:
{{}} The message provided by you e.g. l.Info(message) {{ level }} Log level in four UPPER-CASED letters e.g. INFO, WARN {{ l }} Log level in one UPPER-CASED letter e.g. I, W {{ data }} Date in format "2006-01-02" {{ time }} Time in format "15:04:05") {{ datetime }} Date and time in format "2006-01-02 15:04:05.999" {{ name }} Logger name {{ pid }} Current process ID {{ file_line }} Filename and line number in format "file.go:12"
func (*BaseFormatter) SetColored ¶
func (f *BaseFormatter) SetColored(colored bool)
SetColored set the value of colored.
type BaseRecord ¶
type BaseRecord struct {
// contains filtered or unexported fields
}
BaseRecord stands for a single record of log, usually a single line
func NewBaseRecord ¶
func NewBaseRecord(name string, calldepth int, lv LevelType, msg string) *BaseRecord
NewBaseRecord creates a BaseRecord with given name, calldepth, LevelType and msg.
func (*BaseRecord) AppID ¶
func (br *BaseRecord) AppID() string
AppID return the appID of BaseRecord.
func (*BaseRecord) Fileline ¶
func (br *BaseRecord) Fileline() string
Fileline return the name of BaseRecord.
func (*BaseRecord) Level ¶
func (br *BaseRecord) Level() LevelType
Level return the level of BaseRecord.
func (*BaseRecord) Now ¶
func (br *BaseRecord) Now() time.Time
Now return the now of time of BaseRecord.
func (*BaseRecord) String ¶
func (br *BaseRecord) String() string
String returns the raw message of the Record
type Debugger ¶
type Debugger interface { Debug(a ...interface{}) Debugf(format string, a ...interface{}) }
Debugger represents a logger with Debug APIs
type Errorer ¶
type Errorer interface { Error(a ...interface{}) Errorf(f string, a ...interface{}) }
Errorer represents a logger with Error APIs
type Fataler ¶
type Fataler interface { Fatal(a ...interface{}) Fatalf(f string, a ...interface{}) }
Fataler represents a logger with Fatal APIs
type Formatter ¶
type Formatter interface { ParseFormat(format string) error Format(record Record) []byte Colored() bool SetColored(colored bool) }
Formatter represents a formatter of record.
type Infoer ¶
type Infoer interface { Info(a ...interface{}) Infof(f string, a ...interface{}) }
Infoer represents a logger with Info APIs
type LevelType ¶
type LevelType int
LevelType identifies the level of a logger
const ( // NOTSET indicates the logger level not set NOTSET LevelType = iota // DEBUG indicates the logger level DEBUG DEBUG // INFO indicates the logger level INFO INFO // WARN indicates the logger level WARNING WARN // ERRO indicates the logger level ERROR ERRO // FATA indicates the logger level FATAL FATA )
type Logger ¶
Logger is an object for logging with a set of configurations, including name, level, logging format, and multiple handlers
func NewWithFormat ¶
NewWithFormat creates a Logger with given Writer and format
func NewWithWriter ¶
NewWithWriter creates a Logger with given Writer as the only output
func (*Logger) AddHandler ¶
AddHandler adds the given handler to logger
func (*Logger) Debug ¶
func (l *Logger) Debug(a ...interface{})
Debug calls Output to log with DEBUG level
func (*Logger) Error ¶
func (l *Logger) Error(a ...interface{})
Error calls Output to log with ERRO level
func (*Logger) Fatal ¶
func (l *Logger) Fatal(a ...interface{})
Fatal calls Output to log with FATA level followed by a call to os.Exit(1)
func (*Logger) Fatalf ¶
Fatalf calls Output to log with FATA level with given format, followed by a call to os.Exit(1)
func (*Logger) Info ¶
func (l *Logger) Info(a ...interface{})
Info calls Output to log with INFO level
func (*Logger) Level ¶
Level returns the current level of logger
logger.SetLevel is always authoritative, GlobalLevel is used if SetLevel is not called, otherwise defaultLevel is used.
Level() search priority:
- logger's own level (if set)
- GlobalLevel (if set)
- defaultLevel (built-in, usually INFO)
func (*Logger) Output ¶
Output writes a log to all writers with given record.
Normally, you won't need this.
func (*Logger) Print ¶
func (l *Logger) Print(a ...interface{})
Print calls Output to log with default level
func (*Logger) Println ¶
func (l *Logger) Println(a ...interface{})
Println calls Output to log with default level
func (*Logger) RecordFactory ¶
func (l *Logger) RecordFactory() RecordFactory
RecordFactory return the record factory
func (*Logger) RemoveHandler ¶
RemoveHandler removes a handler
func (*Logger) SetLevel ¶
SetLevel set the level of logger
SetLevel is always authoritative, See also logger.Level()
func (*Logger) SetRecordFactory ¶
func (l *Logger) SetRecordFactory(recordFactory RecordFactory)
SetRecordFactory set the recordFactory of Logger.
type MultiHandler ¶
MultiHandler represents an object with multiple logging handlers
type NamedLeveler ¶
NamedLeveler is the combination of Namer and Leveler
type Printer ¶
type Printer interface { Print(a ...interface{}) Println(a ...interface{}) Printf(f string, a ...interface{}) }
Printer represents a logger with Print APIs
type RPCLogger ¶
type RPCLogger interface { SimpleLogger // RPC APIs WithRPCID(string) RPCLogger WithRequestID(string) RPCLogger }
RPCLogger contains a SimpleLogger with extra RPC APIs
type Record ¶
type Record interface { Level() LevelType AppID() string Now() time.Time Name() string Fileline() string }
Record represents a record object.
type RecordFactory ¶
RecordFactory represents a factory of record.
func NewBaseRecordFactory ¶
func NewBaseRecordFactory() RecordFactory
NewBaseRecordFactory return a record factory for BaseRecord.
type SimpleLogger ¶
type SimpleLogger interface { // Basic NamedLeveler AsyncedLogger // multiple handlers MultiHandler // level APIs Debugger Printer Infoer Warner Errorer Fataler RecordFactory() RecordFactory Output(record Record) }
SimpleLogger represents a named logger which is capable of logging with multiple handlers and different levels.
Normally this is the logger you should use.
type StreamHandler ¶
type StreamHandler struct { Formatter // contains filtered or unexported fields }
StreamHandler is a Handler of Stream writer e.g. console
func NewStreamHandler ¶
func NewStreamHandler(w io.Writer, f Formatter) *StreamHandler
NewStreamHandler creates a StreamHandler with given writer(usually os.Stdout) and format string, whether to color the output is determined by the type of writer
func (*StreamHandler) Colored ¶
func (sw *StreamHandler) Colored(ok ...bool) bool
Colored enable or disable the color function of internal format, usually this is determined automatically
When called with no argument, it returns the current state of color function
func (*StreamHandler) Log ¶
func (sw *StreamHandler) Log(record Record)
Log print the Record to the internal writer