logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 9 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.2.0

TL;DR

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

l, _ := logger.NewLogger("app.log")
defer l.Close()

l.Info("Ready!")

Quick start

import logger "github.com/Chelaran/yagalog"

func main() {
    l, err := logger.NewLogger("app.log")
    if err != nil { panic(err) }
    defer l.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 writing to given file path
func NewLogger(path string) (*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 (keep it simple)

// Level filtering
l.SetLevel(logger.INFO) // drop DEBUG in prod

// Colors
l.WithColors(true)           // force enable/disable (AUTO by default via TTY/NO_COLOR)

// Time format
l.WithTimeFormat(time.RFC3339Nano)

// Caller file:line
l.WithCaller(true)

// JSON to file (single-line per entry)
l.WithJSON()

// Control file sink
l.DisableFile()                 // console only
_ = l.EnableFile("app.log")    // re-enable / switch file

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(logFilePath string) (*Logger, error)

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)

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