alog

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 5 Imported by: 0

README

alog

An opinionated slog handler.

Features

  • JSON logging with customizable attributes
  • Configurable output, log level, and source inclusion
  • Contextual logging support
  • No external dependencies

Installation

To install the package, run:

go get github.com/arthurdotwork/alog

Usage

Basic Usage
package main

import (
    "github.com/arthurdotwork/alog"
    "log/slog"
)

func main() {
    logger := alog.Logger()
    logger.Info("This is an info message")
}
Set as default

You can set the logger as the default logger:

package main

import (
    "github.com/arthurdotwork/alog"
    "log/slog"
)

func main() {
    slog.SetDefault(alog.Logger())
    slog.Info("This is an info message")
}
Custom Options

You can customize the logger by providing options:

package main

import (
    "bytes"
    "github.com/arthurdotwork/alog"
    "log/slog"
)

func main() {
    var buf bytes.Buffer
    logger := alog.Logger(
        alog.WithOutput(&buf),
        alog.WithLevel(slog.LevelDebug),
        alog.WithSource(false),
        alog.WithAttrs(slog.Attr{Key: "app", Value: slog.StringValue("myapp")}),
    )
    logger.Debug("This is a debug message")
}
Contextual Logging

You can add contextual information to your logs:

package main

import (
    "context"
    "github.com/arthurdotwork/alog"
    "log/slog"
)

func main() {
    logger := alog.Logger()
    
    // Add values to context
    ctx := alog.Append(context.Background(), slog.String("request_id", "12345"))
    ctx = alog.Append(ctx, slog.Int("count", 42))
    
    // Log with context
    logger.InfoContext(ctx, "Processing request")
}

Testing

To run the tests, use:

go test ./...

Changelog

All notable changes to this project will be documented in the CHANGELOG.md file.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(ctx context.Context, attr slog.Attr) context.Context

Append adds a key-value pair to the context for logging

func Logger

func Logger(opts ...LoggerOptionsFunc) *slog.Logger

Logger creates a new slog.Logger with the specified options

Types

type ContextKey added in v1.1.0

type ContextKey string

ContextKey is the type used for context keys in the alog package

const LogAttrKey ContextKey = "alog_attributes"

LogAttrKey is the context key used to store log attributes

type LoggerOptions

type LoggerOptions struct {
	Output io.Writer
	Level  slog.Level
	Source bool
	Attrs  []slog.Attr
}

LoggerOptions holds configuration for creating a new logger

type LoggerOptionsFunc

type LoggerOptionsFunc func(*LoggerOptions)

LoggerOptionsFunc is a function that modifies LoggerOptions

func WithAttrs

func WithAttrs(attrs ...slog.Attr) LoggerOptionsFunc

WithAttrs adds default attributes to the logger

func WithLevel

func WithLevel(l slog.Level) LoggerOptionsFunc

WithLevel sets the minimum log level

func WithOutput

func WithOutput(w io.Writer) LoggerOptionsFunc

WithOutput sets the output writer for the logger

func WithSource

func WithSource(b bool) LoggerOptionsFunc

WithSource enables or disables source code location in logs

Jump to

Keyboard shortcuts

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