slog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: BSD-3-Clause Imports: 9 Imported by: 7

README

slog

Go Reference

The slog package provides a simple, basic, fast leveled logger.

It supports logging to stderr and stdout, various levels, caller location information (file, line) and backtraces.

It tries to minimize memory allocations by using an internal lock-less pool of buffers.

It does not use any locking so log messages might get "mixed" between the threads.

Note that enabling file location or backtracing will cause a significant slowdown and lot of memory allocations.

Documentation

Index

Constants

View Source
const (
	BufPoolSz = 128   // maximum buffer in pool, must be 2^k
	BufSz     = 1024  // typical buf sz
	MaxBufSz  = 65534 // max allowed (if more reset)
)

keep a pool of bytes.Buffer(s) for fast re-use

View Source
const Version = "0.1.1"

Variables

View Source
var BuildTags []string

Functions

func BUG

func BUG(f string, args ...interface{})

func CRIT

func CRIT(f string, args ...interface{})

func CRITon

func CRITon() bool

func DBG

func DBG(f string, args ...interface{})

func DBGon

func DBGon() bool

func DefaultLogInit

func DefaultLogInit(lev LogLevel, opt LogOptions, logger LogOutput)

func DefaultLogSetLevel

func DefaultLogSetLevel(lev LogLevel)

func DefaultLogSetOptions

func DefaultLogSetOptions(opt LogOptions)

func DefaultLogSetOutput

func DefaultLogSetOutput(out LogOutput)

func ERR

func ERR(f string, args ...interface{})

func ERRon

func ERRon() bool

func INFO

func INFO(f string, args ...interface{})

func INFOon

func INFOon() bool

func Init

func Init(l *Log, lev LogLevel, opt LogOptions, logger LogOutput)

Init initialises a log with a given maximum log level, options and output type.

func L

func L(l LogLevel) bool

func LevelName added in v0.0.2

func LevelName(l LogLevel) string

LevelName returns the log level name as string. On error it returns the empty string.

func LogOptString added in v0.0.2

func LogOptString(o LogOptions) (string, bool)

LogOptString returns the log options as string (seprated by "|"). It returns the string representation of the options and a boolean specifying if all the options are known (true) or if an error was encountered (false)

func NOTICE

func NOTICE(f string, args ...interface{})

func NOTICEon

func NOTICEon() bool

func PANIC

func PANIC(f string, args ...interface{})

func SetLevel

func SetLevel(l *Log, lev LogLevel)

SetLevel changes the log level.

func SetOptions

func SetOptions(l *Log, opt LogOptions)

SetOptions changes the log options.

func SetOutput

func SetOutput(l *Log, out LogOutput)

SetLevel changes the output type.

func WARN

func WARN(f string, args ...interface{})

func WARNon

func WARNon() bool

Types

type Log

type Log uint64

Log is a simple logger, keeping all its configuration inside an uint64. Format: LogOutput | LogOptions | LogLevel (3 bytes used, 5 free)

func New

func New(lev LogLevel, opt LogOptions, logger LogOutput) Log

New returns a new log, intialised with the give options

func (Log) BUG

func (l Log) BUG(f string, args ...interface{})

BUG logs a message at the BUG level with a BUG prefix.

func (Log) BUGon

func (l Log) BUGon() bool

BUGon returns true if logging at LBUG level is enabled.

func (Log) CRITon

func (l Log) CRITon() bool

CRITon returns true if logging at LCRIT level is enabled.

func (Log) DBG

func (l Log) DBG(fmt string, args ...interface{})

DBG logs a message at the LDBG level with a DBG prefix (shorthand for Log(LDBG, "DBG: ", ...))

func (Log) DBGon

func (l Log) DBGon() bool

DBGon returns true if logging at LDBG level is enabled.

func (Log) ERR

func (l Log) ERR(f string, args ...interface{})

ERR logs a message at the LERR level with a ERROR prefix (shorthand for Log(LERR, "ERROR: ", ...))

func (Log) ERRon

func (l Log) ERRon() bool

ERRon returns true if logging at LERR level is enabled.

func (Log) GetLevel

func (l Log) GetLevel() LogLevel

GetLevel returns the current log level.

func (Log) GetLogger

func (l Log) GetLogger() LogOutput

GetLogger returns the current log output type.

func (Log) GetOpt

func (l Log) GetOpt() LogOptions

GetOpts returns the current log options.

func (Log) INFO

func (l Log) INFO(f string, args ...interface{})

INFO logs a message at the LINFO level with an INFO prefix (shorthand for Log(LINFO, "INFO: ", ...))

func (Log) INFOon

func (l Log) INFOon() bool

INFOon returns true if logging at LINFO level is enabled.

func (Log) L

func (l Log) L(lev LogLevel) bool

L returns true if the specified level is enabled for logging. Can be used to quickly check if logging is enabled, before calling a logging function (thus avoiding unneeded parameter evaluation).

func (Log) LLog

func (l Log) LLog(lev LogLevel, callersSkip int, prefix string,
	f string, args ...interface{})

LLog logs a message with a given prefix at a given level. It is supposed to be used mostly internally. The callersSkip parameter is used to find the caller for which to print the file/line information or where the backtrace should start.

func (Log) Log

func (l Log) Log(lev LogLevel, f string, args ...interface{})

Log() logs directly without adding any log level prefix.

func (Log) LogMux

func (l Log) LogMux(w io.Writer, wcond bool,
	lev LogLevel, f string, args ...interface{})

LogMux() will write the message both to the passed io.Writer and to the log. The message is written to the io.Writer independent of the log level and only if wcond is true. The message in the log will have added prefixes depending on the log options.

func (Log) NOTICEon

func (l Log) NOTICEon() bool

NOTICEon returns true if logging at LNOTICE level is enabled.

func (Log) PANIC

func (l Log) PANIC(f string, args ...interface{})

PANIC logs a message at the BUG level and then calls panic()

func (Log) WARN

func (l Log) WARN(f string, args ...interface{})

WARN logs a message at the LWARN level with a WARN prefix (shorthand for Log(LWARN, "WARN: ", ...))

func (Log) WARNon

func (l Log) WARNon() bool

WARNon returns true if logging at LWARN level is enabled.

type LogLevel

type LogLevel int8

LogLevel is the type used for the logging level.

const (
	LMIN    LogLevel = -3
	LBUG    LogLevel = -3
	LCRIT   LogLevel = -2
	LERR    LogLevel = -1
	LWARN   LogLevel = 0
	LNOTICE LogLevel = 1
	LINFO   LogLevel = 2
	LDBG    LogLevel = 3
	LMAX    LogLevel = 3
)

log levels

type LogOptions

type LogOptions uint8

LogOptions is the type used for the log options.

const (
	LOptNone    LogOptions = 0
	LlocInfoS   LogOptions = 1 << 0 // short filenames
	LlocInfoL   LogOptions = 1 << 1 // long version (complete filenames)
	LtimeStamp  LogOptions = 1 << 2
	LbackTraceS LogOptions = 1 << 3 // short
	LbackTraceL LogOptions = 1 << 4 // long
)

log options

type LogOutput

type LogOutput uint8

LogOutput is the type used for the log output type.

const (
	LDefaultOut LogOutput = iota
	LStdOut
	LStdErr
	LDisabledOut LogOutput = 9
)

log output types

Jump to

Keyboard shortcuts

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