log

package module
v0.0.0-...-5b7cc4d Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Default time format with nanosecond precision
	TimeFormat = "2006-01-02 15:04:05.999999999"

	// Console default time format with millisecond accuracy
	ConsoleTimeFormat = "15:04:05.000"
)

Functions

func IsDebug

func IsDebug() bool

IsDebug determines whether the current environment is `DEBUG` through the `DEBUG` variable in the current environment variables.

func MustToConsoleAndFile

func MustToConsoleAndFile(filepath string, flag int) io.Writer

MustToConsoleAndFile returns an `io.Writer` that can both output the log to a control or terminal emulator and save the log to a local file, which will panic if there is an error opening the file.

The `flag` parameter is passed to the `os.OpenFile` function. If flag < 0, it will be assigned the value `os.O_RDWR|os.O_CREATE|os.O_TRUNC`.

func MustToFile

func MustToFile(filename string, flag int) io.Writer

MustToFile returns an `io.Writer` that saves the log to a local file and will panic if there is an error opening the file.

The `flag` parameter is passed to the `os.OpenFile` function. If flag < 0, it will be assigned the value `os.O_RDWR|os.O_CREATE|os.O_TRUNC`.

func ToConsole

func ToConsole() io.Writer

ToConsole returns an `io.Writer` that outputs the log to the console or terminal emulator or an `error`.

func ToConsoleAndFile

func ToConsoleAndFile(filepath string, flag int) (io.Writer, error)

ToConsoleAndFile returns an `io.Writer` that can both output the log to a control or terminal emulator and save the log to a local file, or an `error`.

The `flag` parameter is passed to the `os.OpenFile` function. If flag < 0, it will be assigned the value `os.O_RDWR|os.O_CREATE|os.O_TRUNC`.

func ToFile

func ToFile(filename string, flag int) (io.Writer, error)

ToFile returns an `io.Writer` that saves the log to a local file or an `error`.

The `flag` parameter is passed to the `os.OpenFile` function. If flag < 0, it will be assigned the value `os.O_RDWR|os.O_CREATE|os.O_TRUNC`.

Types

type Arg

type Arg struct {
	Key   string
	Value any
}

Arg records the parameters required in the log as key-value pairs, the key is of type `string`, and the value can be of any type.

func NewArg

func NewArg(key string, val any) Arg

type Level

type Level int8

Level defines the log level

const (
	TRACE Level = iota - 1
	DEBUG
	INFO
	WARNING
	ERROR
	FATAL
)

log level

func EnvLogLevel

func EnvLogLevel() Level

EnvLogLevel returns the logging level set in the LOG_LEVEL environment variable. If LOG_LEVEL is not set, it defaults to log.WARNING.

The mapping of LOG_LEVEL strings to log levels is:

-1, trace -> log.TRACE
0, debug -> log.DEBUG
1, info  -> log.INFO
2, warning, warn -> log.WARNING
3, error -> log.ERROR
4, fatal -> log.FATAL

LOG_LEVEL is case insensitive - "TRACE", "trace", and "Trace" all map to log.TRACE.

Other values default to log.WARNING.

type Logger

type Logger struct {
	L *zerolog.Logger
	// contains filtered or unexported fields
}

Logger records a `zerolog.Logger` pointer and uses this pointer to implement all logging methods

func NewLogger

func NewLogger(level Level, out io.Writer, skip ...int) *Logger

NewLogger returns a new `Logger` pointer.

func (*Logger) Debug

func (log *Logger) Debug(msg string, args ...Arg)

Debug logs a `DEBUG` message with some `Arg`s.

func (*Logger) Error

func (log *Logger) Error(err any, args ...Arg)

Error logs a `ERROR` message with some `Arg`s.

func (*Logger) Fatal

func (log *Logger) Fatal(err any, args ...Arg)

Fatal logs a `FATAL` message with some `Arg`s, and calls `os.Exit(1)` to exit the application.

func (*Logger) Info

func (log *Logger) Info(msg string, args ...Arg)

Info logs a `INFO` message with some `Arg`s.

func (Logger) Out

func (log Logger) Out() io.Writer

func (*Logger) SetLevel

func (log *Logger) SetLevel(level Level)

SetLevel will create a `zerolog.Logger` instance with a new `LEVEL` using the existing `out`(io.Writer)

func (*Logger) Warning

func (log *Logger) Warning(msg string, args ...Arg)

Warning logs a `WARNING` message with some `Arg`s.

Jump to

Keyboard shortcuts

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