log

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: GPL-3.0 Imports: 7 Imported by: 10

README

Log v1

Structured logging built on top of log/slog.

Overview

This package wraps slog with:

  • package-level helpers for common log levels
  • source metadata on each record
  • stack traces on debug logs
  • optional OpenTelemetry correlation via otel

Installation

go get github.com/jgolang/log

Quick Start

package main

import "github.com/jgolang/log"

func main(){
    log.Info("My info....")
}
Output:
{"time":"2026-05-02T10:00:00Z","level":"INFO","msg":"My info....","source":{"func":"main","file":"main.go","line":6}}

Configuration

log.SetLevel(slog.LevelWarn)
log.NewTextHandler()
log.SetSource(true)
log.SetDebugStackTrace(false)

The package no longer depends on a MODE environment variable. Debug stack traces are now opt-in.

Instance API

logger := log.New(
    log.WithLevel(slog.LevelInfo),
    log.WithTextHandler(os.Stdout),
    log.WithSource(true),
    log.WithDebugStackTrace(false),
)

logger.Info("service started")

OpenTelemetry

The otel handler now disables baggage logging by default. If you need baggage in logs, enable it explicitly and prefer an allow-list:

handler := otel.New(
    slog.NewJSONHandler(os.Stdout, nil),
    otel.WithNoBaggage(false),
    otel.WithBaggageAllowList("request_id", "tenant"),
)

Avoid enabling all baggage in production unless the upstream context is already sanitized. Baggage can contain tenant identifiers, tokens, or other sensitive values; use WithBaggageAllowList, WithBaggageDenyList, or WithBaggageFilter to keep log output intentional.

Verification

go test ./...
go test -race ./...
go vet ./...
go test -bench=. -run=^$ ./...

Use the benchmarks to compare source metadata, disabled levels, and debug stack traces before changing logger internals.


Released under the GPL-3.0.

Documentation

Overview

Package log provides structured logging helpers built on top of log/slog.

The package adds source metadata to every record, stack traces to debug records, and OpenTelemetry integration through the otel subpackage.

Installation

Run command in terminal:

go get github.com/jgolang/log

Quick Start

This is a simple example of how the package is implemented with a basic function.

package main

import "github.com/jgolang/log"

func main(){
    log.Info("My info....")
}

Output:

{"time":"2026-05-02T10:00:00Z","level":"INFO","msg":"My info....","source":{"func":"main","file":"main.go","line":6}}

Configuration

You can configure the current level and handler programmatically:

log.SetLevel(slog.LevelWarn)
log.NewTextHandler()
log.SetSource(true)
log.SetDebugStackTrace(false)

The package does not depend on environment variables, and debug stack traces are opt-in.

The otel subpackage disables baggage logging by default. If baggage is enabled, prefer allow-lists or explicit filters so sensitive context values are not written to logs accidentally.

You can also build isolated logger instances:

logger := log.New(
    log.WithLevel(slog.LevelInfo),
    log.WithTextHandler(os.Stdout),
    log.WithSource(true),
    log.WithDebugStackTrace(false),
)

logger.Info("service started")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...any)

Debug logs a debug-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.

func DebugC added in v1.3.0

func DebugC(ctx context.Context, args ...any)

DebugC logs a debug-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.

func Error

func Error(args ...any)

Error logs an error-level message using the global logger.

func ErrorC added in v1.3.0

func ErrorC(ctx context.Context, args ...interface{})

ErrorC logs an error-level message with context using the global logger. ctx: The context for the log entry..

func Fatal

func Fatal(args ...any)

Fatal logs a fatal-level message using the global logger and then calls os.Exit(1).

func FatalC added in v1.3.0

func FatalC(ctx context.Context, args ...any)

FatalC logs a fatal-level message with context using the global logger and then calls os.Exit(1). ctx: The context for the log entry.

func Info

func Info(args ...interface{})
Example (Info)
package main

import (
	"github.com/jgolang/log"
)

func main() {
	// Use this function to see the trace of execution of the sentence.
	// This function is useful for tracking where errors are generated.
	log.Info("Hello world!")
}

func InfoC added in v1.3.0

func InfoC(ctx context.Context, args ...interface{})

func Level added in v1.3.6

func Level() slog.Level

Level return current log level

func NewJSONHandler added in v1.3.0

func NewJSONHandler()

func NewTextHandler added in v1.3.0

func NewTextHandler()

func Panic

func Panic(args ...any)

Panic logs a panic-level message using the global logger and then panics.

func PanicC added in v1.3.0

func PanicC(ctx context.Context, args ...any)

PanicC logs a panic-level message with context using the global logger and then panics. ctx: The context for the log entry.

func Print added in v1.3.0

func Print(args ...interface{})

func PrintC added in v1.3.3

func PrintC(ctx context.Context, args ...interface{})

func SetCalldepth added in v1.1.2

func SetCalldepth(calldepth int)

SetCalldepth configure the number of stack frames to ascend, with 0 identifying the caller of Caller for default loggin

func SetDebugStackTrace added in v1.4.0

func SetDebugStackTrace(enabled bool)

SetDebugStackTrace controls whether package-level debug logs include stack traces.

func SetLevel added in v1.3.0

func SetLevel(newLevel slog.Level) (oldLevel slog.Level)

SetLevel sets the logging level for the Logger instance. This method updates the log level to the specified level and returns the previous log level.

Parameters:

level (slog.Level) - The new log level to be set. This determines the severity of the logs
                     that will be captured. Common log levels include DEBUG, INFO, WARN, and ERROR.

Returns:

oldLevel (slog.Level) - The previous log level before it was updated. This can be used to restore
                        the previous log level if needed.

Example usage:

logger := &Logger{}
oldLevel := logger.SetLevel(slog.INFO)
// The log level is now set to INFO
// You can restore the old level if needed
logger.SetLevel(oldLevel)

func SetSource added in v1.4.0

func SetSource(enabled bool)

SetSource controls whether package-level logs include source metadata.

func StackTrace

func StackTrace() slog.Attr

StackTrace allows you to view the exact place where the error or incident originated within the code. Shows a trace of up to 10 layers from where the error or incident was generated.

func Warn

func Warn(args ...any)

Warn logs a warning-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.

func WarnC added in v1.3.0

func WarnC(ctx context.Context, args ...interface{})

WarnC logs a warning-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.

Types

type Logger added in v1.1.0

type Logger = logger.Logger

func New added in v1.1.0

func New(opts ...Option) *Logger

New creates a configurable logger instance without touching package-level state.

type Option added in v1.4.0

type Option = logger.Option

func WithDebugStackTrace added in v1.4.0

func WithDebugStackTrace(enabled bool) Option

func WithJSONHandler added in v1.4.0

func WithJSONHandler(w io.Writer) Option

func WithLevel added in v1.4.0

func WithLevel(level slog.Level) Option

func WithSource added in v1.4.0

func WithSource(enabled bool) Option

func WithTextHandler added in v1.4.0

func WithTextHandler(w io.Writer) Option

Directories

Path Synopsis
core module

Jump to

Keyboard shortcuts

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