Documentation
¶
Overview ¶
Package loggo provides a simple logging library with configurable log levels, output destinations, and message templates.
The loggo package allows you to create a Logger with a specified log level Threshold and various options to customize its behavior. You can log messages at different levels such as Debug, Info, Warn, Error, and Fatal.
Index ¶
- type CallerProvider
- type Hook
- type Level
- type Logger
- func (l *Logger) Debug(message string)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) Error(message string)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Fatal(message string)
- func (l *Logger) Fatalf(format string, args ...any)
- func (l *Logger) Info(message string)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Log(level Level, message string)
- func (l *Logger) LogE(level Level, message string) error
- func (l *Logger) Logf(level Level, format string, args ...any)
- func (l *Logger) LogfE(level Level, format string, args ...any) error
- func (l *Logger) Warn(message string)
- func (l *Logger) Warnf(format string, args ...any)
- type Option
- func WithCallerProvider(provider CallerProvider) Option
- func WithContext(ctx context.Context) Option
- func WithMaxSize(size int) Option
- func WithOutput(output io.Writer) Option
- func WithPostHook(hook Hook) Option
- func WithPreHook(hook Hook) Option
- func WithTemplate(template string) Option
- func WithTimeFormat(format string) Option
- func WithTimeProvider(provider TimeProvider) Option
- type TimeProvider
Examples ¶
- Logger.Debug
- Logger.Debugf
- Logger.Error
- Logger.Errorf
- Logger.Fatal
- Logger.Fatalf
- Logger.Info
- Logger.Infof
- Logger.Log
- Logger.Log (CallerProviderErr)
- Logger.Log (Context)
- Logger.Log (MaxSize)
- Logger.Log (PostHook)
- Logger.Log (PreHook)
- Logger.Log (PreHookMessage)
- Logger.Log (Template)
- Logger.Log (TemplateComplete)
- Logger.Log (Threshold)
- Logger.Log (TimeFormat)
- Logger.Logf
- Logger.Logf (Threshold)
- Logger.Warn
- Logger.Warnf
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallerProvider ¶
CallerProvider is a function that returns the path of the caller, the file name, and the line number, and a boolean indicating if the information is available.
type Level ¶
type Level byte
Level represents an available log level.
The log levels are ordered by severity, with LevelDebug being the lowest and LevelFatal being the highest. The levels are: - LevelDebug: Used for debugging purposes. - LevelInfo: Used to log general information about the application. - LevelWarn: Used to log warnings about potential issues. - LevelError: Used to log errors that do not cause the application to stop. - LevelFatal: Used to log fatal errors that cause the application to stop.
const ( // LevelDebug is the lowest level and is mostly used for debugging purposes. LevelDebug Level = iota // LevelInfo is used to log general information about the application. LevelInfo // LevelWarn is used to log warnings about potential issues. LevelWarn // LevelError is used to log errors that do not cause the application to stop. LevelError // LevelFatal is used to log fatal errors that cause the application to stop. LevelFatal )
Available log levels.
type Logger ¶
type Logger struct {
Context context.Context // Context for the logger
Threshold Level // Minimum log level to output
// contains filtered or unexported fields
}
Logger is the structure that holds the logger information. It includes the log level Threshold, output destination, message template, and time provider.
func New ¶
New creates a new Logger with the given Threshold and options. The default output is os.Stdout, the default template is "%s [%5s]: %s", and the default time provider is time.Now.
Parameters:
- Threshold: Minimum log level to output.
- options: Variadic options to configure the Logger.
Returns:
- A pointer to the newly created Logger.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithOutput(os.Stderr))
logger.Info("This is an info message")
func (*Logger) Debug ¶
Debug logs a message at the LevelDebug. If an error occurs while logging the message, it is ignored.
Parameters:
- message: The debug message to log.
Example:
logger := loggo.New(loggo.LevelDebug)
logger.Debug("This is a debug message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelDebug, loggo.WithTimeProvider(fakeNow))
logger.Debug("This is a debug log message")
}
Output: 2022-01-25 00:00:00 [DEBUG]: This is a debug log message
func (*Logger) Debugf ¶
Debugf logs a formatted message at the LevelDebug. If an error occurs while logging the message, it is ignored.
Parameters:
- format: The format string for the debug message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelDebug)
logger.Debugf("This is a debug message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelDebug, loggo.WithTimeProvider(fakeNow))
logger.Debugf("This is a debug log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [DEBUG]: This is a debug log message with a "format"
func (*Logger) Error ¶
Error logs a message at the LevelError. If an error occurs while logging the message, it is ignored.
Parameters:
- message: The error message to log.
Example:
logger := loggo.New(loggo.LevelError)
logger.Error("This is an error message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelError, loggo.WithTimeProvider(fakeNow))
logger.Error("This is an error log message")
}
Output: 2022-01-25 00:00:00 [ERROR]: This is an error log message
func (*Logger) Errorf ¶
Errorf logs a formatted message at the LevelError. If an error occurs while logging the message, it is ignored.
Parameters:
- format: The format string for the error message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelError)
logger.Errorf("This is an error message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelError, loggo.WithTimeProvider(fakeNow))
logger.Errorf("This is an error log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [ERROR]: This is an error log message with a "format"
func (*Logger) Fatal ¶
Fatal logs a message at the LevelFatal. If an error occurs while logging the message, it is ignored.
Parameters:
- message: The fatal message to log.
Example:
logger := loggo.New(loggo.LevelFatal)
logger.Fatal("This is a fatal message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelFatal, loggo.WithTimeProvider(fakeNow))
logger.Fatal("This is a fatal log message")
}
Output: 2022-01-25 00:00:00 [FATAL]: This is a fatal log message
func (*Logger) Fatalf ¶
Fatalf logs a formatted message at the LevelFatal. If an error occurs while logging the message, it is ignored.
Parameters:
- format: The format string for the fatal message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelFatal)
logger.Fatalf("This is a fatal message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelFatal, loggo.WithTimeProvider(fakeNow))
logger.Fatalf("This is a fatal log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [FATAL]: This is a fatal log message with a "format"
func (*Logger) Info ¶
Info logs a message at the LevelInfo. If an error occurs while logging the message, it is ignored.
Parameters:
- message: The info message to log.
Example:
logger := loggo.New(loggo.LevelInfo)
logger.Info("This is an info message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow))
logger.Info("This is an info log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message
func (*Logger) Infof ¶
Infof logs a formatted message at the LevelInfo. If an error occurs while logging the message, it is ignored.
Parameters:
- format: The format string for the info message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelInfo)
logger.Infof("This is an info message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow))
logger.Infof("This is an info log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message with a "format"
func (*Logger) Log ¶
Log logs a message at the given log level. If the log level is below the Threshold, the message is not logged. If an error occurs while logging the message, it is ignored.
Parameters:
- level: The log level of the message.
- message: The message to log.
Example:
logger := loggo.New(loggo.LevelInfo) logger.Log(loggo.LevelInfo, "This is an info message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message
Example (CallerProviderErr) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
var errorCallerProvider = func() (pc uintptr, file string, line int, ok bool) {
return 0, "", 0, false
}
func main() {
logger := loggo.New(
loggo.LevelInfo,
loggo.WithTimeProvider(fakeNow),
loggo.WithTemplate("{{.Caller}} [{{.Level}}]: {{.Message}}"),
loggo.WithCallerProvider(errorCallerProvider),
)
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: unknown [INFO]: This is an info log message
Example (Context) ¶
package main
import (
"context"
"fmt"
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
ctx := context.WithValue(context.Background(), "trace_id", "123456")
postHook := func(l *loggo.Logger, msg *string) {
fmt.Printf("Trace ID: %q\n", l.Context.Value("trace_id"))
}
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithContext(ctx), loggo.WithPostHook(postHook))
logger.Log(loggo.LevelInfo, "This is an info log message")
logger.Fatal("This is a fatal log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message Trace ID: "123456" 2022-01-25 00:00:00 [FATAL]: This is a fatal log message Trace ID: "123456"
Example (MaxSize) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithMaxSize(10))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an
Example (PostHook) ¶
package main
import (
"context"
"fmt"
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
ctx := context.WithValue(context.Background(), "count", 0)
preHook := func(l *loggo.Logger, msg *string) {
*msg = fmt.Sprintf("%s, count: %d", *msg, l.Context.Value("count").(int))
}
postHook := func(l *loggo.Logger, msg *string) {
count := l.Context.Value("count").(int)
l.Context = context.WithValue(ctx, "count", count+1)
}
logger := loggo.New(
loggo.LevelInfo,
loggo.WithTimeProvider(fakeNow),
loggo.WithPreHook(preHook),
loggo.WithPostHook(postHook),
loggo.WithContext(ctx),
)
logger.Log(loggo.LevelInfo, "This is an info log message")
logger.Log(loggo.LevelFatal, "This is a fatal log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message, count: 0 2022-01-25 00:00:00 [FATAL]: This is a fatal log message, count: 1
Example (PreHook) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
preHook := func(l *loggo.Logger, msg *string) {
l.Threshold = loggo.LevelWarn
}
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithPreHook(preHook))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output:
Example (PreHookMessage) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
preHook := func(l *loggo.Logger, msg *string) {
*msg = "This is a pre hook message"
}
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithPreHook(preHook))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is a pre hook message
Example (Template) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithTemplate("[{{.Level}}]: {{.Message}}"))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: [INFO]: This is an info log message
Example (TemplateComplete) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
var okCallerProvider = func() (pc uintptr, file string, line int, ok bool) {
return 0, "file", 1, true
}
func main() {
logger := loggo.New(
loggo.LevelInfo,
loggo.WithTimeProvider(fakeNow),
loggo.WithTemplate("{{.Time}} {{.Caller}} | [{{.Level}}]: {{.Message}}"),
loggo.WithCallerProvider(okCallerProvider),
)
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: 2022-01-25 00:00:00 file:1 | [INFO]: This is an info log message
Example (Threshold) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelWarn, loggo.WithTimeProvider(fakeNow))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output:
Example (TimeFormat) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow), loggo.WithTimeFormat("02/01/06 00:00"))
logger.Log(loggo.LevelInfo, "This is an info log message")
}
Output: 25/01/22 00:00 [ INFO]: This is an info log message
func (*Logger) LogE ¶
LogE logs a message at the given log level and returns an error if the message could not be logged. If the log level is below the Threshold, the message is not logged.
Parameters:
- level: The log level of the message.
- message: The message to log.
Returns:
- An error if the message could not be logged, nil otherwise.
Example:
logger := loggo.New(loggo.LevelInfo)
err := logger.LogE(loggo.LevelInfo, "This is an info message")
if err != nil {
log.Fatal(err)
}
func (*Logger) Logf ¶
Logf logs a formatted message at the given log level. If the log level is below the Threshold, the message is not logged. If an error occurs while logging the message, it is ignored.
Parameters:
- level: The log level of the message.
- format: The format string for the message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelInfo) logger.Logf(loggo.LevelInfo, "This is an info message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(fakeNow))
logger.Logf(loggo.LevelInfo, "This is an info log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [ INFO]: This is an info log message with a "format"
Example (Threshold) ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelWarn, loggo.WithTimeProvider(fakeNow))
logger.Logf(loggo.LevelInfo, "This is an info log message with a %s", "format")
}
Output:
func (*Logger) LogfE ¶
LogfE logs a formatted message at the given log level and returns an error if the message could not be logged. If the log level is below the Threshold, the message is not logged.
Parameters:
- level: The log level of the message.
- format: The format string for the message.
- args: The arguments for the format string.
Returns:
- An error if the message could not be logged, nil otherwise.
Example:
logger := loggo.New(loggo.LevelInfo)
err := logger.LogfE(loggo.LevelInfo, "This is an info message with a %s", "format")
if err != nil {
log.Fatal(err)
}
func (*Logger) Warn ¶
Warn logs a message at the LevelWarn. If an error occurs while logging the message, it is ignored.
Parameters:
- message: The warn message to log.
Example:
logger := loggo.New(loggo.LevelWarn)
logger.Warn("This is a warn message")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelWarn, loggo.WithTimeProvider(fakeNow))
logger.Warn("This is a warn log message")
}
Output: 2022-01-25 00:00:00 [ WARN]: This is a warn log message
func (*Logger) Warnf ¶
Warnf logs a formatted message at the LevelWarn. If an error occurs while logging the message, it is ignored.
Parameters:
- format: The format string for the warn message.
- args: The arguments for the format string.
Example:
logger := loggo.New(loggo.LevelWarn)
logger.Warnf("This is a warn message with a %s", "format")
Example ¶
package main
import (
"time"
"github.com/hvpaiva/loggo"
)
var fakeNow = func() time.Time {
return time.Date(2022, 1, 25, 0, 0, 0, 0, time.UTC)
}
func main() {
logger := loggo.New(loggo.LevelWarn, loggo.WithTimeProvider(fakeNow))
logger.Warnf("This is a warn log message with a %q", "format")
}
Output: 2022-01-25 00:00:00 [ WARN]: This is a warn log message with a "format"
type Option ¶
type Option func(*Logger)
Option is a function that configures a Logger.
func WithCallerProvider ¶
func WithCallerProvider(provider CallerProvider) Option
WithCallerProvider configures the caller provider function of a Logger. The default caller provider is runtime.Caller.
Parameters:
- provider: The CallerProvider function to use.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithCallerProvider(func(skip int) (pc uintptr, file string, line int, ok bool) {
return runtime.Caller(skip)
}))
func WithContext ¶ added in v1.0.0
WithContext configures the context of a Logger. The default context is context.Background.
Parameters:
- Context: The context to use.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithContext(context.Background()))
func WithMaxSize ¶
WithMaxSize configures the maximum size of a log message. The default maximum size is 1000.
Parameters:
- size: The maximum size of the log message.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithMaxSize(1000))
func WithOutput ¶
WithOutput configures the output destination of a Logger. The default output is os.Stdout.
Parameters:
- output: The io.Writer to use as the output destination.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithOutput(os.Stderr))
func WithPostHook ¶ added in v1.0.0
WithPostHook adds a post-hook to a Logger. Post-hooks are executed after logging a message.
Parameters:
- hook: The post-hook function to add.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithPostHook(func(Context context.Context, level loggo.Level, message string) {
// Do something after logging the message
}))
func WithPreHook ¶ added in v1.0.0
WithPreHook adds a pre-hook to a Logger. Pre-hooks are executed before logging a message.
Parameters:
- hook: The pre-hook function to add.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithPreHook(func(Context context.Context, level loggo.Level, message string) {
// Do something before logging the message
}))
func WithTemplate ¶
WithTemplate configures the log message template of a Logger. The default template is "{{.Time}} [{{printf \"%5s\" .Level}}]: {{.Message}}".
Parameters:
- template: The template string for log messages.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithTemplate("{{.Time}}: {{.Message}}"))
func WithTimeFormat ¶
WithTimeFormat configures the time format of a Logger. The default time format is "2006-01-02 15:04:05".
Parameters:
- format: The format string for the time in the log message.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeFormat("2006-01-02 15:04:05"))
func WithTimeProvider ¶
func WithTimeProvider(provider TimeProvider) Option
WithTimeProvider configures the time provider function of a Logger. The default time provider is time.Now.
Parameters:
- provider: The TimeProvider function to use.
Example:
logger := loggo.New(loggo.LevelInfo, loggo.WithTimeProvider(func() time.Time { return time.Unix(0, 0) }))
type TimeProvider ¶
TimeProvider is a function that returns the current time.