log

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 13 Imported by: 0

README

Light Logger

Go Report Card GoDoc

Light but faster(?) logger

Documentation

Index

Constants

View Source
const TimeFormat = "06-01-02MST15:04:05.000"

Time format used in loggers

Variables

View Source
var (
	// Log levels as string
	Levels = [6]string{"TRACE", "DEBUG", "VERBOSE", "INFO", "WARN", "ERROR"}

	FormatValue func(interface{}) string = SimpleFormat
)
View Source
var (
	// Global handles and root logger
	Root *Logger

	// Output raw strings
	Output func(Level, string)

	// Output any variables, like fmt.Println
	Println func(Level, ...interface{})

	// Output common logs in key=value format or string with args
	Log, Logf, Outputf func(Level, string, ...interface{})

	// Dump args details as json string, Dump with indent
	Json, Dump     func(Level, interface{})
	JsonIf, DumpIf (func(bool, Level, interface{}))

	// Global handles for different levels
	Trace, Debug, Verbose, Info, Warn, Error Handle

	// Global handleIfs for different levels
	TraceIf, DebugIf, VerboseIf, InfoIf, WarnIf, ErrorIf HandleIf
)
View Source
var LogFileFlag int = os.O_WRONLY | os.O_CREATE | os.O_APPEND

Default log file flag

Functions

func Assert added in v0.0.5

func Assert(check bool, msg string, args ...interface{})

Assert a condition, fatal otherwise

func Fatal

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

Fatal will exit the process after the log message is printed with stack info attached

func Format

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

Format log string with args as key=value format

func FormatLogfmtInt64

func FormatLogfmtInt64(n int64) string

FormatLogfmtInt64 formats n with thousand separators.

func FormatLogfmtUint64

func FormatLogfmtUint64(n uint64) string

FormatLogfmtUint64 formats n with thousand separators.

func GetStackInfo

func GetStackInfo(omitCalls int) string

Dump stack info with hiding logger internal calls

func Init

func Init(config *LogConfig)

Initialize global logger Read default log level from config or global env variable LOG_LEVEL

func Jsonify added in v0.0.9

func Jsonify(v interface{}) _Jsonify

Jsonify turns a value into Lazy Jsonify

func SetLevel

func SetLevel(target Level)

Set logger levels for root logger

func SimpleFormat added in v0.0.10

func SimpleFormat(value interface{}) string

func StackInfo added in v0.0.5

func StackInfo(omitCalls int, msg string, args ...interface{}) string

Append stack info to given message with args

func Stringify

func Stringify(value interface{}) string

func Verbosify added in v0.0.9

func Verbosify(v interface{}) _Verbosify

Verbosify turns a value into Lazy Jsonify with indentations

Types

type FileWriter

type FileWriter struct {
	LogConfig // Embed the config
	// contains filtered or unexported fields
}

FileWriter defines a file writer instance

func NewFileWriter

func NewFileWriter(c LogConfig) (w *FileWriter, err error)

Create a file writer instance with a log config

func (*FileWriter) Close added in v0.0.3

func (w *FileWriter) Close() (err error)

Close will try to close the file object

func (*FileWriter) Init

func (w *FileWriter) Init() (err error)

Initiate a file writer instance

func (*FileWriter) Write

func (w *FileWriter) Write(p []byte) (n int, err error)

Implement io.Writer interface for file writer

type Float added in v0.0.9

type Float float64

Lazy float

func (Float) String added in v0.0.9

func (f Float) String() string

type Handle

type Handle func(string, ...interface{})

Log handle interface

type HandleIf added in v0.0.9

type HandleIf func(bool, string, ...interface{})

type Hex

type Hex interface {
	Hex() string
}

type Level

type Level int

Log level

const (
	TRACE Level = iota
	DEBUG
	VERBOSE
	INFO
	WARN
	ERROR
)

Log levels

func ParseLevel

func ParseLevel(target string) Level

Parse level string

type LogConfig

type LogConfig struct {
	// Logger level to use
	Level    Level
	MaxSize  uint   // max bytes in MB
	MaxFiles uint   // max log files
	Path     string // main log file path
}

Logger config

func (*LogConfig) Writer

func (c *LogConfig) Writer() io.Writer

Provide logger writer instance, nil config will use os.Stderr instead

type Logger

type Logger struct {

	// Logger writer lock to avoid race conditions
	sync.Mutex

	// Logger handles
	Trace, Debug, Verbose, Info, Warn, Error Handle

	// Logger handleIfs
	TraceIf, DebugIf, VerboseIf, InfoIf, WarnIf, ErrorIf HandleIf
	JsonIf, DumpIf                                       func(bool, Level, interface{})
	// contains filtered or unexported fields
}

Logger defines the logger instance

func NewLogger

func NewLogger(config *LogConfig) *Logger

Create new logger instance with an optional config

func (*Logger) Assert added in v0.0.5

func (l *Logger) Assert(check bool, msg string, args ...interface{})

Assert a condition, fatal otherwise

func (*Logger) Dump

func (l *Logger) Dump(level Level, arg interface{})

Dump args as json with indent

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...interface{})

Exit the process after the log message with stack info attached

func (*Logger) Json

func (l *Logger) Json(level Level, arg interface{})

Dump args as json

func (*Logger) Level added in v0.0.8

func (l *Logger) Level() Level

Get the current logger level

func (*Logger) Log

func (l *Logger) Log(level Level, msg string, args ...interface{})

Output a log with custom level

func (*Logger) Logf

func (l *Logger) Logf(level Level, msg string, args ...interface{})

Output a log message using string formatter with args

func (*Logger) Output

func (l *Logger) Output(level Level, msg string)

Output a raw string with a custom level

func (*Logger) Outputf added in v0.0.6

func (l *Logger) Outputf(level Level, msg string, args ...interface{})

Output a raw string in format with a custom level, just like fmt.Printf with newline appended

func (*Logger) Println

func (l *Logger) Println(level Level, args ...interface{})

Output any args just like fmt.Println

func (*Logger) SetLevel

func (l *Logger) SetLevel(target Level)

Set a level for a logger instance

func (*Logger) Write

func (l *Logger) Write(bytes []byte, newline bool)

Write will write bytes with optional '\n' directly into output writer

Jump to

Keyboard shortcuts

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