log

package module
v0.0.0-...-559d3c8 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 12 Imported by: 1

README

log

It is a logger for golang, which has met many of my needs. It cat set different formatter with the same appender. Also it can set multi-appender for a logger instance.

All the new logger inherited the global logger. If the new logger is not set the formatter or appender, it will inherited the global logger's formatter and appender.

Level

This logger has 6 log-level.

FATAL
ERROR
INFO
WARN
DEBUG
TRACE

We can use the SetLevel or Logger.SetLevel to chang the log-level for each logger instance.

Fomatter

We can set a format layout to the each level for the logger instance. The global logger default format layout is %F %T [%l] %m. The pattern is:

    %m => the log message and its arguments formatted with `fmt.Sprintf` or `fmt.Sprint`
    %l => the log-level string
    %C => the caller with full file path
    %c => the caller with short file path
    %L => the line number of caller
    %% => '%'
    %n => '\n'
    %F => the date formatted like "2006-01-02"
    %D => the date formatted like "01/02/06"
    %T => the time formatted like 24h style "15:04:05"
    %a => the short name of weekday like "Mon"
    %A => the full name of weekday like "Monday"
    %b => the short name of month like "Jan"
    %B => the full name of month like "January"
    %d => the datetime formatted like RFC3339 "2006-01-02T15:04:05Z07:00"

Install

go get -u github.com/lrita/log

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HourlySuffix = ".20060102-15"
	DailySuffix  = ".20060102"
)
View Source
var ExitOnFatal = true

ExitOnFatal decides whether or not to exit when fatal log printing.

View Source
var LevelsToString = map[Level]string{
	TRACE: "TRACE",
	DEBUG: "DEBUG",
	INFO:  "INFO",
	WARN:  "WARN",
	ERROR: "ERROR",
	FATAL: "FATAL",
}
View Source
var StringToLevels = map[string]Level{
	"TRACE": TRACE,
	"DEBUG": DEBUG,
	"INFO":  INFO,
	"WARN":  WARN,
	"ERROR": ERROR,
	"FATAL": FATAL,
}

Functions

func Debug

func Debug(v ...interface{})

func Debugf

func Debugf(fmt string, v ...interface{})

func Error

func Error(v ...interface{})

func Errorf

func Errorf(fmt string, v ...interface{})

func Fatal

func Fatal(v ...interface{})

func Fatalf

func Fatalf(fmt string, v ...interface{})

func Info

func Info(v ...interface{})

func Infof

func Infof(fmt string, v ...interface{})

func IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled indicates whether debug level is enabled

func SetAppender

func SetAppender(appender Appender, levels ...Level)

SetAppender set append for global logger

func SetCallDepth

func SetCallDepth(d int)

SetCallDepth set callee stack depth

func SetFormat

func SetFormat(fmt string, levels ...Level)

SetFormat set format-string for global logger

func SetLevel

func SetLevel(level Level)

SetLevel log level for global logger

func SetRatelimit

func SetRatelimit(limit int64, levels ...Level)

SetRatelimit set log rate limit for global logger

func Trace

func Trace(v ...interface{})

func Tracef

func Tracef(fmt string, v ...interface{})

func Warn

func Warn(v ...interface{})

func Warnf

func Warnf(fmt string, v ...interface{})

Types

type AIO

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

AIO implements buffering asynchronous Writer for an io.Writer object. Which can reduce the latency spike of api occurrence by disk/system latency. If an error occurs writing to a Writer, no more data will be accepted and all subsequent writes, and Flush, will return the error. After all data has been written, the client should call the Flush method to guarantee all data has been forwarded to the underlying io.Writer.

func NewAIO

func NewAIO(w io.Writer, size int) *AIO

NewAIO returns a new Writer whose buffer has at least the specified size. If the argument io.Writer is already a Writer with large enough size, it returns the underlying Writer.

func (*AIO) Available

func (a *AIO) Available() int

Available returns how many bytes are unused in the buffer.

func (*AIO) Buffered

func (a *AIO) Buffered() int

Buffered returns the number of bytes that have been written into the current buffer.

func (*AIO) Flush

func (a *AIO) Flush() error

Flush writes any buffered data to the underlying io.Writer.

func (*AIO) Reset

func (a *AIO) Reset(w io.Writer)

Reset discards any unflushed buffered data, clears any error, and resets b to write its output to w.

func (*AIO) Write

func (a *AIO) Write(p []byte) (nn int, err error)

Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.

type Appender

type Appender interface {
	// Output will be invoked by Logger. The Logger input a formatted data
	// to the appender using Output. And the data is only valid during the
	// Output invoking, if you want do something async with data, you need
	// copy it yourself.
	Output(level Level, t time.Time, data []byte)
}

func NewConsoleAppender

func NewConsoleAppender() Appender

type Flusher

type Flusher interface {
	Flush() error
}

type Level

type Level int8
const (
	FATAL Level = iota
	ERROR
	WARN
	INFO
	DEBUG
	TRACE
)

type Logger

type Logger interface {
	// New return a new log handler which inherit its appender and formater
	New(name string) Logger
	// Level return the logger current log-level
	Level() Level
	// SetLevel set the logger current log-level
	SetLevel(level Level)
	// SetAppender the given log-level to use the special appender.
	// If non-given log-level, all log-level use it
	SetAppender(appender Appender, levels ...Level)
	// SetRatelimit the give limit(QPS) rate to the logger.
	SetRatelimit(limit int64, levels ...Level)
	// SetFormat the given log-level to use the special format.
	// If non-given log-level, all log-level use it
	// fmt is a pattern-string, default is "%F %T [%l] %m"
	// %m => the log message and its arguments formatted with `fmt.Sprintf` or `fmt.Sprint`
	// %l => the log-level string
	// %C => the caller with full file path
	// %c => the caller with short file path
	// %L => the line number of caller
	// %% => '%'
	// %n => '\n'
	// %F => the date formatted like "2006-01-02"
	// %D => the date formatted like "01/02/06"
	// %T => the time formatted like 24h style "15:04:05"
	// %a => the short name of weekday like "Mon"
	// %A => the full name of weekday like "Monday"
	// %b => the short name of month like "Jan"
	// %B => the full name of month like "January"
	// %d => the datetime formatted like RFC3339 "2006-01-02T15:04:05Z07:00"
	SetFormat(fmt string, levels ...Level)
	// SetCallDepth set callee stack depth
	SetCallDepth(d int)
	// IsDebugEnabled indicates whether debug level is enabled
	IsDebugEnabled() bool

	Fatal(v ...interface{})
	Error(v ...interface{})
	Info(v ...interface{})
	Warn(v ...interface{})
	Debug(v ...interface{})
	Trace(v ...interface{})

	Fatalf(fmt string, v ...interface{})
	Errorf(fmt string, v ...interface{})
	Infof(fmt string, v ...interface{})
	Warnf(fmt string, v ...interface{})
	Debugf(fmt string, v ...interface{})
	Tracef(fmt string, v ...interface{})
}

func New

func New(name string) Logger

New return a sub logger of global logger

type Reseter

type Reseter interface {
	Reset(w io.Writer)
}

type RotateAppender

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

func NewDailyRotateAppender

func NewDailyRotateAppender(filename string) (*RotateAppender, error)

func NewDailyRotateBufAppender

func NewDailyRotateBufAppender(filename string, bufsize int) (*RotateAppender, error)

func NewHourlyRotateAppender

func NewHourlyRotateAppender(filename string) (*RotateAppender, error)

func NewHourlyRotateBufAppender

func NewHourlyRotateBufAppender(filename string, bufsize int) (*RotateAppender, error)

func (*RotateAppender) Close

func (a *RotateAppender) Close() error

func (*RotateAppender) Flush

func (a *RotateAppender) Flush() error

func (*RotateAppender) Output

func (a *RotateAppender) Output(_ Level, t time.Time, data []byte)

Jump to

Keyboard shortcuts

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