logo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2021 License: MIT Imports: 8 Imported by: 0

README

logo is a logger for go.

  • Can log filename, line number, struct and function name at which the log method is called

  • Can suppress log messages with low log level

  • Logging without creating a logger struct first

  • Colored output of log level via ANSI-Code

  • String and JSON output

  • Can write to any io.Writer

Config Options

A new logger can be created via logo.New(flags) or logo.Logger{Property: Value}. All bool values have matching flags, which can be combined via the bitwise or flagA | flagB. The logger.Config(flags) function can reconfigure an existing logger.

The standard logger used by logo.Print() can be reconfigured by using logo.Config(flags) or replaced with logo.Standard = logo.Logo{}.

Property Type Description Default
Date bool Specifies whether the date should be displayed true
Time bool Specifies whether the time should be displayed true
Millis bool Specifies whether the milliseconds should be displayed true
Filename bool Specifies whether the filename and line should be displayed that was logged true
Funcname bool Specifies whether the struct and function should be displayed where the logger was called true
Json bool Specifies whether the output should be in Json format true
Output io.Writer The io.Writer the output of the logger shoud be written to os.Stdout
DateFormat string The date format of the output (in go's time format) YYYY-MM-DD (in go's time format "2006-01-02")
TimeFormat string The time format of the output (in go's time format) HH:MM:SS (in go's time format "15:04:05")
Level logo.Level Level is the log level of this logger. The logger only logs messages with a level greater or equal to the log level. By default AllLevels is selected, which logs everything. logo.AllLevels

Log Levels

The logger logs all log messages with a log level greater or equal its logger.Level. The value of the log levels increases towards the bottom.

Level Function Color
DebugLevel Debug() Green
InfoLevel Info() Blue
WarnLevel Warn() Yellow
ErrorLevel Error() Red
PrintLevel Print() Normal
AllLevels - -

Example

import "logo"

func main() {
    logo.Print("Logging made easy")
    logo.Info("Different colors")
    logo.Warn("Uh yellow now");
    // PRINT: 2020-12-27 18:21:36.474 example.go:4 examples.main: Logging made easy
    // INFO : 2020-12-27 18:21:36.476 example.go:5 examples.main: Different colors
    // WARN : 2020-12-27 18:21:36.478 example.go:6 examples.main: Uh yellow now
    
    logo.Config(logo.Time | logo.Millis)
    logo.Error("Some config options")
    // WARN : 18:21:36.478: Some config options
    
    logger := logo.New(logo.Filename | logo.Date)
    logger.Debug("A logger object")
    // WARN : 2020-12-27 example.go:6: A logger object
    
    logger = logo.Logger{
        Date:  true,
        Level: WarnLevel,
    }
    logger.Debug("Due to the log level this message is not displayed")
    logger.Warn("But this message does")
    // WARN : 2020-12-27: But this message does
}

Documentation

Overview

Package logo contains an extension of the log package from the standard library. Its main features are: - Logging filename, function and line number where the logger was called. - Multiple log Level's - Easy start (You don't need to create a logger instance) - Colored output of log Level's via ANSI-Code - String and JSON output - Can write to any io.Writer

Example:

logo.Print("Hallo")

Index

Constants

View Source
const (
	DateFlag = 1 << Flag(iota)
	TimeFlag
	MillisFlag
	FilenameFlag
	FuncnameFlag
	JsonFlag
)

Different Flag's to configure the equivalent property in the logger.

View Source
const (
	DebugLevel = Level(iota)
	InfoLevel
	WarnLevel
	ErrorLevel
	PrintLevel
	FatalLevel
	AllLevels = DebugLevel
)

The log Level's following are sorted in ascending order of priority. DebugLevel(AllLevels), InfoLevel, WarnLevel, ErrorLevel, FatalLevel and PrintLevel. So the highest Level is PrintLevel, the lowest is DebugLevel(PrintLevel). A log message is only logged if its Level is greater or equal to the Level of the Logger.

Variables

View Source
var (
	// Default is the Logger used by all functions that don't get called on a concrete logger instance.
	// For example, if you write logo.Info("Logo"), that uses this Default Logger.
	Default = New(DateFlag | TimeFlag | MillisFlag | FilenameFlag | FuncnameFlag)
)

Functions

func Config

func Config(flag Flag)

Config configures the Default Logger according to the given flags.

func Debug

func Debug(v ...interface{})

Debug logs a message with DebugLevel.

func Error

func Error(v ...interface{})

Error logs a message with ErrorLevel.

func Fatal

func Fatal(v ...interface{})

Fatal logs a message with FatalLevel and panics with the message in v.

func Info

func Info(v ...interface{})

Info logs a message with InfoLevel.

func Log

func Log(level Level, v ...interface{})

Log logs a message with the given Level.

func Print

func Print(v ...interface{})

Print logs a message with PrintLevel.

func Warn

func Warn(v ...interface{})

Warn logs a message with WarnLevel.

Types

type Flag

type Flag uint8

Flag with which the logger can be configured.

type Level

type Level uint8

Level decides which messages get logged in a Logger.

func (Level) ColorizedString

func (l Level) ColorizedString() string

ColorizedString returns the colorized name of the log Level.

func (Level) String

func (l Level) String() string

String returns the name of the log Level.

type Logger

type Logger struct {
	// Date specifies whether the date should be displayed.
	Date bool
	// Time specifies whether the time should be displayed.
	Time bool
	// Millis specifies whether the milliseconds should be displayed.
	Millis bool
	// Filename specifies whether the filename and line should be displayed where the
	// Logger was called.
	Filename bool
	// Funcname specifies whether the struct and function should be displayed where
	// the Logger was called.
	Funcname bool
	// Json specifies whether the output should be in Json format.
	Json bool
	// Output is the writer to which the log should be written.
	Output io.Writer
	// DateFormat is the date format of the output.
	DateFormat string
	// TimeFormat is the time format of the output.
	TimeFormat string
	// Level is the log level of this logger. The logger only logs messages with a
	// level greater or equal to the log level. By default, AllLevels is selected,
	// which logs everything.
	Level Level
}

Logger logs messages with various configuration options.

func New

func New(flags Flag) *Logger

New creates a new Logger with the given flags.

func NewDefault

func NewDefault() *Logger

NewDefault creates a new Logger with default flags. If you want to create a custom Logger, use New() instead or change the configuration options on the returned Logger.

func (*Logger) Config

func (l *Logger) Config(flags Flag)

Config configures the Logger according to the given flags.

func (*Logger) Debug

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

Debug logs a message with DebugLevel.

func (*Logger) Error

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

Error logs a message with ErrorLevel.

func (*Logger) Fatal

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

Fatal logs a message with FatalLevel and panics with the message in v.

func (*Logger) Info

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

Info logs a message with InfoLevel.

func (*Logger) Log

func (l *Logger) Log(level Level, v ...interface{})

Log logs a message with the given Level.

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print logs a message with PrintLevel.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn logs a message with WarnLevel.

Jump to

Keyboard shortcuts

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