log

package module
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2020 License: MIT Imports: 10 Imported by: 1

README

AwesomeLog

AwesomeLog is a 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.

To get log output you MUST define what log levels should be printed out. IMPORTANT The default log level is NONE.

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()

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()
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.NONE / NONE

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.

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!

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

View Source
var COLORS_IN_LOGS = false
View Source
var DEFAULT_LEVEL = INFO
View Source
var LOG_LEVEL = NONE

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 SetLogLevel

func SetLogLevel(lvl LogLevel)

func SetLogLevelByString

func SetLogLevelByString(lvlStr string)

func ShowColorsInLogs

func ShowColorsInLogs(ok bool)

Types

type LogLevel

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

Jump to

Keyboard shortcuts

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