gologger

package module
v1.1.69 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 10 Imported by: 2,616

README

gologger

gologger is a very simple logging package to do structured logging in go.

Features

  • Simple and fast structured logging with beautiful CLI output
  • Fully compatible with Go's standard log/slog package
  • Zero-breaking-change migration - use both APIs simultaneously
  • Identical output format - slog calls produce same CLI appearance
  • Rich metadata support - structured key-value logging
  • Multiple levels - Info, Debug, Warning, Error, Fatal, Verbose, Silent
  • Custom formatters - CLI, JSON, and custom formats
  • Flexible writers - stdout/stderr, files, rotation, custom writers

Use gologger as a library

package main

import (
	"strconv"

	"github.com/projectdiscovery/gologger"
	"github.com/projectdiscovery/gologger/levels"
)

func main() {
	gologger.DefaultLogger.SetMaxLevel(levels.LevelDebug)
	//	gologger.DefaultLogger.SetFormatter(&formatter.JSON{})
	gologger.Print().Msgf("\tgologger: sample test\t\n")
	gologger.Info().Str("user", "pdteam").Msg("running simulation program")
	for i := 0; i < 10; i++ {
		gologger.Info().Str("count", strconv.Itoa(i)).Msg("running simulation step...")
	}
	gologger.Debug().Str("state", "running").Msg("planner running")
	gologger.Warning().Str("state", "errored").Str("status", "404").Msg("could not run")
	gologger.Fatal().Msg("bye bye")
}

slog Compatibility

gologger is fully compatible with Go's standard log/slog package. You can use gologger as an slog handler to get the same beautiful CLI output while leveraging the standard Go logging ecosystem.

Quick Start with slog
package main

import (
	"log/slog"
	"github.com/projectdiscovery/gologger"
	"github.com/projectdiscovery/gologger/levels"
)

func main() {
	// Setup gologger as slog handler (one-time setup)
	gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose)
	slog.SetDefault(slog.New(gologger.DefaultLogger))
	
	// Now both APIs produce identical output:
	gologger.Info().Str("user", "john").Msg("Hello from gologger") 
	slog.Info("Hello from slog", slog.String("user", "john"))
	
	// Both output: [INF] Hello from gologger [user=john]
	//              [INF] Hello from slog [user=john]
}
Migration Benefits
  • 🔄 Zero Breaking Changes: All existing gologger code continues working
  • 🎯 Identical Output: slog produces the exact same CLI format
  • 📈 Incremental Migration: Migrate function by function at your own pace
  • 🌟 Best of Both Worlds: Keep gologger's aesthetics, gain slog's ecosystem
  • 🔧 Advanced Features: Groups, persistent attributes, different handler types
Learn More

See the complete slog Compatibility Guide for:

  • Detailed side-by-side API comparisons with output examples
  • How attributes, groups, and custom levels work
  • Step-by-step migration strategies
  • Advanced features and best practices

gologger is made with 🖤 by the projectdiscovery team.

Documentation

Overview

Package gologger provides a simple layer for leveled logging in go.

Index

Constants

View Source
const (
	LevelTrace   = slog.Level(-8) // Most detailed logging (DEBUG-4)
	LevelVerbose = slog.Level(-6) // More detailed than debug (DEBUG-2)
	LevelSilent  = slog.Level(1)  // No label output (INFO+1)
	LevelFatal   = slog.Level(12) // Critical errors causing exit (ERROR+4)
)

Custom slog levels that match gologger's level hierarchy. These can be used with any slog handler.

Variables

This section is empty.

Functions

func TrimGologgerLevels added in v1.1.69

func TrimGologgerLevels() *slog.HandlerOptions

TrimGologgerLevels creates handler options that convert gologger offset levels to clean names e.g., "DEBUG-4" becomes "TRACE", "ERROR+4" becomes "FATAL", "INFO+1" becomes "" (silent)

Types

type Event added in v1.1.0

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

Event is a log event to be written with data

func Debug

func Debug() *Event

Debug writes an error message on the screen with the default label

func Error

func Error() *Event

Error writes a error message on the screen with the default label

func Fatal

func Fatal() *Event

Fatal exits the program if we encounter a fatal error

func Info

func Info() *Event

Info writes a info message on the screen with the default label

func Print added in v1.1.0

func Print() *Event

Print prints a string without any extra labels.

func Silent

func Silent() *Event

Silent prints a string on stdout without any extra labels.

func Verbose

func Verbose() *Event

Verbose prints a string only in verbose output mode.

func Warning

func Warning() *Event

Warning writes a warning message on the screen with the default label

func (*Event) Label added in v1.1.4

func (e *Event) Label(label string) *Event

Label applies a custom label on the log event

func (*Event) Msg added in v1.1.0

func (e *Event) Msg(message string)

Msg logs a message to the logger

func (*Event) MsgFunc added in v1.1.5

func (e *Event) MsgFunc(messageSupplier func() string)

MsgFunc logs a message with lazy evaluation. Useful when computing the message can be resource heavy.

func (*Event) Msgf added in v1.1.0

func (e *Event) Msgf(format string, args ...interface{})

Msgf logs a printf style message to the logger

func (*Event) Str added in v1.1.0

func (e *Event) Str(key, value string) *Event

Str adds a string metadata item to the log

func (*Event) TimeStamp added in v1.1.5

func (e *Event) TimeStamp() *Event

TimeStamp adds timestamp to the log event

type Logger added in v1.1.0

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

Logger is a logger for logging structured data in a beautiful and fast manner.

var (

	// DefaultLogger is the default logging instance
	DefaultLogger *Logger
)

func (*Logger) Debug added in v1.1.0

func (l *Logger) Debug() *Event

Debug writes an error message on the screen with the default label

func (*Logger) Enabled added in v1.1.69

func (l *Logger) Enabled(_ context.Context, level slog.Level) bool

Enabled implements slog.Handler interface

func (*Logger) Error added in v1.1.0

func (l *Logger) Error() *Event

Error writes a error message on the screen with the default label

func (*Logger) Fatal added in v1.1.0

func (l *Logger) Fatal() *Event

Fatal exits the program if we encounter a fatal error

func (*Logger) Handle added in v1.1.69

func (l *Logger) Handle(ctx context.Context, record slog.Record) error

Handle implements slog.Handler interface

func (*Logger) Info added in v1.1.0

func (l *Logger) Info() *Event

Info writes a info message on the screen with the default label

func (*Logger) Log added in v1.1.0

func (l *Logger) Log(event *Event)

Log logs a message to a logger instance

func (*Logger) Print added in v1.1.0

func (l *Logger) Print() *Event

Print prints a string on screen without any extra labels.

func (*Logger) SetFormatter added in v1.1.0

func (l *Logger) SetFormatter(formatter formatter.Formatter)

SetFormatter sets the formatter instance for a logger

func (*Logger) SetMaxLevel added in v1.1.0

func (l *Logger) SetMaxLevel(level levels.Level)

SetMaxLevel sets the max logging level for logger

func (*Logger) SetTimestamp added in v1.1.6

func (l *Logger) SetTimestamp(timestamp bool, minLevel levels.Level)

SetTimestamp enables/disables automatic or custom timestamp

func (*Logger) SetTimestampWithFormat added in v1.1.58

func (l *Logger) SetTimestampWithFormat(timestamp bool, minLevel levels.Level, format string)

func (*Logger) SetWriter added in v1.1.0

func (l *Logger) SetWriter(writer writer.Writer)

SetWriter sets the writer instance for a logger

func (*Logger) Verbose added in v1.1.0

func (l *Logger) Verbose() *Event

Verbose prints a string only in verbose output mode.

func (*Logger) Warning added in v1.1.0

func (l *Logger) Warning() *Event

Warning writes a warning message on the screen with the default label

func (*Logger) WithAttrs added in v1.1.69

func (l *Logger) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler interface

func (*Logger) WithGroup added in v1.1.69

func (l *Logger) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler interface. Per the slog.Handler contract, an empty name returns the receiver unchanged.

Directories

Path Synopsis
examples
basic command
rotatedaily command
rotatehourly command
slog command
Example: using gologger as a log/slog handler.
Example: using gologger as a log/slog handler.
tee command

Jump to

Keyboard shortcuts

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