logger

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

README

logger

Structured, level-based logger with optional JSON output for production.

What it does

  • Levels: Trace, Info, Warning, Error, Fatal (Fatal calls os.Exit(1)).
  • Module and APP_NAME: Each logger is bound to a module; app name comes from env APP_NAME.
  • Development: Colored, human-readable lines (level, time, module, message, key-value params).
  • Production: When GO_ENV=production, logs are JSON (app, module, level, message, date, and extra fields).
  • LogParams: Extra data as map[string]any (or structs serialized as JSON).

Installation

go get github.com/teathedev/pkg/logger

Requires: github.com/teathedev/pkg/env, github.com/teathedev/pkg/utils.

Usage

import "github.com/teathedev/pkg/logger"

func main() {
	log := logger.New("Payments")

	log.Info("charge started", logger.LogParams{"amount": 1000, "currency": "USD"})
	log.Warning("retry attempt", logger.LogParams{"attempt": 2})
	log.Error("charge failed", logger.LogParams{"error": err.Error(), "id": orderID})
	// log.Fatal("unrecoverable") // logs and os.Exit(1)

	// With struct (in production, serialized as JSON in "data")
	log.Info("order created", logger.LogParams{"data": myOrder})
}

Set environment for production JSON logs:

  • GO_ENV=production
  • APP_NAME=my-service (recommended; init warns if missing)

API

Function / Type Description
New(module string) Logger Creates a logger for that module (uses APP_NAME from env).
Logger Interface: Trace, Info, Warning, Error, Fatal(message string, args ...LogParams).
LogParams map[string]any for key-value or struct payloads.

Documentation

Overview

Package logger provides a logger for the application.

Index

Constants

View Source
const (
	LOGLEVELTrace   logLevels = "TRACE"
	LOGLEVELInfo    logLevels = "INFO"
	LOGLEVELWarning logLevels = "WARNING"
	LOGLEVELError   logLevels = "ERROR"
	LOGLEVELFatal   logLevels = "FATAL"
)

Variables

This section is empty.

Functions

func New

func New(module string) *logger

Types

type LogParams

type LogParams = map[string]any

type Logger

type Logger interface {
	Trace(string, ...LogParams)
	Info(string, ...LogParams)
	Warning(string, ...LogParams)
	Error(string, ...LogParams)
	Fatal(string, ...LogParams)
}

Jump to

Keyboard shortcuts

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