log

package
v0.9.180 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 8 Imported by: 19

Documentation

Overview

Package log implements the log facilities for BCE. It supports log to stderr, stdout as well as log to file with rotating. It is safe to be called by multiple goroutines. By using the package level function to use the default logger:

log.SetLogHandler(log.STDOUT | log.FILE) // default is log to stdout
log.SetLogDir("/tmp")                    // default is /tmp
log.SetRotateType(log.ROTATE_DAY)        // default is log.HOUR
log.SetRotateSize(1 << 30)               // default is 1GB
log.SetLogLevel(log.INFO)                // default is log.DEBUG
log.Debug(1, 1.2, "a")
log.Debugln(1, 1.2, "a")
log.Debugf(1, 1.2, "a")

User can also create new logger without using the default logger:

customLogger := log.NewLogger()
customLogger.SetLogHandler(log.FILE)
customLogger.Debug(1, 1.2, "a")

The log format can also support custom setting by using the following interface:

log.SetLogFormat([]string{log.FMT_LEVEL, log.FMT_TIME, log.FMT_MSG})

Most of the cases just use the default format is enough:

[]string{FMT_LEVEL, FMT_LTIME, FMT_LOCATION, FMT_MSG}

Index

Constants

View Source
const (
	ROTATE_NONE RotateStrategy = iota
	ROTATE_DAY
	ROTATE_HOUR
	ROTATE_MINUTE
	ROTATE_SIZE

	DEFAULT_ROTATE_TYPE           = ROTATE_HOUR
	DEFAULT_ROTATE_SIZE     int64 = 1 << 30
	DEFAULT_LOG_DIR               = "/tmp"
	ROTATE_SIZE_FILE_PREFIX       = "rotating"
)

Constants for log rotating strategy when logging to file, default is by hour

View Source
const (
	FMT_LEVEL    = "level"
	FMT_LTIME    = "ltime"    // long time with microsecond
	FMT_TIME     = "time"     // just with second
	FMT_LOCATION = "location" // caller's location with file, line, function
	FMT_MSG      = "msg"
)

Constants of the log format components to support user custom specification

Variables

View Source
var (
	LOG_FMT_STR = map[string]string{
		FMT_LEVEL:    "[%s]",
		FMT_LTIME:    "2006-01-02 15:04:05.000000",
		FMT_TIME:     "2006-01-02 15:04:05",
		FMT_LOCATION: "%s:%d:%s:",
		FMT_MSG:      "%s",
	}
)

Functions

func Close added in v0.9.87

func Close()

func Debug

func Debug(msg ...interface{})

Export package-level functions for logging messages by using the default logger. It supports 3 kinds of interface which is similar to the standard package "fmt": with suffix of "ln", "f" or nothing.

func Debugf

func Debugf(format string, msg ...interface{})

func Debugln

func Debugln(msg ...interface{})

func Error

func Error(msg ...interface{})

func Errorf

func Errorf(format string, msg ...interface{})

func Errorln

func Errorln(msg ...interface{})

func Fatal

func Fatal(msg ...interface{})

func Fatalf

func Fatalf(format string, msg ...interface{})

func Fatalln

func Fatalln(msg ...interface{})

func Info

func Info(msg ...interface{})

func Infof

func Infof(format string, msg ...interface{})

func Infoln

func Infoln(msg ...interface{})

func NewLogger

func NewLogger() *logger

func Panic

func Panic(msg ...interface{})

func Panicf

func Panicf(format string, msg ...interface{})

func Panicln

func Panicln(msg ...interface{})

func SetLogDir

func SetLogDir(dir string) error

SetLogDir - set the logging directory if logging to file.

PARAMS:

  • dir: the logging directory

RETURNS:

  • error: check the directory and try to make it, otherwise return the error.

func SetLogFormat

func SetLogFormat(format []string)

SetLogFormat - set the log component of each record when logging it. The default log format is {FMT_LEVEL, FMT_LTIME, FMT_LOCATION, FMT_MSG}.

PARAMS:

  • format: the format component array.

func SetLogHandler

func SetLogHandler(h Handler)

SetLogHandler - set the handler of the logger

PARAMS:

  • Handler: the handler defined in this package, now just support STDOUT, STDERR, FILE

func SetLogLevel

func SetLogLevel(l Level)

SetLogLevel - set the level threshold of the logger, only level equal to or bigger than this value will be logged.

PARAMS:

  • Level: the level defined in this package, now support 6 levels.

func SetRotateSize

func SetRotateSize(size int64) error

SetRotateSize - set the rotating size if logging to file and set the strategy of size.

PARAMS:

  • size: the rotating size

RETURNS:

  • error: check the value and return any error if error occurs.

func SetRotateType

func SetRotateType(r RotateStrategy)

SetRotateType - set the rotating strategy if logging to file.

PARAMS:

  • RotateStrategy: the rotate strategy defined in this package, now support 5 strategy.

func Warn

func Warn(msg ...interface{})

func Warnf

func Warnf(format string, msg ...interface{})

func Warnln

func Warnln(msg ...interface{})

Types

type Handler

type Handler uint8
const (
	NONE   Handler = 0
	STDOUT Handler = 1
	STDERR Handler = 1 << 1
	FILE   Handler = 1 << 2
)

Constants for log handler flags, default is STDOUT

type Level

type Level uint8
const (
	DEBUG Level = iota
	INFO
	WARN
	ERROR
	FATAL
	PANIC
)

Constants for log levels, default is DEBUG

type RotateStrategy

type RotateStrategy uint8

Jump to

Keyboard shortcuts

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