clog

package module
v1.0.0 Latest Latest
Warning

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

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

README

clog: My custom preferred setup for logging in go

This features nothing new, the whole purpose of this repo is to save me few seconds it takes to copy paste my logger in every project :)

Credits: lmittmann/tint

Documentation

Overview

Example
package main

import (
	"errors"
	"time"

	"github.com/ayankit/clog"
)

func main() {
	clog.Init(clog.LevelDebug)

	clog.Info("Starting server", "addr", ":8080", "env", "production")
	clog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
	clog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
	clog.Error("DB connection lost", clog.Err(errors.New("connection reset")), "db", "myapp")
}
Example (WithLogToFile)

Create a new logger that writes to given log file

package main

import (
	"errors"
	"time"

	"github.com/ayankit/clog"
)

func main() {
	clog.Init(clog.LevelDebug, "/path/to/log/file")

	clog.Info("Starting server", "addr", ":8080", "env", "production")
	clog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
	clog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
	clog.Error("DB connection lost", clog.Err(errors.New("connection reset")), "db", "myapp")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attr

func Attr(color uint8, attr slog.Attr) slog.Attr

Attr returns a tinted (colorized) slog.Attr that will be written in the specified color by the [tint.Handler]. When used with any other slog.Handler, it behaves as a plain slog.Attr.

Use the uint8 color value to specify the color of the attribute:

  • 0-7: standard ANSI colors
  • 8-15: high intensity ANSI colors
  • 16-231: 216 colors (6×6×6 cube)
  • 232-255: grayscale from dark to light in 24 steps

See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

func Debug

func Debug(msg string, args ...any)

func Debugf

func Debugf(format string, args ...any)

func Err

func Err(err error) slog.Attr

Err returns a tinted (colorized) slog.Attr that will be written in red color by the [tint.Handler]. When used with any other slog.Handler, it behaves as

slog.Any("err", err)

func Error

func Error(msg string, args ...any)

func Errorf

func Errorf(format string, args ...any)

func Fatal

func Fatal(msg string, args ...any)

func Fatalf

func Fatalf(format string, args ...any)

func Info

func Info(msg string, args ...any)

func Infof

func Infof(format string, args ...any)

func Init

func Init(level Level, logFilePath ...string) error

func NewHandler

func NewHandler(w io.Writer, opts *Options) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.

func Warn

func Warn(msg string, args ...any)

func Warnf

func Warnf(format string, args ...any)

func With

func With(args ...any) *slog.Logger

Types

type Level

type Level int
const (
	LevelDebug Level = -4
	LevelInfo  Level = 0
	LevelWarn  Level = 4
	LevelError Level = 8
)

type Options

type Options struct {
	// Enable source code location (Default: false)
	AddSource bool

	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See https://pkg.go.dev/log/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

	// Time format (Default: time.StampMilli)
	TimeFormat string

	// Disable color (Default: false)
	NoColor bool
}

Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.

Options can be used as a drop-in replacement for slog.HandlerOptions.

Jump to

Keyboard shortcuts

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