gelog

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 8 Imported by: 1

README

gelog

gelog is a simple and practical logger initialization and handler helper package built on top of Go’s standard log/slog.

  • Log rotation support (via lumberjack)
  • MultiHandler for fan-out logging
  • Human-readable simple format (PlainHandler)
  • debug / release switching using build tags

Features

✅ Based on slog

Uses Go’s standard log/slog, officially available since Go 1.21.

✅ Log rotation

Uses lumberjack to manage log file size, backups, and retention period.

✅ Multiple outputs

With MultiHandler, a single log record can be sent to multiple slog.Handlers.

✅ debug / release build switching

Using //go:build debug:

  • debug: logging enabled
  • release: logging calls become no-ops

This allows zero logging overhead in production builds.


Installation

go get github.com/ge-editor/gelog

Usage

import "github.com/ge-editor/gelog"

func main() {
    gelog.InitLogger("app.log")

    gelog.Info("application started",
        "version", "1.0.0",
        "port", 8080,
    )
}

Log Output Example

time=2025-01-01T12:00:00.000+09:00 level=INFO source=main.go:12 msg="application started" version=1.0.0 port=8080

MultiHandler

MultiHandler bundles multiple slog.Handler instances into one.

handler := gelog.NewMultiHandler(
    fileHandler,
    stdoutHandler,
)
  • Implements Enabled, Handle, WithAttrs, WithGroup
  • Fully compatible with slog.Handler

OS-specific Behavior

if runtime.GOOS == "windows" {
    handler = fileHandler
} else {
    handler = NewMultiHandler(
        fileHandler,
        // Stdout output is currently disabled
    )
}
  • Windows: file output only
  • Unix-like systems: designed for multiple outputs (currently file only)

debug / release Builds

debug build
go build -tags debug
gelog.Info("this will be logged")
release build (default)
go build
gelog.Info("this will be ignored")

VSCode Development Environment Variables

.vscode/settings.json:

{
    "go.toolsEnvVars": {
        "GOFLAGS": "-tags=debug,ja_JP"
    }
}

Documentation

Index

Constants

View Source
const (
	LevelKey slog.Level = 2
)

Variables

This section is empty.

Functions

func CallerInfo added in v0.1.5

func CallerInfo() string

func Debug

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

func Error

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

func Info

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

func InitLogger

func InitLogger(logFilePath string, maxSizeMB int) error

func Key

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

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) slog.Handler

func Warn added in v0.1.5

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

Types

type MultiHandler

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

--- 複数出力用ハンドラ ---

func (*MultiHandler) Enabled

func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*MultiHandler) Handle

func (h *MultiHandler) Handle(ctx context.Context, record slog.Record) error

func (*MultiHandler) WithAttrs

func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*MultiHandler) WithGroup

func (h *MultiHandler) WithGroup(name string) slog.Handler

type PlainHandler

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

func NewPlainHandler

func NewPlainHandler(w io.Writer) *PlainHandler

func (*PlainHandler) Enabled

func (h *PlainHandler) Enabled(_ context.Context, _ slog.Level) bool

func (*PlainHandler) Handle

func (h *PlainHandler) Handle(_ context.Context, r slog.Record) error

func (*PlainHandler) WithAttrs

func (h *PlainHandler) WithAttrs(_ []slog.Attr) slog.Handler

func (*PlainHandler) WithGroup

func (h *PlainHandler) WithGroup(_ string) slog.Handler

type SizeLimitedWriter

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

func NewSizeLimitedWriter

func NewSizeLimitedWriter(path string, maxSizeMB int) (*SizeLimitedWriter, error)

func (*SizeLimitedWriter) Write

func (w *SizeLimitedWriter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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