ulogger

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

ulogger

A custom logger that can be configured with different output colors. It can also stream logs to a remote URL. Take a look at the documentation. It can also deep log an element using spew.

Usage

import "github.com/Unaxiom/ulogger"

func main() {
    log := ulogger.New()
    log.SetLogLevel(ulogger.DebugLevel) // Possible values are ulogger.DebugLevel (debug - least), ulogger.InfoLevel (info), ulogger.WarningLevel (warning), ulogger.ErrorLevel (error), ulogger.FatalLevel (fatal - highest), in ascending order, with ulogger.InfoLevel being the default
    log.RemoteAvailable = true // Defines whether logs need to the be streamed to the remote URL
    log.ApplicationName = "Temp Debugger" // Sets the applicaton name
    log.OrganizationName = "New org" // Sets the organization name that this build is licensed to
    log.LineNumber = false // Bool value sets if line number, function name, and the file name need to be printed to the console. Default is true
    ulogger.RemoteURL = "https://example.com" // Sets the remote URL where the log message needs to be sent via a POST request. If this is not set, and if log.RemoteAvailability is true, then the default URL is ""
}

Possible log levels

# Debug level logs
log.Debug(args ...interface{})
log.Debugf(format string, args ...interface{})
log.Debugln(args ...interface{})

# Info level logs
log.Info(args ...interface{})
log.Infof(format string, args ...interface{})
log.Infoln(args ...interface{})

# Warning level logs
log.Warning(args ...interface{})
log.Warningf(format string, args ...interface{})
log.Warningln(args ...interface{})

# Error level logs
log.Error(args ...interface{})
log.Errorf(format string, args ...interface{})
log.Errorln(args ...interface{})

# Fatal level logs
log.Fatal(args ...interface{})
log.Fatalf(format string, args ...interface{})
log.Fatalln(args ...interface{})

Deep logging

log.DebugDump(args ...interface{})
log.InfoDump(args ...interface{})
log.WarningDump(args ...interface{})
log.ErrorDump(args ...interface{})
log.FatalDump(args ...interface{})

Modification of output colors

log.InfoColor
log.InfoTimeColor
log.InfoMessageTypeColor
log.DebugColor
log.DebugTimeColor
log.DebugMessageTypeColor
log.WarningColor
log.WarningTimeColor
log.WarningMessageTypeColor
log.ErrorColor
log.ErrorTimeColor
log.ErrorMessageTypeColor
log.FatalColor
log.FatalTimeColor
log.FatalMessageTypeColor

Each of these values could be assigned an color from the package github.com/fatih/color.

Disabling remote logging

Remote logging can be disabled by setting log.RemoteAvailable to false.

Print function name, line number, and the file name

This can be done by setting log.LineNumber = true. The default instance when creating a new logger has it set to true. If these values aren't required, they can be hidden by setting this value to false.

Send log messages to custom URL

If log.RemoteAvailable is set to true, then log messages are sent via a POST request to the URL. In case the URL needs to be changed, then it can be done so by updating ulogger.RemoteURL to the appropriate URL string. These messages would be sent via goroutines, so the execution will not be blocked.

Documentation

Index

Constants

View Source
const DebugLevel = "debug"

DebugLevel denotes a shorthand notation for setting log level to 'debug'

View Source
const ErrorLevel = "error"

ErrorLevel denotes a shorthand notation for setting log level to 'error'

View Source
const FatalLevel = "fatal"

FatalLevel denotes a shorthand notation for setting log level to 'fatal'

View Source
const InfoLevel = "info"

InfoLevel denotes a shorthand notation for setting log level to 'info'

View Source
const WarningLevel = "warning"

WarningLevel denotes a shorthand notation for setting log level to 'warning'

Variables

View Source
var RemoteURL = ""

RemoteURL is the location where the log message is sent to

Functions

This section is empty.

Types

type DisplayField

type DisplayField struct {
	Name  string
	Value interface{}
}

DisplayField stores the name and the value of the field that needs to be printed along with the log message

type Logger

type Logger struct {
	OrganizationName string `json:"organization_name"`
	ApplicationName  string `json:"application_name"`
	RemoteAvailable  bool   // Stores if the struct needs to be pushed to the remote URL
	LineNumber       bool   // Stores if the file, function, and the line number need to be printed with every log

	LogLevel string // Stores the log level; values are debug, info, warning, error and fatal

	// Customizable colors
	// Info colors
	InfoColor            *color.Color // Color of the info message
	InfoTimeColor        *color.Color // Color of the info timestamp
	InfoMessageTypeColor *color.Color // Color of the message type

	// Debug colors
	DebugColor            *color.Color // Color of the debug message
	DebugTimeColor        *color.Color // Color of the debug timestamp
	DebugMessageTypeColor *color.Color // Color of the message type

	// Warning colors
	WarningColor            *color.Color // Color of the warning message
	WarningTimeColor        *color.Color // Color of the warning timestamp
	WarningMessageTypeColor *color.Color // Color of the message type

	// Error colors
	ErrorColor            *color.Color // Color of the error message
	ErrorTimeColor        *color.Color // Color of the error timestamp
	ErrorMessageTypeColor *color.Color // Color of the message type

	// fatal colors
	FatalColor            *color.Color // Color of the fatal message
	FatalTimeColor        *color.Color // Color of the fatal timestamp
	FatalMessageTypeColor *color.Color // Color of the message type
	// contains filtered or unexported fields
}

Logger is the main logging object

func New

func New() *Logger

New returns a logger object

func (*Logger) Debug

func (log *Logger) Debug(args ...interface{})

Debug displays a debugging message useful in development environment

func (*Logger) DebugDump

func (log *Logger) DebugDump(args ...interface{})

DebugDump displays the dump of the variables passed using the go-spew library

func (*Logger) Debugf

func (log *Logger) Debugf(format string, args ...interface{})

Debugf displays a debugging message

func (*Logger) Debugln

func (log *Logger) Debugln(args ...interface{})

Debugln displays a debugging message

func (*Logger) Error

func (log *Logger) Error(args ...interface{})

Error displays an error message

func (*Logger) ErrorDump

func (log *Logger) ErrorDump(args ...interface{})

ErrorDump displays the dump of the variables passed using the go-spew library

func (*Logger) Errorf

func (log *Logger) Errorf(format string, args ...interface{})

Errorf displays an error message

func (*Logger) Errorln

func (log *Logger) Errorln(args ...interface{})

Errorln displays an error message

func (*Logger) Fatal

func (log *Logger) Fatal(args ...interface{})

Fatal displays a message and crashes the program

func (*Logger) FatalDump

func (log *Logger) FatalDump(args ...interface{})

FatalDump displays the dump of the variables passed using the go-spew library

func (*Logger) Fatalf

func (log *Logger) Fatalf(format string, args ...interface{})

Fatalf displays a message and crashes the program

func (*Logger) Fatalln

func (log *Logger) Fatalln(args ...interface{})

Fatalln displays a message and crashes the program

func (*Logger) Info

func (log *Logger) Info(args ...interface{})

Info displays a non-fatal log message

func (*Logger) InfoDump

func (log *Logger) InfoDump(args ...interface{})

InfoDump displays the dump of the variables passed using the go-spew library

func (*Logger) Infof

func (log *Logger) Infof(format string, args ...interface{})

Infof displays a non-fatal log message according to the format string

func (*Logger) Infoln

func (log *Logger) Infoln(args ...interface{})

Infoln displays a non-fatal log message

func (*Logger) SetLogLevel

func (log *Logger) SetLogLevel(level string)

SetLogLevel sets the log level of the logger

func (*Logger) Warning

func (log *Logger) Warning(args ...interface{})

Warning displays a warning message

func (*Logger) WarningDump

func (log *Logger) WarningDump(args ...interface{})

WarningDump displays the dump of the variables passed using the go-spew library

func (*Logger) Warningf

func (log *Logger) Warningf(format string, args ...interface{})

Warningf displays a warning message

func (*Logger) Warningln

func (log *Logger) Warningln(args ...interface{})

Warningln displays a warning message

func (*Logger) WithFields

func (log *Logger) WithFields(fields []DisplayField)

WithFields adds the passed fields and attaches them to the logging object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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