logging

package
v0.0.0-...-b6fc690 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: BSD-3-Clause, BSD-2-Clause Imports: 20 Imported by: 0

Documentation

Overview

A simple logging module that mimics the behavior of Python's logging module.

All it does basically is wrap Go's logger with nice multi-level logging calls, and allows you to set the logging level of your app in runtime.

Logging is done just like calling fmt.Sprintf:

logging.Info("This object is %s and that is %s", obj, that)

example output:

[DBG 1670353253256778 instance.go:123] Setting config: projects [DBG 1670353253259897 subshell.go:95] Detected SHELL: zsh [DBG 1670353253259915 subshell.go:132] Using binary: /bin/zsh

Index

Constants

View Source
const (
	DEBUG    = 1
	INFO     = 2
	WARNING  = 4
	WARN     = 4
	ERROR    = 8
	NOTICE   = 16 //notice is like info but for really important stuff ;)
	CRITICAL = 32
	QUIET    = ERROR | NOTICE | CRITICAL               //setting for errors only
	NORMAL   = INFO | WARN | ERROR | NOTICE | CRITICAL // default setting - all besides debug
	ALL      = 255
	NOTHING  = 0
)
View Source
const FileNameSuffix = ".log"
View Source
const TailSize = 5000

TailSize specifies the number of logged bytes to keep for use with Tail.

Variables

View Source
var LevelsByName = map[string]int{
	"DEBUG":    DEBUG,
	"INFO":     INFO,
	"WARNING":  WARN,
	"WARN":     WARN,
	"ERROR":    ERROR,
	"NOTICE":   NOTICE,
	"CRITICAL": CRITICAL,
	"QUIET":    QUIET,
	"NORMAL":   NORMAL,
	"ALL":      ALL,
	"NOTHING":  NOTHING,
}
View Source
var LogPrefixRx = regexp.MustCompile(`^[a-zA-Z\-]+`)

Functions

func BridgeStdLog

func BridgeStdLog(level int)

BridgeStdLog bridges all messages written using the standard library's log.Print* and makes them output through this logger, at a given level.

func Close

func Close()

func Critical

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

Output a CRITICAL level message while showing a stack trace This should be called sparingly, as multilog.Critical() is preferred.

func Debug

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

Output debug logging messages

func Error

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

Output ERROR level messages This should be used sparingly, as multilog.Error() is preferred.

func ErrorNoStacktrace

func ErrorNoStacktrace(msg string, args ...interface{})

Same as Error() but without a stacktrace.

func FileName

func FileName() string

func FileNameFor

func FileNameFor(pid int) string

func FileNameForCmd

func FileNameForCmd(cmd string, pid int) string

func FileNamePrefix

func FileNamePrefix() string

func FilePath

func FilePath() string

func FilePathFor

func FilePathFor(filename string) string

func FilePathForCmd

func FilePathForCmd(cmd string, pid int) string

func Info

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

output INFO level messages

func Notice

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

Output NOTICE level messages

func Panic

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

Raise a PANIC while writing the stack trace to the log

func ReadTail

func ReadTail() string

ReadTail returns as a string the last TailSize bytes written by this logger.

func SetHandler

func SetHandler(h LoggingHandler)

Set the current handler of the library. We currently support one handler, but it might be nice to have more

func SetLevel

func SetLevel(l int)

Set the logging level.

Contrary to Python that specifies a minimal level, this logger is set with a bit mask of active levels.

e.g. for INFO and ERROR use:

SetLevel(logging.INFO | logging.ERROR)

For everything but debug and info use:

SetLevel(logging.ALL &^ (logging.INFO | logging.DEBUG))

func SetMinimalLevel

func SetMinimalLevel(l int)

Set a minimal level for loggin, setting all levels higher than this level as well.

the severity order is DEBUG, INFO, WARNING, ERROR, CRITICAL

func SetMinimalLevelByName

func SetMinimalLevelByName(l string) error

Set minimal level by string, useful for config files and command line arguments. Case insensitive.

Possible level names are DEBUG, INFO, WARNING, ERROR, NOTICE, CRITICAL

func SetOutput

func SetOutput(w io.Writer)

Set the output writer. for now it just wraps log.SetOutput()

func StartRotateLogTimer

func StartRotateLogTimer() func()

StartRotateLogTimer starts log rotation on a timer and returns a function that should be called to stop it.

func Warning

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

Output WARNING level messages

Types

type Formatter

type Formatter interface {
	Format(ctx *MessageContext, message string, args ...interface{}) string
}

A formatting interface- it is responsible of taking the arguments and composing a message

var DefaultFormatter Formatter = &SimpleFormatter{
	FormatString: "[%[1]s %[2]s %[3]s:%[4]d] %[5]s",
}

type Logger

type Logger func(msg string, args ...interface{})

Logger describes a logging function, like Debug, Error, Warning, etc.

type LoggingHandler

type LoggingHandler interface {
	SetFormatter(Formatter)
	SetVerbose(bool)
	Output() io.Writer
	Emit(ctx *MessageContext, message string, args ...interface{}) error
	Printf(msg string, args ...interface{})
	Close()
}

a pluggable logger interface

func CurrentHandler

func CurrentHandler() LoggingHandler

type MessageContext

type MessageContext struct {
	Level     string
	File      string
	Line      int
	TimeStamp time.Time
}

type SimpleFormatter

type SimpleFormatter struct {
	FormatString string
}

func (*SimpleFormatter) Format

func (f *SimpleFormatter) Format(ctx *MessageContext, message string, args ...interface{}) string

Jump to

Keyboard shortcuts

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