Skip to content

Repository files navigation

Logger for Go

A simple, customizable logger for Go with both synchronous and asynchronous logging modes, colored output, and various log levels.

Features

  • Supports JSON formatted output with metadata (device_id, app_version, service), trace_id, and arbitrary structured data
  • Supports sync and async logging
  • ANSI color formatting for log levels (text mode)
  • Supports Info, Debug, Warn, Error, Panic, Fatal messages
  • Buffered channel for async logging with Flush() support
  • Configurable log styles
  • Tagged log messages

Installation

go get github.com/ABA-Developer/go-logger

Usage

JSON Logger

package main

import (
	"github.com/ABA-Developer/go-logger"
)

func main() {
	config := logger.JSONConfig{
		DeviceID:    "gantry-bersih-01",
		AppVersion:  "v1.4.2",
		Service:     "rfid-hardware",
		LogLevel:    logger.LevelDebug, // LevelDebug (0), LevelInfo (1), LevelWarn (2), LevelError (3), LevelFatal (4)

		PrettyPrint: false,             // set true for formatted JSON
	}

	log := logger.NewSyncJSON(config)

	log.Warn("RFID reader high antenna temperature detected").
		TraceID("TRX-1785752204719").
		Data(map[string]any{"antenna_id": 2, "temperature_celsius": 68.5})
}

Output:

{
    "timestamp": "2026-08-05T13:28:03.123+07:00",
    "level": "WARN",
    "device_id": "gantry-bersih-01",
    "app_version": "v1.4.2",
    "trace_id": "TRX-1785752204719",
    "service": "rfid-hardware",
    "msg": "RFID reader high antenna temperature detected",
    "data": {
        "antenna_id": 2,
        "temperature_celsius": 68.5,
        "threshold_celsius": 65.0
    }
}

Synchronous Logger

package main

import (
	"github.com/ABA-Developer/go-logger"
)

func main() {
	SyncImplementation()
}

func SyncImplementation() {
	logger := logger.NewSync("TEST", true) // Tag: "TEST", Debug mode enabled
	logger.SetDefaultStyle()
	logger.Debug("Sync logger started")

	for i := 1; i <= 10; i++ {
		if i%3 == 0 {
			logger.Error(i)
		} else if i%5 == 0 {
			logger.Warn(i)
		} else {
			logger.Info(i)
		}
	}
}

Asynchronous Logger

package main

import (
	"github.com/ABA-Developer/go-logger"
)

func main() {
	AsyncImplementation()
}

func AsyncImplementation() {
	logger := logger.NewAsync("TEST", 10, true) // Tag: "TEST", Buffer size: 10, Debug mode enabled
	logger.SetDefaultStyle()
	logger.Debug("Async logger started")

	for i := 1; i <= 10; i++ {
		if i%3 == 0 {
			logger.Error(i)
		} else if i%5 == 0 {
			logger.Warn(i)
		} else {
			logger.Info(i)
		}
	}

	logger.Flush() // Ensure all logs are printed before exit
}

API Reference

Logger Creation

  • NewSync(tag string, debugMode bool) *LoggerSync
  • NewAsync(tag string, bufferSize int, debugMode bool) *LoggerAsync
  • NewSyncJSON(config JSONConfig) *LoggerSyncJSON (or NewJSON(config))

Logging Methods

  • logger.Info(a ...any)
  • logger.Infof(format string, a ...any)
  • logger.Debug(a ...any)
  • logger.Debugf(format string, a ...any)
  • logger.Warn(a ...any)
  • logger.Warnf(format string, a ...any)
  • logger.Error(a ...any)
  • logger.Errorf(format string, a ...any)
  • logger.Panic(a ...any)
  • logger.Panicf(format string, a ...any)
  • logger.Fatal(a ...any)
  • logger.Fatalf(format string, a ...any)

JSON Context Methods

  • logger.WithTraceID(traceID string)
  • logger.WithData(data any)
  • logger.With(traceID string, data any)
  • logger.WarnWith(msg string, traceID string, data any)
  • logger.InfoWith(msg string, traceID string, data any)
  • logger.ErrorWith(msg string, traceID string, data any)
  • logger.DebugWith(msg string, traceID string, data any)

Additional Methods

  • logger.Flush() (async logger)
  • logger.SetInfoStyle(styles ...int8)
  • logger.SetWarnStyle(styles ...int8)
  • logger.SetErrorStyle(styles ...int8)
  • logger.SetDebugStyle(styles ...int8)
  • logger.SetPanicStyle(styles ...int8)
  • logger.SetFatalStyle(styles ...int8)
  • logger.SetDefaultStyle()

Log Output Format

Standard text mode:

[INFO ] [YYYY-MM-DD HH:MM:SS.mmm] [TAG]: message
[WARN ] [YYYY-MM-DD HH:MM:SS.mmm] [TAG]: message
[ERROR] [YYYY-MM-DD HH:MM:SS.mmm] [TAG]: message
[DEBUG] [YYYY-MM-DD HH:MM:SS.mmm] [TAG]: message

JSON mode:

{
  "timestamp": "2026-08-05T13:28:03.123+07:00",
  "level": "WARN",
  "device_id": "gantry-bersih-01",
  "app_version": "v1.4.2",
  "trace_id": "TRX-1785752204719",
  "service": "rfid-hardware",
  "msg": "RFID reader high antenna temperature detected",
  "data": {
    "antenna_id": 2,
    "temperature_celsius": 68.5,
    "threshold_celsius": 65
  }
}

License

MIT

About

Golang logger styling

Resources

Stars

0 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages