quicklog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: LGPL-3.0 Imports: 7 Imported by: 0

README

Quick Log

Quick Log is a small logging that package supports logging levels, log archiving, and custom loggers.

Installing

After initializing your go module, run:

go get -u github.com/I-Am-Dench/quick-log

Log Archiving

This package comes with an automatic archiving of log files built-in. When the logger is created, it will generate a file called current.log inside of the configured log directory (./log/ by default). Whenever the logger is closed, the contents of current.log are compressed through gzip and written to the log directory as yyyy-mm-dd_#.log.gz where # is the number of logs archived for that day. The # is added so that if the logger is closed multiple times in a day, each archive does not override one another.

Whenever any logger write function is called, i.e. Logger.Infof(), Logger.Errorf(), etc., if the current day is not equal to when current.log was created, the log will be archived.

Log archiving can be disabled by calling Logger.SetArchiveLogs(false)

Examples

Global Logger

Source: main.go

package main

import log "github.com/I-Am-Dench/quick-log"

func main() {
    log.Debugf("Debug log")
    log.Tracef("Trace log")
    log.Infof("Info log")
    log.Warnf("Warn log")
    log.Errorf("Error log")
    // log.Fatalf("Fatal log")

    log.Close()
}

Output

[D; 1970-01-01; 00:00:00] Debug log
[T; 1970-01-01; 00:00:00] [main.go:7] Trace log
[I; 1970-01-01; 00:00:00] Info log
[W; 1970-01-01; 00:00:00] Warn log
[E; 1970-01-01; 00:00:00] Error log

Logger.Fatalf will call panic() after logger has finished handling the log message.

Custom Logger

Source: main.go

package main

import log "github.com/I-Am-Dench/quick-log"

func main() {
    logger1 := log.New("./dir1/logs/")
    defer logger1.Close()

    logger2 := log.New("./dir2/logs/")
    defer logger2.Close()
    logger2.SetLevel(log.LEVEL_INFO)

    logger1.Debugf("Logger1 - Debug")
    logger1.Infof("Logger1 - Info")

    logger2.Debugf("Logger2 - Debug") // Ignored since log.LEVEL_DEBUG < log.LEVEL_INFO
    logger2.Infof("Logger2 - Info")
}

Output

[D; 1970-01-01; 00:00:00] Logger1 - Debug
[I; 1970-01-01; 00:00:00] Logger1 - Info
[I; 1970-01-01; 00:00:00] Logger2 - Info

Loggers, by default, have a log level of log.LEVEL_DEBUG.

Log Levels

The priority of log levels goes as follows:

log.LEVEL_DEBUG < log.LEVEL_TRACE < log.LEVEL_INFO < log.LEVEL_WARN < log.LEVEL_ERROR < log.LEVEL_FATAL

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LOG_PREFIX = [NUM_LOG_LEVELS]string{"D", "T", "I", "W", "E", "F"}

Functions

func Close

func Close()

func Debugf

func Debugf(format string, a ...any)

func DoesLogArchives

func DoesLogArchives() bool

func Errorf

func Errorf(format string, a ...any)

func Fatalf

func Fatalf(format string, a ...any)

func Infof

func Infof(format string, a ...any)

func SetArchiveLogs

func SetArchiveLogs(archiveLogs bool)

func SetDir

func SetDir(logDir string)

func SetLevel

func SetLevel(level LogLevel)

func Tracef

func Tracef(format string, a ...any)

func Warnf

func Warnf(format string, a ...any)

Types

type Config

type Config struct {
	// Lowest log level that can be handled
	Level LogLevel

	// The number of callstack frames to skip. This would be the argument passed to runtime.Caller(skip int)
	// By default, this is 1 such that the Trace will log where the statement was executed
	TraceSkip int

	// Enables/disables log archiving
	ArchiveLogs bool

	// If File is nil, it will default to os.Stdout
	File *os.File
}

type LogLevel

type LogLevel int
const (
	LEVEL_DEBUG LogLevel = iota
	LEVEL_TRACE
	LEVEL_INFO
	LEVEL_WARN
	LEVEL_ERROR
	LEVEL_FATAL
	NUM_LOG_LEVELS
)

func GetLevel

func GetLevel() LogLevel

type LogType

type LogType string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func New

func New(logDir string, config ...Config) *Logger

func (*Logger) ArchiveCurrentLog

func (logger *Logger) ArchiveCurrentLog() error

func (*Logger) Close

func (logger *Logger) Close()

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, a ...any)

func (*Logger) DoesLogArchives

func (logger *Logger) DoesLogArchives() bool

func (*Logger) Errorf

func (logger *Logger) Errorf(format string, a ...any)

func (*Logger) Fatalf

func (logger *Logger) Fatalf(format string, a ...any)

func (*Logger) GetLevel

func (logger *Logger) GetLevel() LogLevel

func (*Logger) Infof

func (logger *Logger) Infof(format string, a ...any)

func (*Logger) Logf

func (logger *Logger) Logf(writer io.Writer, level LogLevel, format string, a ...any)

func (*Logger) SetArchiveLogs

func (logger *Logger) SetArchiveLogs(archiveLogs bool)

func (*Logger) SetDir

func (logger *Logger) SetDir(logDir string)

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level LogLevel)

func (*Logger) Tracef

func (logger *Logger) Tracef(format string, a ...any)

func (*Logger) Warnf

func (logger *Logger) Warnf(format string, a ...any)

func (*Logger) Write

func (logger *Logger) Write(writer io.Writer, message string, level LogLevel)

Jump to

Keyboard shortcuts

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