zlogger

package module
v0.0.0-...-83205bd Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 11 Imported by: 0

README

ZLogger - Advanced Structured Logging for Go

ZLogger is a structured logging library for Go, built on top of zerolog. It provides an easy-to-use API with advanced features like:

  • Log rotation with lumberjack
  • Sentry integration for automatic error tracking
  • Custom log levels with colors for better readability
  • Context-aware logging for structured and contextual debugging

🚀 Features

  • 🔥 High-performance structured logging using zerolog.
  • 📁 Automatic log rotation to prevent log files from growing indefinitely.
  • 🛠 Sentry integration to capture and monitor errors seamlessly.
  • 🎨 Customizable log level names and colors.
  • 📡 Context-aware logging to enrich log data.
  • 🌍 Lightweight and efficient with minimal overhead.

📦 Installation

# Install ZLogger using go modules
 go get github.com/pablolagos/zlogger

⚡ Usage

Basic Logging
package main

import (
	"github.com/pablolagos/zlogger"
)

func main() {
	logger := zlogger.New("app.log", 10, 5, true) // 10MB max size, 5 backups, colors enabled
	logger.Info("Application started")
	logger.Debug("Debugging details...")
	logger.Error("An error occurred")
}
Logging with Sentry
logger := zlogger.NewWithSentry("app.log", 10, 5, true, "your_sentry_dsn", "1.0.0", "production")
logger.Error("Critical error: Database connection failed")
Context-aware Logging
import "context"

ctx := context.WithValue(context.Background(), "request_id", "12345")
logger.InfoCtx(ctx, "Processing request")
Logging to Stderr (Testing)
logger := zlogger.NewStdErr()
logger.Info("This message logs to stderr")

🔔 How Sentry Works with ZLogger

ZLogger integrates seamlessly with Sentry to capture and monitor errors in your Go application. When using NewWithSentry(), ZLogger automatically sends logs of level Error and above to Sentry.

How it Works
  1. ZLogger initializes a Sentry client with the provided DSN (Data Source Name).
  2. Errors, warnings, or fatal logs are captured and sent to Sentry.
  3. Sentry records logs with stack traces and metadata (like environment and release version).
  4. You can view and analyze errors in your Sentry dashboard.
Example Configuration
logger := zlogger.NewWithSentry("app.log", 10, 5, true, "your_sentry_dsn", "1.0.0", "production")
logger.Error("Database connection failed")
Sentry Best Practices
  • Ensure your DSN is correctly configured in environment variables.
  • Use meaningful release versions to track issues across deployments.
  • Call sentry.Flush(time.Second * 2) before exiting the application to ensure logs are sent.

🎨 Log Level Customization

ZLogger supports custom log level names and colors:

  • INFO: Blue
  • WARN: Yellow
  • ERROR: Red
  • FATAL: Red background with white text
  • DEBUG: High-intensity blue

To disable colors, pass false in the New() function:

logger := zlogger.New("app.log", 10, 5, false) // Colors disabled

🛠 Available Methods

ZLogger provides the following logging methods:

Standard Logging Methods
logger.Debug("Debug message")
logger.Info("Informational message")
logger.Warn("Warning message")
logger.Error("Error message")
Formatted Logging Methods
logger.Debugf("Debugging: %s", "details")
logger.Infof("User %s logged in", "John")
logger.Warnf("Warning: %d attempts detected", 3)
logger.Errorf("Error: %v", err)
Context-aware Logging Methods
logger.DebugCtx(ctx, "Debug message with context")
logger.InfoCtx(ctx, "Info message with context")
logger.WarnCtx(ctx, "Warning message with context")
logger.ErrorCtx(ctx, "Error message with context")

🛠 Configuration Options

Parameter Type Description
filename string Log file path (empty to use stderr)
maxSize int Max log file size in MB before rotation
maxBackups int Number of rotated logs to retain (0 = unlimited)
enableColors bool Enable/disable color output

🔥 Why Use ZLogger?

  • Performance: Efficient structured logging with low memory overhead.
  • Flexibility: Works with stdout, file-based logging, and remote monitoring (Sentry).
  • Simplicity: Easy-to-use API with sane defaults.
  • Scalability: Suitable for microservices, monoliths, and cloud-based applications.

🛡 License

This project is licensed under the MIT License.


👨‍💻 Contributing

We welcome contributions! Feel free to submit issues and pull requests to improve ZLogger.


Author

Developed by Pablo Lagos.


⭐ If you like this project, don't forget to star it on GitHub!


Happy Logging! 🚀

Documentation

Overview

Package zlogger is a wrapper for zerolog with specific format and optionally integrated with Sentry

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiLevelLogger

type MultiLevelLogger interface {
	Printf(format string, args ...interface{})
	Print(args ...interface{})
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Panic(args ...interface{})
	Panicf(format string, args ...interface{})
}

type SimpleLogger

type SimpleLogger interface {
	Printf(format string, args ...interface{})
	Print(args ...interface{})
}

type ZLogger

type ZLogger struct {
	zerolog.Logger
	// contains filtered or unexported fields
}

func New

func New(filename string, maxSize int, maxBackups int, color bool) *ZLogger

New creates a console zerolog with auto rotating feature

	Filename: Filename to write log. If empty, stderr will be used.
	MaxSize: Max size before rotating, in MB
	MaxBackups: Number of backups to retain. 0=unlimited
 color: If true, colors will be used in the output

func NewStdErr

func NewStdErr() *ZLogger

NewStdErr creates a zerolog with stderr output, for testing purposes

func NewWithSentry

func NewWithSentry(filename string, maxSize int, maxBackups int, dsn, release, environment string, color bool) *ZLogger

NewWithSentry creates a zerolog with auto rotating feature and Sentry integration

Filename: Filename to write log. If empty, stderr will be used.
MaxSize: Max size before rotating, in MB
MaxBackups: Number of backups to retain. 0=unlimited

func (*ZLogger) Debug

func (z *ZLogger) Debug(v ...interface{})

func (*ZLogger) Debugf

func (z *ZLogger) Debugf(format string, v ...interface{})

func (*ZLogger) Error

func (z *ZLogger) Error(v ...interface{})

func (*ZLogger) Errorf

func (z *ZLogger) Errorf(format string, v ...interface{})

func (*ZLogger) Fatal

func (z *ZLogger) Fatal(v ...interface{})

func (*ZLogger) Fatalf

func (z *ZLogger) Fatalf(format string, v ...interface{})

func (*ZLogger) GetLogger

func (z *ZLogger) GetLogger() zerolog.Logger

func (*ZLogger) Info

func (z *ZLogger) Info(v ...interface{})

func (*ZLogger) Infof

func (z *ZLogger) Infof(format string, v ...interface{})

func (*ZLogger) Panic

func (z *ZLogger) Panic(v ...interface{})

func (*ZLogger) Panicf

func (z *ZLogger) Panicf(format string, v ...interface{})

func (*ZLogger) Rotate

func (z *ZLogger) Rotate() error

Rotate attempts to rotate the underlying log file if a lumberjack logger is configured. Returns an error on failure. The rotation is automatically triggered when the log file reaches the specified size limit, but this method can be called manually if needed.

func (*ZLogger) Warn

func (z *ZLogger) Warn(v ...interface{})

func (*ZLogger) Warnf

func (z *ZLogger) Warnf(format string, v ...interface{})

Jump to

Keyboard shortcuts

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