logger

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 10 Imported by: 0

README

YagaLog

Simple, colorful Go logger. Dual output (console + file). Thread‑safe. Zero setup.

Go Reference Go Version


Install last version

go get github.com/Chelaran/yagalog@v0.3.0

TL;DR

import (
    logger "github.com/Chelaran/yagalog"
)

// console only
l, _ := logger.NewLogger()
defer l.Close()

// or with file
l2, err := logger.NewLogger(logger.WithFilePath("app.log"))
if err == nil {
    defer l2.Close()
}

l.Info("Ready!")

Quick start

import logger "github.com/Chelaran/yagalog"

func main() {
    // console only
    l, err := logger.NewLogger()
    if err != nil { panic(err) }
    defer l.Close()

    // or with file
    lf, err := logger.NewLogger(logger.WithFilePath("app.log"))
    if err == nil {
        defer lf.Close()
    }

    l.Info("Hello from YagaLog %d", 1)
    l.Warning("Be careful")
    l.Error("Oops: %s", "something went wrong")
}

Features

  • Colorized console levels: DEBUG, INFO, WARNING, ERROR, FATAL
  • File logging with date/time
  • Dual output: console + file (simultaneously)
  • Thread‑safe file writes
  • Lightweight dependency footprint

Levels

Level Purpose
DEBUG подробные события в DEV
INFO обычный поток событий
WARNING потенциальные проблемы
ERROR ошибки, но сервис жив
FATAL критично, завершение

API

// Create new logger with functional options
func NewLogger(opts ...Option) (*Logger, error)

// Logging methods (printf‑style formatting supported)
func (l *Logger) Debug(msg string, v ...interface{})
func (l *Logger) Info(msg string, v ...interface{})
func (l *Logger) Warning(msg string, v ...interface{})
func (l *Logger) Error(msg string, v ...interface{})
func (l *Logger) Fatal(msg string, v ...interface{}) // exits with code 1

// Gracefully close file handle
func (l *Logger) Close() error

When to use

  • You want a tiny, readable logger with colorized console and file output
  • You don’t need JSON/structured logs, hooks, or advanced sinks (yet)

Compatibility

  • Go 1.20+ (tested with 1.25.3)
  • OS: Linux/macOS/Windows

Options (functional options API)

// Examples using options at construction time or via helpers

// Construction-time options
lf, _ := logger.NewLogger(
        logger.WithFilePath("app.log"),
        logger.WithLevel(logger.INFO),
        logger.WithJSON(),
)
defer lf.Close()

// Runtime helpers remain available
l.SetLevel(logger.INFO)
l.WithColors(true) // toggles color output for console
// or use WithWriter to direct console logs to a custom io.Writer
buf := &bytes.Buffer{}
_ = logger.NewLogger(logger.WithWriter(buf))

// You can still DisableFile()/EnableFile(path) at runtime
// to switch file sinks dynamically.

Changelog

  • v0.3.0 — 2025-11-20

    • Introduced functional-options API for NewLogger(opts ...Option).
    • New options: WithFilePath, WithWriter, WithLevel, WithJSON, WithTimeFormat, WithCaller, WithColors.
    • Backwards compatibility: constructor signature changed — replace NewLogger(path) with NewLogger(WithFilePath(path)) or call NewLogger() for console-only.
  • v0.2.0 — previous

Examples

Run any example (more in examples/):

go run ./examples/basic    # INFO level, RFC3339Nano time
go run ./examples/caller   # adds file:line
go run ./examples/json     # writes JSON lines to app.log

Best practices

DEV preset

l.SetLevel(logger.DEBUG)
l.WithCaller(true)
// colors auto (TTY), human‑readable console + file

PROD preset

l.SetLevel(logger.INFO)
l.WithCaller(false)
l.WithJSON()           // JSON lines to file for aggregators
// optionally: l.DisableFile() if logs collected from stdout only

Roadmap (short)

  • Optional JSON/structured output
  • Custom formatters and minimal hook interface
  • Rolling file options

Contributing

Issues and PRs are welcome. Keep code clear, small, and well‑tested.

Security

Found a vulnerability? Please open a private report via GitHub Security Advisories.

License

MIT © 2025 Chelaran. Created by bambutcha.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel

type LogLevel int
const (
	DEBUG LogLevel = iota
	INFO
	WARNING
	ERROR
	FATAL
)

type Logger

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

func NewLogger

func NewLogger(opts ...Option) (*Logger, error)

NewLogger constructs a Logger with functional options. Options are applied before any file is opened, so options like WithFilePath should be provided to have effect. NewLogger may return an error if opening the configured file fails.

func (*Logger) Close

func (l *Logger) Close() error

func (*Logger) Debug

func (l *Logger) Debug(msg string, v ...interface{})

func (*Logger) DisableFile added in v0.2.0

func (l *Logger) DisableFile()

File control

func (*Logger) EnableFile added in v0.2.0

func (l *Logger) EnableFile(path string) error

func (*Logger) Error

func (l *Logger) Error(msg string, v ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, v ...interface{})

func (*Logger) Info

func (l *Logger) Info(msg string, v ...interface{})

func (*Logger) SetLevel added in v0.2.0

func (l *Logger) SetLevel(level LogLevel)

func (*Logger) Warning

func (l *Logger) Warning(msg string, v ...interface{})

func (*Logger) WithCaller added in v0.2.0

func (l *Logger) WithCaller(enable bool)

func (*Logger) WithColors added in v0.2.0

func (l *Logger) WithColors(enable bool)

func (*Logger) WithJSON added in v0.2.0

func (l *Logger) WithJSON()

func (*Logger) WithTimeFormat added in v0.2.0

func (l *Logger) WithTimeFormat(layout string)

type Option added in v0.3.0

type Option func(*Logger)

Option configures a Logger during construction.

func WithCaller added in v0.3.0

func WithCaller(enable bool) Option

func WithColors added in v0.3.0

func WithColors(enable bool) Option

func WithFilePath added in v0.3.0

func WithFilePath(path string) Option

WithFilePath sets a file path to open and write logs into. NewLogger will attempt to open the file and may return an error if it fails.

func WithJSON added in v0.3.0

func WithJSON() Option

func WithLevel added in v0.3.0

func WithLevel(level LogLevel) Option

func WithTimeFormat added in v0.3.0

func WithTimeFormat(layout string) Option

func WithWriter added in v0.3.0

func WithWriter(w io.Writer) Option

WithWriter sets the destination used for console loggers (defaults to os.Stdout).

Directories

Path Synopsis
examples
basic command
caller command
json command

Jump to

Keyboard shortcuts

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