log

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: MIT Imports: 11 Imported by: 1

README

AwesomeLog

AwesomeLog is an inplace replacement of the default log package with some extended features.

Mainly AwesomeLog provides functionality to define log levels as well as a PrettyPrint function to niceley and readable print out objects. It also shows the filename and line of code on logs with a level of DEBUG or VERBOSE

Quick start

Import the module with the alias log as shown in the example below.

package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.VERBOSE)
}
Functions

The following functions are provided from this package:

log.Print()
log.Println()
log.Printf()
log.PrettyPrint()

String functions:

log.Sprint()
log.Sprintln()
log.Sprintf()
log.SprettyPrint()

Functions without additional functionality. Panic and Fatal directly calls the original log functions:

log.Fatal()
log.Fatalf()
log.Fatalln()
log.Panic()
log.Panicf()
log.Panicln()

And AwesomeLog setting functions:

log.SetLogLevel()
log.SetLogLEvelByString()
log.SetDefaultLevel()
log.ShowColorsInLogs()
log.ShowCaller()
log.ShowColors()
log.ShowTimestamps()

log.DefaultLevelConfig()
log.SetLevelConfig()
Setup Functions:
log.SetLogLevel(logLevel)

log.SetLogLevel() defines to which level messages should be logged.

log.SetLogLevelByString(string)

log.SetLogLevelByString() same as SetLogLevel() but you can pass the log level as a string to the function (e.g. from a configuration file)

Default is log.VERBOSE / VERBOSE

See examples below.

log.SetDefaultLevel(logLevel)

log.SetDefaultLevel() defines the default log level if a print function is called without a log level as first argument.

Default: log.INFO

log.ShowColorsInLogs(bool)

log.ShowColorsInLogs() defines if the colored log-level labels should be shown in logs which are redirected to a file. If this is set to false and the outout is visible in the terminal AND is saved to a log file (e.g. docker logs) it will be shown with colors on the terminal output but without in the docker log.

Default: false

This function is not fully tested.

log.ShowCaller(bool)

log.ShowCaller() defines if the caller (function name) should be shown on all log levels.

Default: false (Shows the caller only on log level DEBUG and VERBOSE)

log.ShowTimestamps(bool)

log.ShowTimestamps() defines if timestamps should be shown.

Default: true

log.ShowColors(bool)

log.ShowColors() defines if labels should be colored.

Default: true

log.DefaultLevelConfig()

log.DefaultLevelConfig() returns the default config for all levels.

log.SetLevelConfig()

log.SetLevelConfig() set custom config for log levels. Example below.

Examples

package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.VERBOSE)

    log.Print("foobar")
}
package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.VERBOSE)

    log.Println("Foobar")
}
package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.VERBOSE)

    log.Printf("Foo%s", "Bar")
}
package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

type foo struct {
	Foo string
	Bar string
	Foobar struct {
		Meeps []string
	}
}

func main() {
    log.SetLogLevel(log.VERBOSE)

    foo := foo{
		Foo: "Test",
		Bar: "Test",
		Foobar: struct {
    	Meeps []string
		}{[]string{"Meep", "Meep2", "Meep2.1"}},
	}

    log.PrettyPrint(foo)
}

Output:

cmdline output
Examples with Loglevel

Now the interesting part:

package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.VERBOSE)

    log.Println(log.VERBOSE, "Foobar Verbose")
    log.Println(log.DEBUG, "Foobar Debug")
    log.Println(log.INFO, "Foobar Info")
    log.Println(log.WARN, "Foobar Warning")
}

Output:

cmdline output
Show only messages to a specific level:

The priority of the log levels is as following (highest to lowest):

NONE
WARN
INFO
DEBUG
VERBOSE

If you set the log-level to info:

package main
import (
    log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
    log.SetLogLevel(log.INFO)

    log.Println(log.VERBOSE, "Foobar Verbose")
    log.Println(log.DEBUG, "Foobar Debug")
    log.Println(log.INFO, "Foobar Info")
    log.Println(log.WARN, "Foobar Warning")
}

The output is reduced to the following messages:

cmdline output

Notes

PrettyPrint can only display fields which are exported!

Config Example
package main
import (
	log "github.com/chris-dot-exe/AwesomeLog"
)

func main() {
	cfg := log.DefaultLevelConfig()

	cfg.Debug = LevelConfig{
		ShowLineNumber:   false,
		ShowFunctionName: true,
		ShowFilePath:     false,
	}

	log.SetLevelConfig(cfg)
}

Default Config

Config{
		Verbose: LevelConfig{
			ShowLineNumber:   true,
			ShowFunctionName: true,
			ShowFilePath:     true,
		},
		Debug: LevelConfig{
			ShowLineNumber:   true,
			ShowFunctionName: true,
			ShowFilePath:     true,
		},
		Warn: LevelConfig{
			ShowLineNumber:   false,
			ShowFunctionName: false,
			ShowFilePath:     false,
		},
		Info: LevelConfig{
			ShowLineNumber:   false,
			ShowFunctionName: false,
			ShowFilePath:     false,
		},
	}

Documentation

Index

Constants

View Source
const ANSI_BLACK = "\u001B[30m"
View Source
const ANSI_BLACK_BACKGROUND = "\u001B[40m"
View Source
const ANSI_BLUE = "\u001B[34m"
View Source
const ANSI_BLUE_BACKGROUND = "\u001B[44m"
View Source
const ANSI_CYAN = "\u001B[36m"
View Source
const ANSI_CYAN_BACKGROUND = "\u001B[46m"
View Source
const ANSI_GREEN = "\u001B[32m"
View Source
const ANSI_GREEN_BACKGROUND = "\u001B[42m"
View Source
const ANSI_PURPLE = "\u001B[35m"
View Source
const ANSI_PURPLE_BACKGROUND = "\u001B[45m"
View Source
const ANSI_RED = "\u001B[31m"
View Source
const ANSI_RED_BACKGROUND = "\u001B[41m"
View Source
const ANSI_RESET = "\u001B[0m"
View Source
const ANSI_WHITE = "\u001B[37m"
View Source
const ANSI_WHITE_BACKGROUND = "\u001B[47m"
View Source
const ANSI_YELLOW = "\u001B[33m"
View Source
const ANSI_YELLOW_BACKGROUND = "\u001B[43m"

Variables

This section is empty.

Functions

func Fatal

func Fatal(params ...interface{})

func Fatalf

func Fatalf(format string, params ...interface{})

func Fatalln

func Fatalln(params ...interface{})

func Panic

func Panic(params ...interface{})

func Panicf

func Panicf(format string, params ...interface{})

func Panicln

func Panicln(params ...interface{})

func PrettyPrint

func PrettyPrint(params ...interface{})

func Print

func Print(params ...interface{})

func Printf

func Printf(paramsOriginal ...interface{})

func Println

func Println(params ...interface{})

func SetDefaultLevel

func SetDefaultLevel(lvl LogLevel)

func SetLevelConfig added in v0.10.0

func SetLevelConfig(cfg *Config)

func SetLogLevel

func SetLogLevel(lvl LogLevel)

func SetLogLevelByString

func SetLogLevelByString(lvlStr string)

func ShowCaller added in v0.9.6

func ShowCaller(show bool)

func ShowColors added in v0.10.0

func ShowColors(show bool)

func ShowColorsInLogs

func ShowColorsInLogs(show bool)

func ShowTimestamp added in v0.10.0

func ShowTimestamp(show bool)

func SprettyPrint added in v0.10.0

func SprettyPrint(params ...interface{}) string

func Sprint added in v0.10.0

func Sprint(params ...interface{}) string

func Sprintf added in v0.10.0

func Sprintf(paramsOriginal ...interface{}) string

func Sprintln added in v0.10.0

func Sprintln(params ...interface{}) string

Types

type Config added in v0.10.0

type Config struct {
	Verbose LevelConfig
	Debug   LevelConfig
	Warn    LevelConfig
	Info    LevelConfig
}

func DefaultLevelConfig added in v0.10.0

func DefaultLevelConfig() *Config

type LevelConfig added in v0.10.0

type LevelConfig struct {
	ShowLineNumber   bool
	ShowFunctionName bool
	ShowFilePath     bool
}

type LogLevel

type LogLevel uint
const (
	NONE    LogLevel = 0
	WARN    LogLevel = 1
	INFO    LogLevel = 2
	DEBUG   LogLevel = 10
	VERBOSE LogLevel = 20
)

func (*LogLevel) Color added in v0.10.0

func (t *LogLevel) Color() string

func (*LogLevel) String added in v0.10.0

func (t *LogLevel) String() string

Jump to

Keyboard shortcuts

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