log

package
v0.0.0-...-f382dc8 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: GPL-3.0 Imports: 16 Imported by: 0

README

Log

Features

  • Level
  • Format
  • Syslog
  • Colored output
  • ELog

Documentation

Index

Constants

View Source
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}}] ## {{}}"
)
View Source
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

View Source
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
)
View Source
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

func AttachFlagSet(flagSet *flag.FlagSet)

AttachFlagSet attaches a flag to the given FlagSet indicating the global log level

Passing nil flagSet for default FlagSet(flag.CommandLine)

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns true if the given writer supports colored output

func ParseFlag

func ParseFlag() error

ParseFlag should be used after AttachFlagSet

func SetBufferSize

func SetBufferSize(size int)

SetBufferSize sets the default buffer size for async logging Default to 1024 NOTE: Call this before logging anything

func SetGlobalAppID

func SetGlobalAppID(appID string)

SetGlobalAppID sets the global AppID

func SetGlobalLevel

func SetGlobalLevel(lv LevelType)

SetGlobalLevel sets the global log level

func Wait

func Wait()

Wait wait for all writer worker close

Types

type AsyncedLogger

type AsyncedLogger interface {
	SetAsync(async bool)
}

AsyncedLogger async log

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) Colored

func (f *BaseFormatter) Colored() bool

Colored return is colored.

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) Name

func (br *BaseRecord) Name() string

Name return the name 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 Handler

type Handler interface {
	Log(record Record)
	Writer() io.Writer
}

Handler represents a handler 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
)

func GlobalLevel

func GlobalLevel() LevelType

GlobalLevel returns the global log level

type Leveler

type Leveler interface {
	Level() LevelType
	SetLevel(lv LevelType)
}

Leveler represents a leveled object

type Logger

type Logger struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Logger is an object for logging with a set of configurations, including name, level, logging format, and multiple handlers

func NewWithFormat

func NewWithFormat(name string, w io.Writer, format string) *Logger

NewWithFormat creates a Logger with given Writer and format

func NewWithWriter

func NewWithWriter(name string, w io.Writer) *Logger

NewWithWriter creates a Logger with given Writer as the only output

func (*Logger) AddHandler

func (l *Logger) AddHandler(h Handler)

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) Debugf

func (l *Logger) Debugf(format string, a ...interface{})

Debugf calls Output to log with DEBUG level and given format

func (*Logger) Error

func (l *Logger) Error(a ...interface{})

Error calls Output to log with ERRO level

func (*Logger) Errorf

func (l *Logger) Errorf(f string, a ...interface{})

Errorf calls Output to log with ERRO level and given format

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

func (l *Logger) Fatalf(f string, a ...interface{})

Fatalf calls Output to log with FATA level with given format, followed by a call to os.Exit(1)

func (*Logger) Handlers

func (l *Logger) Handlers() []Handler

Handlers returns all handlers

func (*Logger) Info

func (l *Logger) Info(a ...interface{})

Info calls Output to log with INFO level

func (*Logger) Infof

func (l *Logger) Infof(f string, a ...interface{})

Infof calls Output to log with INFO level and given format

func (*Logger) Level

func (l *Logger) Level() LevelType

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:

  1. logger's own level (if set)
  2. GlobalLevel (if set)
  3. defaultLevel (built-in, usually INFO)

func (*Logger) Name

func (l *Logger) Name() string

Name returns the name of logger

func (*Logger) Output

func (l *Logger) Output(record Record)

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) Printf

func (l *Logger) Printf(f string, a ...interface{})

Printf calls Output to log with default level and given format

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

func (l *Logger) RemoveHandler(h Handler)

RemoveHandler removes a handler

func (*Logger) SetAsync

func (l *Logger) SetAsync(async bool)

SetAsync set output as async

func (*Logger) SetLevel

func (l *Logger) SetLevel(lv LevelType)

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.

func (*Logger) Warn

func (l *Logger) Warn(a ...interface{})

Warn calls Output to log with WARN level

func (*Logger) Warnf

func (l *Logger) Warnf(f string, a ...interface{})

Warnf calls Output to log with WARN level and given format

type MultiHandler

type MultiHandler interface {
	AddHandler(h Handler)
	RemoveHandler(h Handler)
	Handlers() []Handler
}

MultiHandler represents an object with multiple logging handlers

type NamedLeveler

type NamedLeveler interface {
	Namer
	Leveler
}

NamedLeveler is the combination of Namer and Leveler

type Namer

type Namer interface {
	Name() string
}

Namer represents a named object

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

type RecordFactory func(name string, calldepth int, lv LevelType, msg string) Record

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.

func New

func New(name string) SimpleLogger

New creates a Logger with Stdout as default Output

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

func (*StreamHandler) Writer

func (sw *StreamHandler) Writer() io.Writer

Writer return the writer

type Warner

type Warner interface {
	Warn(a ...interface{})
	Warnf(f string, a ...interface{})
}

Warner represents a logger with Warn APIs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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