logger

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 8 Imported by: 0

README

JSON based logger for golang

A simple and efficient JSON-based logging library for Go applications, designed for easy integration and observability. It provides structured logging with various levels, automatic trace context propagation, and customizable output to streamline log management and analysis.

[!IMPORTANT] This repository was migrated from github.

Please refer to github.com/iolave/go-logger for versions <= v1.0.1

Install

go get codeberg.org/mice1337/go-logger

Features

  • JSON formatted logs for easy parsing and querying.
  • Log levels to control log verbosity.
  • Automatic inclusion of trace information from context via go-trace.
  • Handles circular references in log data.
  • Customizable log output with additional data.

Usage

Create a logger
// ...
import "codeberg.org/mice1337/go-logger"

// Creates a logger with a name and version
logger, err := logger.New(logger.LEVEL_INFO, "my-awesome-logger", "1.0.0")
if err != nil {
    // Handle error
}
Write logs
ctx := context.Background() // Or your request context

logger.Debug(ctx, "debug message")
// {"timestamp":1722216216,"level":"debug","name":"my-awesome-logger","version":"1.0.0","trace":{},"info":{},"msg":"debug message"}

logger.Info(ctx, "info message")
// {"timestamp":1722216216,"level":"info","name":"my-awesome-logger","version":"1.0.0","trace":{},"info":{},"msg":"info message"}

logger.Warn(ctx, "warn message", errors.New("something happened"))
// {"timestamp":1722216216,"level":"warn","name":"my-awesome-logger","version":"1.0.0","trace":{},"error":{},"info":{},"msg":"warn message"}

logger.Error(ctx, "error message", errors.New("something bad happened"))
// {"timestamp":1722216216,"level":"error","name":"my-awesome-logger","version":"1.0.0","trace":{},"error":{},"info":{},"msg":"error message"}

logger.Fatal(ctx, "fatal message", errors.New("something terrible happened"))
// Exits the application after logging
// {"timestamp":1722216216,"level":"fatal","name":"my-awesome-logger","version":"1.0.0","trace":{},"error":{},"info":{},"msg":"fatal message"}
With trace information

The logger will automatically include trace information from the context if it's provided via go-trace.

import (
	"context"
	"errors"

	"codeberg.org/mice1337/go-logger"
	"codeberg.org/mice1337/go-trace"
)

// ...

// Create a new trace
t := trace.Trace{}
t.Set("trace_id", "my-trace-id")
t.Set("span_id", "my-span-id")

// Add the trace to the context
ctx := t.SetInContext(context.Background())

// Log a message with the context
logger.Info(ctx, "This log includes trace information")
// {"timestamp":1722216216,"level":"info","name":"my-awesome-logger","version":"1.0.0","trace":{"trace_id":"my-trace-id","span_id":"my-span-id"},"info":{},"msg":"This log includes trace information"}

Schema version: v1.0.0

Log entry schema
Field Description JSON type
timestamp Unix time number
level Log entry level string
name Name of the app/logger string
version Version of the app/logger string
trace Trace information from context object
error Error information object
info Custom data in any form or shape object
msg Log message string
Log level definition
Level Name
60 fatal
50 error
40 warn
30 info
20 debug
10 trace

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

Development

To get started with development, you'll need Go installed.

  1. Fork the repository.
  2. Clone your fork: git clone https://codeberg.org/YOUR_USERNAME/go-logger.git
  3. Create a new branch: git checkout -b my-feature-branch
  4. Make your changes.
  5. Run tests: make test
  6. Push to your branch: git push origin my-feature-branch
  7. Open a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Level is the level of the logger.
	Level Level
}

Config is the configuration of the logger.

type Entry

type Entry struct {
	Timestamp int64          `json:"timestamp"`
	Level     Level          `json:"-"`
	LevelStr  string         `json:"level"`
	Name      string         `json:"name"`
	Version   string         `json:"version"`
	Trace     trace.Trace    `json:"trace"`
	Error     error          `json:"error,omitempty"`
	Info      map[string]any `json:"info"`
	Msg       string         `json:"msg"`
}

Entry is the json representation of a log entry.

type Level

type Level int

Level is the level of the logger.

const (
	// LEVEL_TRACE is the trace level.
	LEVEL_TRACE Level = (iota + 1) * 10
	// LEVEL_DEBUG is the debug level.
	LEVEL_DEBUG
	// LEVEL_INFO is the info level.
	LEVEL_INFO
	// LEVEL_WARN is the warn level.
	LEVEL_WARN
	// LEVEL_ERROR is the error level.
	LEVEL_ERROR
	// LEVEL_FATAL is the fatal level.
	LEVEL_FATAL
)

func NewLevelFromString

func NewLevelFromString(s string) (Level, error)

NewLevelFromString returns a level from a string. If the string is not a valid level, an error of type *errors.Error is returned.

func (Level) IsValid

func (l Level) IsValid() bool

IsValid checks if the value is a valid level.

func (Level) String

func (l Level) String() string

String returns the string representation of the level in lower case.

type Logger

type Logger interface {
	// Trace logs a message with the LEVEL_TRACE level.
	Trace(ctx context.Context, msg string)

	// TraceWithData logs a message with the LEVEL_TRACE level
	// and the given data.
	TraceWithData(ctx context.Context, msg string, data map[string]any)

	// Debug logs a message with the LEVEL_DEBUG level.
	Debug(ctx context.Context, msg string)

	// DebugWithData logs a message with the LEVEL_DEBUG level
	// and the given data.
	DebugWithData(ctx context.Context, msg string, data map[string]any)

	// Info logs a message with the LEVEL_INFO level.
	Info(ctx context.Context, msg string)

	// InfoWithData logs a message with the LEVEL_INFO level
	// and the given data.
	InfoWithData(ctx context.Context, msg string, data map[string]any)

	// Warn logs a message with the LEVEL_WARN level.
	Warn(ctx context.Context, msg string, err error)

	// WarnWithData logs a message with the LEVEL_WARN level
	// and the given data.
	WarnWithData(ctx context.Context, msg string, err error, data map[string]any)

	// Error logs a message with the LEVEL_ERROR level.
	Error(ctx context.Context, msg string, err error)

	// ErrorWithData logs a message with the LEVEL_ERROR level
	// and the given data.
	ErrorWithData(ctx context.Context, msg string, err error, data map[string]any)

	// Fatal logs a message with the LEVEL_FATAL level and it's followed by a
	// call to os.Exit(1).
	Fatal(ctx context.Context, msg string, err error)

	// FatalWithData logs a message with the LEVEL_FATAL level
	// and the given data and it's followed by a call to os.Exit(1).
	FatalWithData(ctx context.Context, msg string, err error, data map[string]any)
}

func New

func New(level Level, name string, version string) (Logger, error)

New creates a new logger.

If cfg.Level is not valid, an error of type github.com/iolave/go-errors.GenericError is returned.

Directories

Path Synopsis
internal
test_utils
go-coverage:ignore
go-coverage:ignore
pkg

Jump to

Keyboard shortcuts

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