abc

package module
v0.0.0-...-d3859f5 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 11 Imported by: 0

README

Go Report Card GoDoc

abc

A Golang logging library.

Installation

go get -u github.com/TimSatke/abc

Example

abc supports several types of loggers. For better examples and more loggers, see examples/* or the wiki.

SimpleLogger
logger := abc.NewSimpleLogger()
logger.SetLevel(abc.LevelDebug)
logger.Debugf("Hello %v!", "World") // 2018-11-24 20:10:55.300 [DEBG] - Hello World
NamedLogger
logger := abc.NewNamedLogger("MyLogger")
logger.SetLevel(abc.LevelDebug)
logger.Debugf("Hello %v!", "World") // 2018-11-24 20:10:55.300 <MyLogger> [DEBG] - Hello World

Benchmarks

$ go test -count 5 -bench . -benchmem
goos: windows
goarch: amd64
pkg: github.com/TimSatke/abc
BenchmarkColoredLogger_SimpleLogger_Printf-8             1000000              1064 ns/op             320 B/op          9 allocs/op
BenchmarkColoredLogger_SimpleLogger_Printf-8             1000000              1055 ns/op             320 B/op          9 allocs/op
BenchmarkColoredLogger_SimpleLogger_Printf-8             1000000              1058 ns/op             320 B/op          9 allocs/op
BenchmarkColoredLogger_SimpleLogger_Printf-8             1000000              1060 ns/op             320 B/op          9 allocs/op
BenchmarkColoredLogger_SimpleLogger_Printf-8             1000000              1051 ns/op             320 B/op          9 allocs/op
BenchmarkCustomPatternLogger_Printf-8                    2000000               785 ns/op             808 B/op         21 allocs/op
BenchmarkCustomPatternLogger_Printf-8                    2000000               794 ns/op             808 B/op         21 allocs/op
BenchmarkCustomPatternLogger_Printf-8                    2000000               790 ns/op             808 B/op         21 allocs/op
BenchmarkCustomPatternLogger_Printf-8                    2000000               777 ns/op             808 B/op         21 allocs/op
BenchmarkCustomPatternLogger_Printf-8                    2000000               763 ns/op             808 B/op         21 allocs/op
BenchmarkCustomPatternLogger_Printf_Stack_Ops-8          1000000              2590 ns/op            1897 B/op         43 allocs/op
BenchmarkCustomPatternLogger_Printf_Stack_Ops-8           500000              2575 ns/op            1897 B/op         43 allocs/op
BenchmarkCustomPatternLogger_Printf_Stack_Ops-8           500000              2424 ns/op            1897 B/op         43 allocs/op
BenchmarkCustomPatternLogger_Printf_Stack_Ops-8           500000              2479 ns/op            1897 B/op         43 allocs/op
BenchmarkCustomPatternLogger_Printf_Stack_Ops-8           500000              2657 ns/op            1897 B/op         43 allocs/op
BenchmarkNamedLogger_Printf-8                           10000000               238 ns/op             368 B/op          9 allocs/op
BenchmarkNamedLogger_Printf-8                           10000000               236 ns/op             368 B/op          9 allocs/op
BenchmarkNamedLogger_Printf-8                            5000000               243 ns/op             368 B/op          9 allocs/op
BenchmarkNamedLogger_Printf-8                            5000000               238 ns/op             368 B/op          9 allocs/op
BenchmarkNamedLogger_Printf-8                           10000000               250 ns/op             368 B/op          9 allocs/op
BenchmarkSimpleLogger_Printf-8                           5000000               250 ns/op             304 B/op          8 allocs/op
BenchmarkSimpleLogger_Printf-8                           5000000               249 ns/op             304 B/op          8 allocs/op
BenchmarkSimpleLogger_Printf-8                           5000000               244 ns/op             304 B/op          8 allocs/op
BenchmarkSimpleLogger_Printf-8                           5000000               254 ns/op             304 B/op          8 allocs/op
BenchmarkSimpleLogger_Printf-8                           5000000               274 ns/op             304 B/op          8 allocs/op
BenchmarkStdLogger-8                                     3000000               470 ns/op              96 B/op          2 allocs/op
BenchmarkStdLogger-8                                     3000000               469 ns/op              96 B/op          2 allocs/op
BenchmarkStdLogger-8                                     3000000               475 ns/op              96 B/op          2 allocs/op
BenchmarkStdLogger-8                                     3000000               471 ns/op              96 B/op          2 allocs/op
BenchmarkStdLogger-8                                     3000000               470 ns/op              96 B/op          2 allocs/op
BenchmarkStdLogger_Stackops-8                            1000000              1199 ns/op             176 B/op          2 allocs/op
BenchmarkStdLogger_Stackops-8                            1000000              1183 ns/op             176 B/op          2 allocs/op
BenchmarkStdLogger_Stackops-8                            1000000              1184 ns/op             176 B/op          2 allocs/op
BenchmarkStdLogger_Stackops-8                            1000000              1189 ns/op             176 B/op          2 allocs/op
BenchmarkStdLogger_Stackops-8                            1000000              1213 ns/op             176 B/op          2 allocs/op
PASS
ok      github.com/TimSatke/abc 59.162s

Documentation

Index

Constants

View Source
const (
	// TimeLayoutCustomPatternLogger is the time layout that the simple logger
	// uses for its messages if no formatting pattern is given.
	TimeLayoutCustomPatternLogger = "2006-01-02 15:04:05.000"
	// CustomPatternLoggerDefaultPattern is the fallback pattern that is used
	// if the pattern compilation or template execution fails.
	CustomPatternLoggerDefaultPattern = "{{.Timestamp}} [{{.Level}}] - {{.Message}}\n"
)
View Source
const (
	// TimeLayoutNamedLogger is the time layout that the named logger
	// uses for its messages.
	TimeLayoutNamedLogger = "2006-01-02 15:04:05.000"
)
View Source
const (
	// TimeLayoutSimpleLogger is the time layout that the simple logger
	// uses for its messages.
	TimeLayoutSimpleLogger = "2006-01-02 15:04:05.000"
)

Variables

View Source
var (
	ColorGray   = color("\u001b[30;1m")
	ColorGreen  = color("\u001b[32;1m")
	ColorNone   = ColorReset
	ColorRed    = color("\u001b[31m")
	ColorReset  = color("\u001b[0m")
	ColorYellow = color("\u001b[33m")
)

Available colors (16 bit ANSI color codes)

Functions

func Debug

func Debug(v ...interface{})

Debug prints the given values with log level DEBG.

func Debugf

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

Debugf formats and prints the given values with log level DEBG.

func Error

func Error(v ...interface{})

Error prints the given values with log level ERR.

func Errorf

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

Errorf formats and prints the given values with log level ERR.

func Fatal

func Fatal(v ...interface{})

Fatal prints the given values with log level FATAL.

func Fatalf

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

Fatalf formats and prints the given values with log level FATAL.

func Info

func Info(v ...interface{})

Info prints the given values with log level INFO.

func Infof

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

Infof formats and prints the given values with log level INFO.

func IsLevelEnabled

func IsLevelEnabled(lvl LogLevel) bool

IsLevelEnabled returns true if and only if the root logger would print messages with the given log level. False otherwise.

func Print

func Print(lvl LogLevel, v ...interface{})

Print delegates to the root logger, if and only if the given level is enabled by the root logger.

func Printf

func Printf(lvl LogLevel, format string, v ...interface{})

Printf delegates to the root logger, if and only if the given level is enabled by the root logger.

func SetLevel

func SetLevel(lvl LogLevel)

SetLevel sets a new log level for the root logger.

func SetRoot

func SetRoot(lg Logger)

SetRoot changes the globally used root logger. This function is safe for concurrent use.

func Verbose

func Verbose(v ...interface{})

Verbose prints the given values with log level DEBG, but the root logger must have verbose log levels enabled to show any output.

func Verbosef

func Verbosef(format string, v ...interface{})

Verbosef formats and prints the given values with log level DEBG, but the root logger must have verbose log levels enabled to show any output.

func Warn

func Warn(v ...interface{})

Warn prints the given values with log level WARN.

func Warnf

func Warnf(format string, v ...interface{})

Warnf formats and prints the given values with log level WARN.

Types

type ColoredLogger

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

ColoredLogger is a wrapper for any WriterLogger. Depending on the level that should be printed, this wrapper will prepend an ANSI-color code to the wrapped loggers output writer and will then call the respective output method. Please notice that this is not add a decorator for a given logger, but a wrapper, which must be used for colors to show up.

The color codes are written to the wrapped loggers output writer. An output call will trigger three writes on the wrapped loggers output writer.

1. color code

2. output from wrapped logger

3. color code reset

The second call will not originate from the wrapper, as, after the color code was written, Print (or respectively Printf) will be called on the wrapped logger.

func (*ColoredLogger) Debug

func (s *ColoredLogger) Debug(v ...interface{})

Debug delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Debugf

func (s *ColoredLogger) Debugf(format string, v ...interface{})

Debugf delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Error

func (s *ColoredLogger) Error(v ...interface{})

Error delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Errorf

func (s *ColoredLogger) Errorf(format string, v ...interface{})

Errorf delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Fatal

func (s *ColoredLogger) Fatal(v ...interface{})

Fatal delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

THIS WRAPPER DOES NOT TERMINATE THE APPLICATION AFTER A FATAL CALL.

func (*ColoredLogger) Fatalf

func (s *ColoredLogger) Fatalf(format string, v ...interface{})

Fatalf delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

THIS WRAPPER DOES NOT TERMINATE THE APPLICATION AFTER A FATAL CALL.

func (*ColoredLogger) Info

func (s *ColoredLogger) Info(v ...interface{})

Info delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Infof

func (s *ColoredLogger) Infof(format string, v ...interface{})

Infof delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) IsLevelEnabled

func (s *ColoredLogger) IsLevelEnabled(lvl LogLevel) bool

IsLevelEnabled delegates to the wrapped loggers IsLevelEnabled method.

func (*ColoredLogger) Out

func (s *ColoredLogger) Out() io.Writer

Out returns the writer of the wrapped logger.

func (*ColoredLogger) Print

func (s *ColoredLogger) Print(lvl LogLevel, v ...interface{})

Print delegates the values with the given log level to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Printf

func (s *ColoredLogger) Printf(lvl LogLevel, format string, v ...interface{})

Printf delegates the format string and values with the given log level to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) SetLevel

func (s *ColoredLogger) SetLevel(lvl LogLevel)

SetLevel delegates the given log level to the wrapped logger.

func (*ColoredLogger) SetLevelString

func (s *ColoredLogger) SetLevelString(level string)

func (*ColoredLogger) SetOut

func (s *ColoredLogger) SetOut(out io.Writer)

SetOut sets a new writer for the wrapped logger.

func (*ColoredLogger) Verbose

func (s *ColoredLogger) Verbose(v ...interface{})

Verbose delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Verbosef

func (s *ColoredLogger) Verbosef(format string, v ...interface{})

Verbosef delegates the given values and format string to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Warn

func (s *ColoredLogger) Warn(v ...interface{})

Warn delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

func (*ColoredLogger) Warnf

func (s *ColoredLogger) Warnf(format string, v ...interface{})

Warnf delegates the given values to the wrapped logger while writing ansi color codes to the wrapped loggers output writer.

type CustomPatternLogger

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

CustomPatternLogger is a logger that supports a custom log pattern. The pattern is a go template text. If its compilation or execution fails at any point, the CustomPatternLoggerDefaultPattern will be used. It supports the following operations:

{{.Level}} // the level of the message

Level prints the level of the log message, at least 4 characters.

{{.Message}} // the message to be printed

Message prints the log message that should be printed. If the message or the pattern doesn't end with a line break, no line break will be printed.

{{.Timestamp}} or {{.Timestampf "2006-01-02 03:04:05PM"}} // time.Time.Format's layout is used

Timestampf takes a string argument, which will be used for formatting the timestamp in the log message. The reference time is the same as in time.Time's function "Format". Timestamp uses the layout "2006-01-02 15:04:05.000".

{{.File}} or {{.Filef "short"}} // one of "short", "full" (anything different will be interpreted as "full")

Filef "short" prints only the filename, while Filef "full" prints the file's absolute path.

{{.Line}} // prints the line of the output call

Line only prints the line number.

{{.Function}} or {{.Functionf "package"}} // one of "short", "package", "full" (anything different will be interpreted as "full")

Functionf "short" prints only the function name, while Functionf "package" will print <package>.<function>. Functionf "full" will print <full_package>.<function>, e.g. "github.com/TimSatke/abc.main".

Example:

{{.Timestamp}} {{.Filef "short"}}:{{.Line}} {{.Functionf "package"}} [{{.Level}}] - {{.Message}}\n

will print something like

2018-11-24 15:26:44.453 main.go:16 main.main [INFO] - Hello World!
<line break>

func (*CustomPatternLogger) Debug

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

Debug prints the given values with log level DEBG.

func (*CustomPatternLogger) Debugf

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

Debugf formats and prints the given values with log level DEBG.

func (*CustomPatternLogger) Error

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

Error prints the given values with log level ERR.

func (*CustomPatternLogger) Errorf

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

Errorf formats and prints the given values with log level ERR.

func (*CustomPatternLogger) Fatal

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

Fatal prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*CustomPatternLogger) Fatalf

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

Fatalf formats and prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*CustomPatternLogger) Info

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

Info prints the given values with log level INFO.

func (*CustomPatternLogger) Infof

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

Infof formats and prints the given values with log level INFO.

func (*CustomPatternLogger) IsLevelEnabled

func (l *CustomPatternLogger) IsLevelEnabled(lvl LogLevel) bool

IsLevelEnabled returns true if and only if this logger would print messages with the given log level. False otherwise.

func (*CustomPatternLogger) Level

func (l *CustomPatternLogger) Level() LogLevel

Level returns the current level of this logger.

func (*CustomPatternLogger) Out

func (l *CustomPatternLogger) Out() io.Writer

Out returns the writer of this logger.

func (*CustomPatternLogger) Print

func (l *CustomPatternLogger) Print(lvl LogLevel, v ...interface{})

Print prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*CustomPatternLogger) Printf

func (l *CustomPatternLogger) Printf(lvl LogLevel, format string, v ...interface{})

Printf formats and prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*CustomPatternLogger) SetClock

func (l *CustomPatternLogger) SetClock(clk clock)

SetClock sets a new clock for this logger.

func (*CustomPatternLogger) SetLevel

func (l *CustomPatternLogger) SetLevel(lvl LogLevel)

SetLevel changes the log level of this logger.

func (*CustomPatternLogger) SetLevelString

func (s *CustomPatternLogger) SetLevelString(level string)

func (*CustomPatternLogger) SetOut

func (l *CustomPatternLogger) SetOut(out io.Writer)

SetOut sets a new writer for this logger.

func (*CustomPatternLogger) Verbose

func (l *CustomPatternLogger) Verbose(v ...interface{})

Verbose prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*CustomPatternLogger) Verbosef

func (l *CustomPatternLogger) Verbosef(format string, v ...interface{})

Verbosef formats and prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*CustomPatternLogger) Warn

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

Warn prints the given values with log level WARN.

func (*CustomPatternLogger) Warnf

func (l *CustomPatternLogger) Warnf(format string, v ...interface{})

Warnf formats and prints the given values with log level WARN.

type LogLevel

type LogLevel uint8

LogLevel is an alias and represents a log level.

const (
	LevelVerbose LogLevel = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Available log levels.

LevelVerbose LogLevel = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal

func ToLogLevel

func ToLogLevel(levelName string) LogLevel

func (LogLevel) String

func (l LogLevel) String() string

String returns a string representation of the level that can be used in the log output.

type Logger

type Logger interface {
	// Print prints the given values with the given log level,
	// if and only if the given log level is higher than or
	// equal to the one of this logger.
	Print(LogLevel, ...interface{})
	// Printf formats and prints the given values with the given log level,
	// if and only if the given log level is higher than or
	// equal to the one of this logger.
	Printf(LogLevel, string, ...interface{})

	// Verbose prints the given values with log level DEBG,
	// if and only if this logger has the verbose log level enabled.
	Verbose(...interface{})
	// Verbosef formats and prints the given values with log level DEBG,
	// if and only if this logger has the verbose log level enabled.
	Verbosef(string, ...interface{})

	// Debug prints the given values with log level DEBG.
	Debug(...interface{})
	// Debugf formats and prints the given values with log level DEBG.
	Debugf(string, ...interface{})

	// Info prints the given values with log level INFO.
	Info(...interface{})
	// Infof formats and prints the given values with log level INFO.
	Infof(string, ...interface{})

	// Warn prints the given values with log level WARN.
	Warn(...interface{})
	// Warnf formats and prints the given values with log level WARN.
	Warnf(string, ...interface{})

	// Error prints the given values with log level ERR.
	Error(...interface{})
	// Errorf formats and prints the given values with log level ERR.
	Errorf(string, ...interface{})

	// Fatal prints the given values with log level FATAL.
	// IT DOES NOT TERMINATE THE APPLICATION.
	Fatal(...interface{})
	// Fatalf formats and prints the given values with log level FATAL.
	// IT DOES NOT TERMINATE THE APPLICATION.
	Fatalf(string, ...interface{})

	// SetLevel changes the log level of this logger.
	SetLevel(LogLevel)
	// SetLevelString changes to log level of this logger.
	// The string is case-insensitive and can be one of
	//
	//	[verbose,debug,info,warn,error,fatal]
	SetLevelString(string)
	// IsLevelEnabled returns true if and only if this logger would print
	// messages with the given log level.
	// False otherwise.
	IsLevelEnabled(LogLevel) bool
}

Logger describes objects that can log messages. It can differentiate between several log levels, and provides methods for each one.

func Root

func Root() Logger

Root returns the globally used root logger.

type NamedLogger

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

NamedLogger is a logger that has and prints a name in its log messages. NamedLoggers are completely safe for concurrent use.

func (*NamedLogger) Debug

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

Debug prints the given values with log level DEBG.

func (*NamedLogger) Debugf

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

Debugf formats and prints the given values with log level DEBG.

func (*NamedLogger) Error

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

Error prints the given values with log level ERR.

func (*NamedLogger) Errorf

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

Errorf formats and prints the given values with log level ERR.

func (*NamedLogger) Fatal

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

Fatal prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*NamedLogger) Fatalf

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

Fatalf formats and prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*NamedLogger) Info

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

Info prints the given values with log level INFO.

func (*NamedLogger) Infof

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

Infof formats and prints the given values with log level INFO.

func (*NamedLogger) IsLevelEnabled

func (l *NamedLogger) IsLevelEnabled(lvl LogLevel) bool

IsLevelEnabled returns true if and only if this logger would print messages with the given log level. False otherwise.

func (*NamedLogger) Level

func (l *NamedLogger) Level() LogLevel

Level returns the current level of this logger.

func (*NamedLogger) Name

func (l *NamedLogger) Name() string

Name returns the name of this logger.

func (*NamedLogger) Out

func (l *NamedLogger) Out() io.Writer

Out returns the writer of this logger.

func (*NamedLogger) Print

func (l *NamedLogger) Print(lvl LogLevel, v ...interface{})

Print prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*NamedLogger) Printf

func (l *NamedLogger) Printf(lvl LogLevel, format string, v ...interface{})

Printf formats and prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*NamedLogger) SetClock

func (l *NamedLogger) SetClock(clk clock)

SetClock sets a new clock for this logger.

func (*NamedLogger) SetLevel

func (l *NamedLogger) SetLevel(lvl LogLevel)

SetLevel changes the log level of this logger.

func (*NamedLogger) SetLevelString

func (s *NamedLogger) SetLevelString(level string)

func (*NamedLogger) SetName

func (l *NamedLogger) SetName(name string)

SetName sets a new name for this logger.

func (*NamedLogger) SetOut

func (l *NamedLogger) SetOut(out io.Writer)

SetOut sets a new writer for this logger.

func (*NamedLogger) Verbose

func (l *NamedLogger) Verbose(v ...interface{})

Verbose prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*NamedLogger) Verbosef

func (l *NamedLogger) Verbosef(format string, v ...interface{})

Verbosef formats and prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*NamedLogger) Warn

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

Warn prints the given values with log level WARN.

func (*NamedLogger) Warnf

func (l *NamedLogger) Warnf(format string, v ...interface{})

Warnf formats and prints the given values with log level WARN.

type SimpleLogger

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

SimpleLogger is a logger that prints log messages. SimpleLoggers are completely safe for concurrent use.

func (*SimpleLogger) Debug

func (s *SimpleLogger) Debug(v ...interface{})

Debug prints the given values with log level DEBG.

func (*SimpleLogger) Debugf

func (s *SimpleLogger) Debugf(format string, v ...interface{})

Debugf formats and prints the given values with log level DEBG.

func (*SimpleLogger) Error

func (s *SimpleLogger) Error(v ...interface{})

Error prints the given values with log level ERR.

func (*SimpleLogger) Errorf

func (s *SimpleLogger) Errorf(format string, v ...interface{})

Errorf formats and prints the given values with log level ERR.

func (*SimpleLogger) Fatal

func (s *SimpleLogger) Fatal(v ...interface{})

Fatal prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*SimpleLogger) Fatalf

func (s *SimpleLogger) Fatalf(format string, v ...interface{})

Fatalf formats and prints the given values with log level FATAL. IT DOES NOT TERMINATE THE APPLICATION.

func (*SimpleLogger) Info

func (s *SimpleLogger) Info(v ...interface{})

Info prints the given values with log level INFO.

func (*SimpleLogger) Infof

func (s *SimpleLogger) Infof(format string, v ...interface{})

Infof formats and prints the given values with log level INFO.

func (*SimpleLogger) IsLevelEnabled

func (s *SimpleLogger) IsLevelEnabled(lvl LogLevel) bool

IsLevelEnabled returns true if and only if this logger would print messages with the given log level. False otherwise.

func (*SimpleLogger) Level

func (s *SimpleLogger) Level() LogLevel

Level returns the current level of this logger.

func (*SimpleLogger) Out

func (s *SimpleLogger) Out() io.Writer

Out returns the writer of this logger.

func (*SimpleLogger) Print

func (s *SimpleLogger) Print(lvl LogLevel, v ...interface{})

Print prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*SimpleLogger) Printf

func (s *SimpleLogger) Printf(lvl LogLevel, format string, v ...interface{})

Printf formats and prints the given values with the given log level, if and only if the given log level is higher than or equal to the one of this logger.

func (*SimpleLogger) SetClock

func (s *SimpleLogger) SetClock(clk clock)

SetClock sets a new clock for this logger.

func (*SimpleLogger) SetLevel

func (s *SimpleLogger) SetLevel(lvl LogLevel)

SetLevel changes the log level of this logger.

func (*SimpleLogger) SetLevelString

func (s *SimpleLogger) SetLevelString(level string)

func (*SimpleLogger) SetOut

func (s *SimpleLogger) SetOut(out io.Writer)

SetOut sets a new writer for this logger.

func (*SimpleLogger) Verbose

func (s *SimpleLogger) Verbose(v ...interface{})

Verbose prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*SimpleLogger) Verbosef

func (s *SimpleLogger) Verbosef(format string, v ...interface{})

Verbosef formats and prints the given values with log level DEBG, if and only if this logger has the verbose log level enabled.

func (*SimpleLogger) Warn

func (s *SimpleLogger) Warn(v ...interface{})

Warn prints the given values with log level WARN.

func (*SimpleLogger) Warnf

func (s *SimpleLogger) Warnf(format string, v ...interface{})

Warnf formats and prints the given values with log level WARN.

type WriterLogger

type WriterLogger interface {
	Logger

	// Out returns the writer to which the output
	// is printed.
	Out() io.Writer
	// SetOut changes the writer to which the output
	// is printed.
	SetOut(io.Writer)
}

WriterLogger is an interface that embeds abc.Logger. It describes loggers that write their output to an io.Writer, and provides methods for retrieving and changing that writer.

func NewColoredLogger

func NewColoredLogger(wrapped WriterLogger) WriterLogger

NewColoredLogger creates a wrapper for a given WriterLogger. Depending on the level that should be printed, this wrapper will prepend an ANSI-color code to the wrapped loggers output writer and will then call the respective output method. Please notice that this function does not add a decorator to the given logger, but creates a wrapper, which must be used for colors to show up.

func NewCustomPatternLogger

func NewCustomPatternLogger(pattern string) (WriterLogger, error)

NewCustomPatternLogger returns a new abc.CustomPatternLogger, which was initialized and thus is ready to use. If an error occurs during the initialization, that error is returned.

The given pattern is a go template text and supports the following operations:

{{.Level}} // the level of the message

Level prints the level of the log message, at least 4 characters.

{{.Message}} // the message to be printed

Message prints the log message that should be printed. If the message or the pattern doesn't end with a line break, no line break will be printed.

{{.Timestamp}} or {{.Timestampf "2006-01-02 03:04:05PM"}} // time.Time.Format's layout is used

Timestampf takes a string argument, which will be used for formatting the timestamp in the log message. The reference time is the same as in time.Time's function "Format". Timestamp uses the layout "2006-01-02 15:04:05.000".

{{.File}} or {{.Filef "short"}} // one of "short", "full" (anything different will be interpreted as "full")

Filef "short" prints only the filename, while Filef "full" prints the file's absolute path.

{{.Line}} // prints the line of the output call

Line only prints the line number.

{{.Function}} or {{.Functionf "package"}} // one of "short", "package", "full" (anything different will be interpreted as "full")

Functionf "short" prints only the function name, while Functionf "package" will print <package>.<function>. Functionf "full" will print <full_package>.<function>, e.g. "github.com/TimSatke/abc.main".

Example:

{{.Timestamp}} {{.Filef "short"}}:{{.Line}} {{.Functionf "package"}} [{{.Level}}] - {{.Message}}\n

will print something like

2018-11-24 15:26:44.453 main.go:16 main.main [INFO] - Hello World!
<line break>

func NewNamedLogger

func NewNamedLogger(name string) WriterLogger

NewNamedLogger returns a new abc.NamedLogger, which is ready to use. The default log level is INFO and can be changed with

logger.SetLevel(abc.LevelInfo)

The logger prints to os.Stdout by default. The output writer can be changed with

logger.SetOut(os.Stdout)

func NewSimpleLogger

func NewSimpleLogger() WriterLogger

NewSimpleLogger returns a new abc.SimpleLogger, which is ready to use. The default log level is INFO and can be changed with

logger.SetLevel(abc.LevelInfo)

The logger prints to os.Stdout by default. The output writer can be changed with

logger.SetOut(os.Stdout)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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