logg

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 5 Imported by: 0

README

logg

A thread-safe zerolog wrapper that supports global redirection.

logg lets you change the output destination (including multiple writers) at runtime for all existing loggers. It also enforces consistent structured fields (pkg, component) across your application.

Quick Start

import "github.com/cyphix/logg"

func main() {
    // Global setup
    logg.Init("info")
    
    // Usage
    logger := logg.Ctx("main", "startup")
    logger.Info().Msg("Application starting...")
}

Features

  • Dynamic Redirection: Change output writers globally. Existing loggers automatically use the new destination without being recreated.
  • Structured Context: Ctx(pkg, component) ensures metadata is consistently applied.
  • Silent Mode: Global toggle to discard all logs.
  • Console Support: Built-in zerolog.ConsoleWriter integration.
  • Custom Keys: Override default field names (pkg, component).

Usage Examples

Development (Pretty-Print)
logg.InitConsole("debug")
Multiple Destinations
f, _ := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
logg.InitWithWriters("info", os.Stderr, f)
Runtime Redirection

Loggers created before a redirection will immediately use the new writer.

logger := logg.Ctx("service", "worker")

// Logs to Stderr
logger.Info().Msg("Starting")

// Redirect all loggers to a buffer
var buf bytes.Buffer
logg.InitWithWriter("info", &buf)

logger.Info().Msg("This now goes to buf")
Silent Mode
logg.SetSilent(true)
// ... noise-heavy operations ...
logg.SetSilent(false)
Custom Field Names
logg.SetKeys("mod", "sub", "ev", "res")
logger := logg.Ctx("auth", "ldap")
// Output: {"mod": "auth", "sub": "ldap", ...}

Installation

go get github.com/cyphix/logg

Documentation

Overview

Package logg provides a simple, thread-safe logging wrapper for zerolog with dynamic redirection support.

It allows for global initialization of logging levels and output destinations, which can be changed at runtime even for loggers that have already been created. It also encourages structured logging by providing package and component context through the Ctx function.

Index

Constants

This section is empty.

Variables

View Source
var (
	KeyPkg       = "pkg"
	KeyComponent = "component"
	KeyEvent     = "event"
	KeyResult    = "result"
)

Functions

func Ctx

func Ctx(pkg string, component string) zerolog.Logger

Ctx returns a zerolog.Logger pre-configured with package and component context. This preserves the full zerolog API while ensuring structured metadata is always present.

func Init

func Init(level string)

Init initializes the logger with the specified level and logs to os.Stderr.

func InitConsole

func InitConsole(level string)

InitConsole initializes the logger with a ConsoleWriter for pretty-printed, colored output.

func InitWithWriter

func InitWithWriter(level string, w io.Writer)

InitWithWriter initializes the logger with the specified level and output writer.

func InitWithWriters

func InitWithWriters(level string, writers ...io.Writer)

InitWithWriters initializes the logger with the specified level and multiple output writers.

func SetKeys

func SetKeys(pkg string, component string, event string, result string)

SetKeys allows customizing the keys used for structured logging.

func SetSilent

func SetSilent(silent bool)

SetSilent enables or disables silent mode. In silent mode, all logs are discarded. When disabled, the previous writer is restored.

Types

This section is empty.

Jump to

Keyboard shortcuts

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