simplexlog

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2021 License: MIT Imports: 6 Imported by: 0

README

SIMPLEXLOG leveled logging library

Simplexlog simple wrapper for the go standard log package, that adds log level.

The standard log package is enough (imho) for many applications, but sometimes is usefull to have some log level. Simplexlog is a wrapper that adds some log level, the ability to use different io.Writer for different level.

The log levels are: TRACE, DEBUG, CRITICAL, ERROR, WARNING, NOTICE, INFO or ALL to switch on all levels

Simplexlog is concurrent safe.

Installation

The package is go gettable: go get -u github.com/vpxyz/simplexlog

Example


package main

import (
    "github.com/vpxyz/simplexlog"
    "log"
    "os"
)

func main() {
    // by default use os.Stderr for error, critical, fatal and panic, and os.Stdout for others
    l := simplexlog.New()

    fmt.Printf("available levels: %s\n", l.LevelNames())

    // the defaul log level is Info
    l.Trace("Trace log")

    l.Info("Info log")

    l.Notice("Notice log")

    l.Warning("Warning log")

    l.Debug("Debug log")

    l.Error("Error log")

    l.Critical("Critical log")

    l.SwitchTo(sl.Warning)

    l.Info("This is hidden")

    // if you need, you can pass around an standard log.Logger, bypassing the LogLevel setting
    l.CriticalLogger().Print("test")

}

More "complex" example

package main

import (
    sl "github.com/vpxyz/simplexlog"
    "log"
    "os"
    "bytes"
)

func main() {
    // Set different tag for any level
    // If you need, you can use a different io.Writer for each level witch different flags and prefix
    // If you want color labels, simply put colors escape sequence around label. For e.g. "\x1b[20;32m"+sl.LevelInfo+"\x1b[0m"
    l := sl.New(
        sl.SetDebug(sl.Config{Out: os.Stdout, Label: sl.LevelInfo + " ==> ", Flags: sl.DefaultLogFlags | log.Lshortfile}),
        sl.SetTrace(sl.Config{Out: os.Stdout, Label: sl.LevelTrace + " ==> ", Flags: sl.DefaultLogFlags | log.Lshortfile}),
        sl.SetInfo(sl.Config{Out: os.Stdout, Label: sl.LevelInfo + " =>", Flags: sl.DefaultLogFlags}),
        sl.SetNotice(sl.Config{Out: os.Stdout, Label: fmt.Sprintf("%-10s", "["+sl.LevelNotice+"]:"), Flags: sl.DefaultLogFlags}),
        sl.SetWarning(sl.Config{Out: os.Stdout, Label: sl.LevelWarning + ", 😒 ", Flags: sl.DefaultLogFlags}),
        sl.SetError(sl.Config{Out: os.Stderr, Label: sl.LevelError + " ", Flags: sl.DefaultLogFlags}),
        sl.SetCritical(sl.Config{Out: os.Stderr, Label: sl.LevelCritical + ",😡 ", Flags: sl.DefaultLogFlags | log.Lshortfile}),
    )

    // enable all log level
    l.SwitchTo(sl.All)

    l.Tracef("Trace log %s", "!!!")

    l.Info("Info log")

    l.Notice("Notice log")

    l.Warning("Warning log")

    l.Debug("Debug log")

    l.Error("Error log")

    l.Critical("Critical log")

    // change level
    l.SwitchTo(sl.Warning)

    l.Info("This is hidden")

    // if you need, you can pass around an standard log.Logger, bypassing the LogLevel setting
    l.CriticalLogger().Print("test")

    // change the log level using log level name (case insensitive)
    l.SwitchTo("error")

    l.Infof("Info log")

    l.Noticef("Notice log")

    l.Warningf("Warning log")

    l.Debugf("Debug log")

    l.Errorf("Error log")

    l.Criticalf("Critical log")

    // change the output of the Info level
    var buf bytes.Buffer
    l.SetOutput(sl.Info, &buf)

    // change label of Error level
    l.SetLabel(sl.Error, 😢)

    // change flags of Info level
    l.SetFlags(sl.Info, sl.DefaultLogFlags | log.Lshortfile)
}

Documentation

Overview

Package simplexlog simple wrapper for the standard log package, that adds log level. simplexlog is concurrent safe.

Index

Constants

View Source
const (

	// Critical log level
	Critical LogLevel = iota
	// Error log level
	Error
	// Warning log level
	Warning
	// Notice log level
	Notice
	// Info log level
	Info
	// Debug log level
	Debug
	// Trace log level
	Trace
	// All log level
	All

	// LevelCritical critical level
	LevelCritical = "CRITICAL"
	// LevelError error level
	LevelError = "ERROR"
	// LevelWarning warning level
	LevelWarning = "WARNING"
	// LevelNotice notice level
	LevelNotice = "NOTICE"
	// LevelInfo info level
	LevelInfo = "INFO"
	// LevelDebug debug level
	LevelDebug = "DEBUG"
	// LevelTrace trace level
	LevelTrace = "TRACE"
	// LevelAll all level
	LevelAll = "ALL"
)
View Source
const (
	// DefaultLogFlags default flagsfor log
	DefaultLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds
)

Variables

This section is empty.

Functions

func SetAllDefault

func SetAllDefault(c Config) func(*Logger)

SetAllDefault set the options of default logger used by all the log level

func SetCritical

func SetCritical(c Config) func(*Logger)

SetCritical set the options of critical logger

func SetDebug

func SetDebug(c Config) func(*Logger)

SetDebug set the options of debug logger

func SetDefault

func SetDefault(c Config) func(*Logger)

SetDefault set the options of default logger used by all log level except Error, Critical, Fatal and Panic

func SetError

func SetError(c Config) func(*Logger)

SetError set the options of error logger

func SetErrorDefault

func SetErrorDefault(c Config) func(*Logger)

SetErrorDefault set the options of default logger for error (used by Error, Critical level and by Fatal and Panic)

func SetInfo

func SetInfo(c Config) func(*Logger)

SetInfo set the option of info logger

func SetNotice

func SetNotice(c Config) func(*Logger)

SetNotice set the option of notice logger

func SetOutput

func SetOutput(level LogLevel, w io.Writer) func(*Logger)

SetOutput set only the output destination for a specified log level

func SetTrace

func SetTrace(c Config) func(*Logger)

SetTrace set the options of trace logger

func SetWarning

func SetWarning(o Config) func(*Logger)

SetWarning set the option of warning logger

Types

type Config

type Config struct {
	// Out is the output writer
	Out io.Writer
	// Label the prefix of a log line
	Label string
	// Flags are the same combination of flag of standard log package
	Flags int
}

Config log option

type LogLevel

type LogLevel uint

LogLevel level of log

type Logger

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

Logger simple log wrapper

func New

func New(configurations ...func(*Logger)) *Logger

New return a new logger. By default, all logs message are output to os.Stdout, except "error" and "critical" message that are logged to os.Stderr.

func (*Logger) Critical

func (l *Logger) Critical(a ...interface{})

Critical print to the Critical logger

func (*Logger) CriticalLogger

func (l *Logger) CriticalLogger() *log.Logger

CriticalLogger return the error logger

func (*Logger) Criticalf

func (l *Logger) Criticalf(format string, v ...interface{})

Criticalf print, accordind to format, to the Critical logger

func (*Logger) Debug

func (l *Logger) Debug(a ...interface{})

Debug print to the Debug logger

func (*Logger) DebugLogger

func (l *Logger) DebugLogger() *log.Logger

DebugLogger return the debug logger

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf print, accordind to format, to the Debug logger

func (*Logger) Error

func (l *Logger) Error(a ...interface{})

Error print to the Error logger

func (*Logger) ErrorLogger

func (l *Logger) ErrorLogger() *log.Logger

ErrorLogger Return the error logger

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf print, accordind to format, to the Error logger

func (*Logger) Fatal

func (l *Logger) Fatal(a ...interface{})

Fatal print fatal message to critical logger, followed by call to os.Exit(1)

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf print fatal message, accordind to format, to critical logger, followed by call to os.Exit(1)

func (*Logger) Info

func (l *Logger) Info(a ...interface{})

Info print to the Info logger

func (*Logger) InfoLogger

func (l *Logger) InfoLogger() *log.Logger

InfoLogger return the info logger

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof print, accordind to format, to the Info logger

func (*Logger) Level

func (l *Logger) Level() LogLevel

Level return the current log level

func (*Logger) LevelName

func (l *Logger) LevelName() string

LevelName return the current level name

func (*Logger) LevelNames

func (l *Logger) LevelNames() string

LevelNames return all the availables level name, from the most specific (little data) to the least specific (all data)

func (*Logger) Notice

func (l *Logger) Notice(a ...interface{})

Notice print to the Notice logger

func (*Logger) NoticeLogger

func (l *Logger) NoticeLogger() *log.Logger

NoticeLogger return the error logger

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{})

Noticef print, accordind to format, to the Notice logger

func (*Logger) Panic

func (l *Logger) Panic(a ...interface{})

Panic print panic message to the critical logger, followed by call to panic()

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf print panic message to the critical logger, followed by call to panic()

func (*Logger) SetFlags

func (l *Logger) SetFlags(level LogLevel, flag int)

SetFlags set the flags for a specified log level

func (*Logger) SetLabel

func (l *Logger) SetLabel(level LogLevel, label string)

SetLabel set the label for a specified log level

func (*Logger) SetOutput

func (l *Logger) SetOutput(level LogLevel, w io.Writer)

SetOutput set the output destination for a specified log level

func (*Logger) SwitchTo

func (l *Logger) SwitchTo(level interface{})

SwitchTo change the log level, level can be of type string (must match, case insensitive, level name like LevelTrace, LevelCritical etc), int or LogLevel to take effect

func (*Logger) Trace

func (l *Logger) Trace(a ...interface{})

Trace print to the Debug logger

func (*Logger) TraceLogger

func (l *Logger) TraceLogger() *log.Logger

TraceLogger return the trace logger

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

Tracef print, accordind to format, to the Debug logger

func (*Logger) Warning

func (l *Logger) Warning(a ...interface{})

Warning print to the Warning logger

func (*Logger) WarningLogger

func (l *Logger) WarningLogger() *log.Logger

WarningLogger return the warning logger

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf print, accordind to format, to the Warning logger

Jump to

Keyboard shortcuts

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