logger

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

README

██╗░░░░░░█████╗░░██████╗░░██████╗░███████╗██████╗░
██║░░░░░██╔══██╗██╔════╝░██╔════╝░██╔════╝██╔══██╗
██║░░░░░██║░░██║██║░░██╗░██║░░██╗░█████╗░░██████╔╝
██║░░░░░██║░░██║██║░░╚██╗██║░░╚██╗██╔══╝░░██╔══██╗
███████╗╚█████╔╝╚██████╔╝╚██████╔╝███████╗██║░░██║
╚══════╝░╚════╝░░╚═════╝░░╚═════╝░╚══════╝╚═╝░░╚═╝

Logger: Context-Aware Logging for Better Insights

logger is an attempt to improve log visualization by embedding parent/child relationships within the logging context, making it easier to trace log flows. It is uses log/slog package under the hood.

Getting Started

1. Install the Logger Binary

To install the logger binary, run the following command:

go install ella.to/logger/cmd/logger-server@latest

This binary includes both the UI and server components. You can start the logger server by simply running:

logger-server

By default, the server runs on address localhost:2022. To change the port, specify it as an argument:

logger-server localhost:2021

You can then access the UI via your browser at:
http://localhost:2022

2. Integrating Logger into Your Project

First, include the logger library in your project by running:

go get ella.to/logger@latest

Next, add the following code to the main function of your project to set up the logger:

package main

import (
    // other imports
    "log/slog"
    "os"

    "ella.to/logger"
)

func main() {
    slog.SetDefault(
        slog.New(
            logger.NewHttpExporter(
                "http://localhost:2022", // logger server address
                slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
                    Level: slog.LevelDebug,
                }),
            ),
        ),
    )

    // your application logic
}
3. Logging Usage

The logger provides four logging functions—Info, Debug, Warn, and Error—which work similarly to slog.InfoContext.

Example:

ctx := context.Background()

logger.Info(ctx, "first log message", "request_id", 1)

Each of these functions returns a new context, allowing you to pass it to subsequent logs, creating parent/child relationships between logs.

4. HTTP Middleware

This library also includes an HTTP middleware compatible with Go's http.Handler signature:

func(http.Handler) http.Handler

You can use this middleware to either retrieve the parent log ID from the incoming request headers or generate a new parent ID, ensuring all logs within an HTTP request are grouped together for better traceability.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, args ...any) context.Context

func Error

func Error(ctx context.Context, msg string, args ...any) context.Context

func HttpMiddleware added in v0.0.2

func HttpMiddleware() func(next http.Handler) http.Handler

func HttpUpdateRequest added in v0.0.5

func HttpUpdateRequest(r *http.Request)

HttpUpdateRequest updates the http request with the log parent id added to the header

func Info

func Info(ctx context.Context, msg string, args ...any) context.Context

func NewHttpRequest added in v0.0.5

func NewHttpRequest(ctx context.Context, method string, url string, body io.Reader) (*http.Request, error)

func Warn

func Warn(ctx context.Context, msg string, args ...any) context.Context

func WithBufferSize added in v0.0.2

func WithBufferSize(size int) httpExporterOptsFn

func WithInterval added in v0.0.2

func WithInterval(interval time.Duration) httpExporterOptsFn

Types

type HttpExporter added in v0.0.2

type HttpExporter struct {
	// contains filtered or unexported fields
}

func NewHttpExporter added in v0.0.2

func NewHttpExporter(addr string, handler slog.Handler, opts ...httpExporterOptsFn) *HttpExporter

func (*HttpExporter) Close added in v0.0.2

func (e *HttpExporter) Close()

func (*HttpExporter) Enabled added in v0.0.2

func (e *HttpExporter) Enabled(ctx context.Context, level slog.Level) bool

func (*HttpExporter) Handle added in v0.0.2

func (e *HttpExporter) Handle(ctx context.Context, r slog.Record) error

func (*HttpExporter) WithAttrs added in v0.0.2

func (e *HttpExporter) WithAttrs(attrs []slog.Attr) slog.Handler

func (*HttpExporter) WithGroup added in v0.0.2

func (e *HttpExporter) WithGroup(name string) slog.Handler

type Record added in v0.0.2

type Record struct {
	Id        string         `json:"id"`
	ParentId  string         `json:"parent_id"`
	Level     string         `json:"level"`
	Message   string         `json:"message"`
	Meta      map[string]any `json:"meta"`
	Timestamp string         `json:"timestamp"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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