log

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 9 Imported by: 8

Documentation

Overview

Package log provides an improved logger

Example (Logger)
logger, err := New("/path/to/file.log", 0644)

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

// Enable buffered IO with 1-second flush interval
logger.EnableBufIO(time.Second)

// Set minimal log level
logger.MinLevel(INFO)

// Print message to log
logger.Print(DEBUG, "This is %s message", "debug")

// Package provides different methods for each level
logger.Debug("This is %d %s message", 2, "debug")
logger.Info("This is info message")
logger.Warn("This is warning message")
logger.Error("This is error message")
logger.Crit("This is critical message")

// AUX message it's unskippable message which will be printed to log file with
// any minimum level
logger.Aux("This is aux message")

// For log rotation we provide method Reopen
logger.Reopen()

// If buffered IO is used, you should flush data before exit
logger.Flush()
Output:

Index

Examples

Constants

View Source
const (
	DEBUG uint8 = 0  // DEBUG debug messages
	INFO        = 1  // INFO info messages
	WARN        = 2  // WARN warning messages
	ERROR       = 3  // ERROR error messages
	CRIT        = 4  // CRIT critical error messages
	AUX         = 99 // AUX unskipable messages (separators, headers, etc...)
)

Variables

View Source
var (
	// ErrLoggerIsNil is returned by Logger struct methods if struct is nil
	ErrLoggerIsNil = errors.New("Logger is nil or not created properly")

	// ErrUnexpectedLevel is returned by the MinLevel method if given level is unknown
	ErrUnexpectedLevel = errors.New("Unexpected level type")

	// ErrOutputNotSet is returned by the Reopen method if output file is not set
	ErrOutputNotSet = errors.New("Output file is not set")
)

Errors

View Source
var Colors = map[uint8]string{
	DEBUG: "{s-}",
	INFO:  "",
	WARN:  "{y}",
	ERROR: "{r}",
	CRIT:  "{m}",
}

Colors colors is map with fmtc color tags for every level

View Source
var Global = &Logger{
	PrefixWarn:  true,
	PrefixError: true,
	PrefixCrit:  true,

	minLevel: INFO,
	mu:       &sync.Mutex{},
}

Global is global logger struct

View Source
var PrefixMap = map[uint8]string{
	DEBUG: "[DEBUG]",
	INFO:  "[INFO]",
	WARN:  "[WARNING]",
	ERROR: "[ERROR]",
	CRIT:  "[CRITICAL]",
}

PrefixMap is map with messages prefixes

View Source
var TimeFormat = "2006/01/02 15:04:05.000"

TimeFormat contains format string for time in logs

Functions

func Aux

func Aux(f string, a ...interface{}) error

Aux write unskippable message (for separators/headers)

func Crit

func Crit(f string, a ...interface{}) error

Crit write critical message to global logger output

func Debug

func Debug(f string, a ...interface{}) error

Debug write debug message to global logger output

func EnableBufIO

func EnableBufIO(interval time.Duration)

EnableBufIO enable buffered I/O

func Error

func Error(f string, a ...interface{}) error

Error write error message to global logger output

func Flush

func Flush() error

Flush write buffered data to file

func Info

func Info(f string, a ...interface{}) error

Info write info message to global logger output

func MinLevel

func MinLevel(level interface{}) error

MinLevel defines minimal logging level

func Print

func Print(level uint8, f string, a ...interface{}) error

Print write message to global logger output

func Reopen

func Reopen() error

Reopen close file descriptor for global logger and open it again Useful for log rotation

func Set

func Set(file string, perms os.FileMode) error

Set change global logger output target

func Warn

func Warn(f string, a ...interface{}) error

Warn write warning message to global logger output

Types

type Logger

type Logger struct {
	PrefixDebug bool // Prefix for debug messages
	PrefixInfo  bool // Prefix for info messages
	PrefixWarn  bool // Prefix for warning messages
	PrefixError bool // Prefix for error messages
	PrefixCrit  bool // Prefix for critical error messages

	UseColors bool // Enable ANSI escape codes for colors in output
	// contains filtered or unexported fields
}

Logger is a basic logger struct

func New

func New(file string, perms os.FileMode) (*Logger, error)

New creates new logger struct

func (*Logger) Aux

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

Aux write unfiltered message (for separators/headers) to logger output

func (*Logger) Crit

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

Crit write critical message to logger output

func (*Logger) Debug

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

Debug write debug message to logger output

func (*Logger) EnableBufIO

func (l *Logger) EnableBufIO(interval time.Duration)

EnableBufIO enable buffered I/O support

func (*Logger) Error

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

Error write error message to logger output

func (*Logger) Flush

func (l *Logger) Flush() error

Flush write buffered data to file

func (*Logger) Info

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

Info write info message to logger output

func (*Logger) MinLevel

func (l *Logger) MinLevel(level interface{}) error

MinLevel defines minimal logging level

func (*Logger) Print

func (l *Logger) Print(level uint8, f string, a ...interface{}) error

Print write message to logger output

func (*Logger) Reopen

func (l *Logger) Reopen() error

Reopen close file descriptor and open again Useful for log rotation

func (*Logger) Set

func (l *Logger) Set(file string, perms os.FileMode) error

Set change logger output target

func (*Logger) Warn

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

Warn write warning message to logger output

type StdLogger

type StdLogger struct {
	Logger *Logger
}

StdLogger is logger wrapper compatible with stdlib logger

func (*StdLogger) Fatal

func (l *StdLogger) Fatal(v ...interface{})

Fatal is analog of Fatal from stdlib

func (*StdLogger) Fatalf

func (l *StdLogger) Fatalf(format string, v ...interface{})

Fatalf is analog of Fatalf from stdlib

func (*StdLogger) Fatalln

func (l *StdLogger) Fatalln(v ...interface{})

Fatalln is analog of Fatalln from stdlib

func (*StdLogger) Output

func (l *StdLogger) Output(calldepth int, s string) error

Output is analog of Output from stdlib

func (*StdLogger) Panic

func (l *StdLogger) Panic(v ...interface{})

Panic is analog of Panic from stdlib

func (*StdLogger) Panicf

func (l *StdLogger) Panicf(format string, v ...interface{})

Panicf is analog of Panicf from stdlib

func (*StdLogger) Panicln

func (l *StdLogger) Panicln(v ...interface{})

Panicln is analog of Panicln from stdlib

func (*StdLogger) Print

func (l *StdLogger) Print(v ...interface{})

Print is analog of Print from stdlib

func (*StdLogger) Printf

func (l *StdLogger) Printf(format string, v ...interface{})

Printf is analog of Printf from stdlib

func (*StdLogger) Println

func (l *StdLogger) Println(v ...interface{})

Println is analog of Println from stdlib

Jump to

Keyboard shortcuts

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