Documentation ¶
Overview ¶
xylog is a logging module based on the design of python logging.
Example ¶
package main import ( "os" "github.com/xybor/xyplatform/xylog" ) func main() { // You can directly use xylog functions to log with the root logger. var handler = xylog.NewHandler("", xylog.NewStreamEmitter(os.Stdout)) xylog.SetLevel(xylog.DEBUG) xylog.AddHandler(handler) xylog.Debug("foo") // Handlers in the root logger will affect to other logger, so in this // example, it should remove this handler from the root logger. xylog.RemoveHandler(handler) }
Output: foo
Index ¶
- Constants
- Variables
- func AddFilter(f Filter)
- func AddHandler(h *Handler)
- func AddLevel(level int, levelName string)
- func Critical(msg string, a ...any)
- func Debug(msg string, a ...any)
- func Error(msg string, a ...any)
- func Fatal(msg string, a ...any)
- func Info(msg string, a ...any)
- func Log(level int, msg string, a ...any)
- func NewTextFormatter(f string) textFormatter
- func RemoveFilter(f Filter)
- func RemoveHandler(h *Handler)
- func SetLevel(level int)
- func SetTimeLayout(layout string)
- func Warn(msg string, a ...any)
- func Warning(msg string, a ...any)
- type Emitter
- type Filter
- type Formatter
- type Handler
- type LogRecord
- type Logger
- func (lg *Logger) AddFilter(f Filter)
- func (lg *Logger) AddHandler(h *Handler)
- func (lg *Logger) Critical(msg string, a ...any)
- func (lg *Logger) Debug(msg string, a ...any)
- func (lg *Logger) Error(msg string, a ...any)
- func (lg *Logger) Fatal(msg string, a ...any)
- func (lg *Logger) GetFilters() []Filter
- func (lg *Logger) GetHandlers() []*Handler
- func (lg *Logger) Info(msg string, a ...any)
- func (lg *Logger) Log(level int, msg string, a ...any)
- func (lg *Logger) RemoveFilter(f Filter)
- func (lg *Logger) RemoveHandler(h *Handler)
- func (lg *Logger) SetLevel(level int)
- func (lg *Logger) Warn(msg string, a ...any)
- func (lg *Logger) Warning(msg string, a ...any)
- type StreamEmitter
Examples ¶
Constants ¶
const ( CRITICAL = 50 FATAL = CRITICAL ERROR = 40 WARNING = 30 WARN = WARNING INFO = 20 DEBUG = 10 NOTSET = 0 )
Variables ¶
var StderrEmitter = NewStreamEmitter(os.Stderr)
StderrEmitter is a shortcut of NewStreamEmitter(os.Stderr)
var StdoutEmitter = NewStreamEmitter(os.Stdout)
StdoutEmitter is a shortcut of NewStreamEmitter(os.Stdout)
Functions ¶
func AddLevel ¶
AddLevel associates a log level with name. It can overwrite other log levels. Default log levels:
NOTSET 0 DEBUG 10 INFO 20 WARN/WARNING 30 ERROR/FATAL 40 CRITICAL 50
func NewTextFormatter ¶
func NewTextFormatter(f string) textFormatter
NewTextFormatter creates a textFormatter which uses LogRecord attributes to contribute logging string, e.g. %(message)s or %(levelno)d. See LogRecord for more details.
func RemoveFilter ¶
func RemoveFilter(f Filter)
RemoveFilter removes an existed filter from root logger.
func RemoveHandler ¶
func RemoveHandler(h *Handler)
RemoveHandler removes an existed handler from root logger.
func SetTimeLayout ¶
func SetTimeLayout(layout string)
SetTimeLayout sets the time layout to print asctime. It is time.RFC3339Nano by default.
Types ¶
type Emitter ¶ added in v0.0.2
type Emitter interface {
Emit(string)
}
Emitter instances dispatch logging events to specific destinations.
type Filter ¶
Filter instances are used to perform arbitrary filtering of LogRecord.
func GetFilters ¶ added in v0.0.2
func GetFilters() []Filter
GetFilters returns all filters of filterer.
type Formatter ¶
Formatter instances are used to convert a LogRecord to text.
Formatter need to know how a LogRecord is constructed. They are responsible for converting a LogRecord to a string which can be interpreted by either a human or an external system.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles logging events. Do NOT instantiated directly this struct.
Any Handler with a not-empty name will be associated with its name.
Example ¶
package main import ( "fmt" "github.com/xybor/xyplatform/xylog" ) func main() { // You can use a handler throughout program without storing it in global // scope. All handlers can be identified by their names. var handlerA = xylog.NewHandler("example", xylog.StdoutEmitter) var handlerB = xylog.GetHandler("example") if handlerA == handlerB { fmt.Println("handlerA == handlerB") } else { fmt.Println("handlerA != handlerB") } // In case name is an empty string, it totally is a fresh handler. var handlerC = xylog.NewHandler("", xylog.StdoutEmitter) var handlerD = xylog.NewHandler("", xylog.StdoutEmitter) if handlerC == handlerD { fmt.Println("handlerC == handlerD") } else { fmt.Println("handlerC != handlerD") } }
Output: handlerA == handlerB handlerC != handlerD
func GetHandler ¶ added in v0.0.2
GetHandler returns the handler associated with the name. If no handler found, returns nil.
func GetHandlers ¶ added in v0.0.2
func GetHandlers() []*Handler
GetHandlers returns all handlers of root logger.
func NewHandler ¶ added in v0.0.2
NewHandler creates a Handler with a specified Emitter.
Any Handler with a not-empty name will be associated with its name. Calling NewHandler twice with the same name will cause a panic. If you want to create an anonymous Handler, call this function with an empty name.
func (*Handler) GetFilters ¶ added in v0.0.2
GetFilters returns all filters of filterer.
func (*Handler) RemoveFilter ¶ added in v0.0.2
RemoveFilter removes an existed filter.
func (*Handler) SetFormatter ¶ added in v0.0.2
SetFormatter sets the new formatter of handler.
type LogRecord ¶
type LogRecord struct { // Textual time when the LogRecord was created. Asctime string `map:"asctime"` // Time when the LogRecord was created (time.Now().Unix() return value). Created int64 `map:"created"` // Filename portion of pathname. FileName string `map:"filename"` // Function name logged the record. FuncName string `map:"funcname"` // Text logging level for the message ("DEBUG", "INFO", "WARNING", "ERROR", // "CRITICAL"). LevelName string `map:"levelname"` // Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, // CRITICAL). LevelNo int `map:"levelno"` // Source line number where the logging call was issued. LineNo int `map:"lineno"` // The logging message. Message string `map:"message"` // The module called log method. Module string `map:"module"` // Millisecond portion of the creation time. Msecs int `map:"msecs"` // Name of the logger. Name string `map:"name"` // Full pathname of the source file where the logging call was issued. PathName string `map:"pathname"` // Process ID. Process int `map:"process"` // Time in milliseconds when the LogRecord was created, relative to the time // the logging module was loaded (typically at application startup time). RelativeCreated int64 `map:"relativeCreated"` }
A LogRecord instance represents an event being logged.
LogRecord instances are created every time something is logged. They contain all the information pertinent to the event being logged. The main information passed in is Message. The record also includes information as when the record was created or the source line where the logging call was made.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a single logging channel. A "logging channel" indicates an area of an application. Exactly how an "area" is defined is up to the application developer. Since an application can have any number of areas, logging channels are identified by a unique string. Application areas can be nested (e.g. an area of "input processing" might include sub-areas "read CSV files", "read XLS files" and "read Gnumeric files"). To cater for this natural nesting, channel names are organized into a namespace hierarchy where levels are separated by periods. So in the instance given above, channel names might be "input" for the upper level, and "input.csv", "input.xls" and "input.gnu" for the sub-levels. There is no arbitrary limit to the depth of nesting.
func GetLogger ¶
GetLogger gets a logger with the specified name (channel name), creating it if it doesn't yet exist. This name is a dot-separated hierarchical name, such as "a", "a.b", "a.b.c" or similar.
Leave name as empty string to get the root logger.
Example ¶
package main import ( "os" "github.com/xybor/xyplatform/xylog" ) func main() { var handler = xylog.NewHandler("", xylog.NewStreamEmitter(os.Stdout)) handler.SetFormatter(xylog.NewTextFormatter( "module=%(name)s level=%(levelname)s %(message)s")) var logger = xylog.GetLogger("example") logger.AddHandler(handler) logger.SetLevel(xylog.DEBUG) logger.Debug("foo %s", "bar") }
Output: module=example level=DEBUG foo bar
func (*Logger) GetFilters ¶ added in v0.0.2
GetFilters returns all filters of filterer.
func (*Logger) GetHandlers ¶ added in v0.0.2
GetHandlers returns all handlers of logger.
func (*Logger) RemoveFilter ¶
RemoveFilter removes an existed filter.
func (*Logger) RemoveHandler ¶
RemoveHandler removes an existed handler.
type StreamEmitter ¶ added in v0.0.2
type StreamEmitter struct {
// contains filtered or unexported fields
}
StreamEmitter writes logging message to a stream. Note that this class does not close the stream, as os.Stdout or os.Stderr may be used.
func NewStreamEmitter ¶ added in v0.0.2
func NewStreamEmitter(f io.Writer) *StreamEmitter
NewStreamEmitter creates a StreamEmitter which writes message to a stream (os.Stderr by default).
func (*StreamEmitter) Emit ¶ added in v0.0.2
func (e *StreamEmitter) Emit(msg string)
Emit will be called after a record was decided to log.