logger

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2021 License: MIT Imports: 9 Imported by: 0

README

logger

logger

TODO 提供 Hook 功能

TODO formatter 应该提供 jsonFormatter 格式,参考 logrus

Project fork from github.com/koding/logging

Documentation

Overview

Package logger is an alternative to log package in standard library.

Index

Constants

View Source
const (
	CRITICAL level = iota
	ERROR
	WARNING
	NOTICE
	INFO
	DEBUG
)

Logger levels.

View Source
const (
	BLACK color = iota + 30
	RED
	GREEN
	YELLOW
	BLUE
	MAGENTA
	CYAN
	WHITE
)

Colors for different log levels.

Variables

View Source
var (
	// DefaultLogger holds default logger
	DefaultLogger Logger = NewLogger(procName())

	// DefaultLevel holds default value for loggers
	DefaultLevel level = INFO

	// DefaultFormatter holds default formatter for loggers
	DefaultFormatter Formatter = &defaultFormatter{}

	// DefaultHandler holds default handler for loggers
	DefaultHandler Handler = stderrHandler
)

Functions

func Critical

func Critical(format string, args ...interface{})

Critical prints a critical level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

func Debug

func Debug(format string, args ...interface{})

Debug prints a debug level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

func Error

func Error(format string, args ...interface{})

Error prints a error level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

func Fatal

func Fatal(format string, args ...interface{})

Fatal is equivalent to Critical() followed by a call to os.Exit(1).

func Info

func Info(format string, args ...interface{})

Info prints a info level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

func Notice

func Notice(format string, args ...interface{})

Notice prints a notice level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

func Panic

func Panic(format string, args ...interface{})

Panic is equivalent to Critical() followed by a call to panic().

func Warning

func Warning(format string, args ...interface{})

Warning prints a warning level log message to the stderr. Arguments are handled in the manner of fmt.Printf.

Types

type BaseHandler

type BaseHandler struct {
	Level     level
	Formatter Formatter
}

BaseHandler provides basic functionality for handler

func NewBaseHandler

func NewBaseHandler() *BaseHandler

NewBaseHandler creates a newBaseHandler with default values

func (*BaseHandler) FilterAndFormat

func (h *BaseHandler) FilterAndFormat(rec *Record) string

FilterAndFormat filters any record according to logger level

func (*BaseHandler) SetFormatter

func (h *BaseHandler) SetFormatter(f Formatter)

SetFormatter sets logger formatter for handler

func (*BaseHandler) SetLevel

func (h *BaseHandler) SetLevel(l level)

SetLevel sets logger level for handler

type CustomFormatter

type CustomFormatter struct{}

func (*CustomFormatter) Format

func (f *CustomFormatter) Format(rec *Record) string

type Formatter

type Formatter interface {
	// Format the record and return a message.
	Format(*Record) (message string)
}

Formatter formats a record.

type Handler

type Handler interface {
	SetFormatter(Formatter)
	SetLevel(level)

	// Handle single log record.
	Handle(*Record)

	// Close the handler.
	Close()
}

Handler handles the output.

type Logger

type Logger interface {
	// SetLevel changes the level of the logger. Default is logging.Info.
	SetLevel(level)

	// SetHandler replaces the current handler for output. Default is logger.stderrHandler.
	SetHandler(Handler)

	// SetCallDepth sets the parameter passed to runtime.Caller().
	// It is used to get the file name from call stack.
	// For example you need to set it to 1 if you are using a wrapper around
	// the Logger. Default value is zero.
	SetCallDepth(int)

	// New creates a new inerhited context logger with given prefixes.
	New(prefixes ...interface{}) Logger

	// Fatal is equivalent to l.Critical followed by a call to os.Exit(1).
	Fatal(format string, args ...interface{})

	// Panic is equivalent to l.Critical followed by a call to panic().
	Panic(format string, args ...interface{})

	// Critical logs a message using CRITICAL as log level.
	Critical(format string, args ...interface{})

	// Error logs a message using ERROR as log level.
	Error(format string, args ...interface{})

	// Warning logs a message using WARNING as log level.
	Warning(format string, args ...interface{})

	// Notice logs a message using NOTICE as log level.
	Notice(format string, args ...interface{})

	// Info logs a message using INFO as log level.
	Info(format string, args ...interface{})

	// Debug logs a message using DEBUG as log level.
	Debug(format string, args ...interface{})
}

Logger is the interface for output log messages in different levels. A new Logger can be created with NewLogger() function. You can changed the output handler with SetHandler() function.

func NewCustom

func NewCustom(name string, debug bool) Logger

func NewLogger

func NewLogger(name string) Logger

type MultiHandler

type MultiHandler struct {
	// contains filtered or unexported fields
}

MultiHandler sends the log output to multiple handlers concurrently.

func NewMultiHandler

func NewMultiHandler(handlers ...Handler) *MultiHandler

NewMultiHandler creates a new handler with given handlers

func (*MultiHandler) Close

func (b *MultiHandler) Close()

Close closes all handlers concurrently

func (*MultiHandler) Handle

func (b *MultiHandler) Handle(rec *Record)

Handle handles given record with all handlers concurrently

func (*MultiHandler) SetFormatter

func (b *MultiHandler) SetFormatter(f Formatter)

SetFormatter sets formatter for all handlers

func (*MultiHandler) SetLevel

func (b *MultiHandler) SetLevel(l level)

SetLevel sets level for all handlers

type Record

type Record struct {
	Format      string        // Format string
	Args        []interface{} // Arguments to format string
	LoggerName  string        // Name of the logger module
	Level       level         // Level of the record
	Time        time.Time     // Time of the record (local time)
	Filename    string        // File name of the log call (absolute path)
	Line        int           // Lint number in file
	ProcessID   int           // PID
	ProcessName string        // Name of the process
}

Record contains all of the information about a single log message.

type SinkHandler

type SinkHandler struct {
	// contains filtered or unexported fields
}

SinkHandler sends log records to buffered channel, the logs are written in a dedicated routine consuming the channel.

func NewSinkHandler

func NewSinkHandler(inner Handler, bufSize int) *SinkHandler

func (*SinkHandler) Close

func (b *SinkHandler) Close()

Close closes the sink channel, inner handler will be closed when all pending logs are processed. Close blocks until all the logs are processed.

func (*SinkHandler) Handle

func (b *SinkHandler) Handle(rec *Record)

Handle puts rec to the sink.

func (*SinkHandler) SetFormatter

func (b *SinkHandler) SetFormatter(f Formatter)

SetFormatter sets logger formatter for handler.

func (*SinkHandler) SetLevel

func (b *SinkHandler) SetLevel(l level)

SetLevel sets logger level for handler.

func (*SinkHandler) Status

func (b *SinkHandler) Status() (int, int)

Status reports sink capacity and length.

type SyslogHandler

type SyslogHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

SyslogHandler sends the logger output to syslog.

func NewSyslogHandler

func NewSyslogHandler(tag string) (*SyslogHandler, error)

func (*SyslogHandler) Close

func (b *SyslogHandler) Close()

func (*SyslogHandler) Handle

func (b *SyslogHandler) Handle(rec *Record)

type WriterHandler

type WriterHandler struct {
	*BaseHandler

	Colorize bool
	// contains filtered or unexported fields
}

WriterHandler is a handler implementation that writes the logger output to a io.Writer.

func NewWriterHandler

func NewWriterHandler(w io.Writer) *WriterHandler

NewWriterHandler creates a new writer handler with given io.Writer

func (*WriterHandler) Close

func (b *WriterHandler) Close()

Close closes WriterHandler

func (*WriterHandler) Handle

func (b *WriterHandler) Handle(rec *Record)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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