Skip to content
chrischtel edited this page Feb 5, 2025 · 3 revisions

nexlog - High-Performance Logging Library for Zig

nexlog is a flexible, feature-rich logging library for Zig applications that provides asynchronous logging, file rotation, multiple output handlers, and structured logging capabilities. It's designed to be both powerful and easy to use, making it suitable for applications of any size.

Features

  • Multiple Log Levels: Support for debug, info, warning, and error levels
  • Asynchronous Logging: High-performance async mode for minimal impact on application performance
  • File Rotation: Automatic log file rotation with configurable file size and backup count
  • Customizable Handlers: Built-in handlers for console, file, and JSON output, with support for custom handlers
  • Rich Metadata: Automatic inclusion of timestamps, thread IDs, file names, and function names
  • Structured Logging: JSON output support for machine-readable logs
  • Color Support: Terminal color coding for different log levels
  • Configurable Buffer Size: Adjustable buffer size for optimal performance
  • Context-Based Logging: Support for department/component-specific logging contexts

Installation

Add nexlog as a dependency in your build.zig.zon:

zig fetch --save git+https://github.com/chrischtel/nexlog/

.{
    .name = "my-project",
    .version = "0.1.0",
    .dependencies = .{
        .nexlog = .{
            // 🚧 Nexlog: Actively Developing
            // Expect rapid feature growth and frequent changes
            // Should look something like this, if you want to fetch the develop branch, just ad `#develop`behind nexlog (without the trailing */*
            .url = "git+https://github.com/chrischtel/nexlog/",
            .hash = "...",
        },
    },
}

If you want to fetch a specific release of Nexlog, use the following url format: zig fetch --save https://github.com/chrischtel/nexlog/archive/v0.4.0.tar.gz (choose your corresponding release)

Quick Start

Here's a basic example to get you started:

const std = @import("std");
const nexlog = @import("nexlog");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // Initialize logger with basic configuration
    var builder = nexlog.LogBuilder.init();
    try builder
        .setMinLevel(.debug)
        .enableColors(true)
        .enableFileLogging(true, "logs/app.log")
        .build(allocator);
    defer nexlog.deinit();

    // Get the default logger
    const logger = nexlog.getDefaultLogger() orelse return error.LoggerNotInitialized;

    // Log some messages
    logger.info("Application starting", .{}, null);
    logger.debug("Debug information", .{}, null);
    logger.warn("Warning message", .{}, null);
    logger.err("Error occurred", .{}, null);
}

Advanced Configuration

nexlog supports extensive configuration through its builder pattern:

try builder
    .setMinLevel(.debug)
    .enableColors(true)
    .setBufferSize(8192)
    .enableFileLogging(true, "logs/app.log")
    .setMaxFileSize(5 * 1024 * 1024)  // 5MB
    .setMaxRotatedFiles(3)
    .enableRotation(true)
    .enableAsyncMode(true)
    .enableMetadata(true)
    .build(allocator);

Custom Handlers

nexlog allows you to create custom log handlers for specialized logging needs. Here's a simple example:

const CustomHandler = struct {
    // See custom handler example in documentation
};

JSON Logging

For structured logging, nexlog provides built-in JSON support:

var json_handler = try JsonHandler.init(allocator, .{
    .min_level = .debug,
    .pretty_print = true,
    .output_file = "logs/app.json",
});

Documentation

For more detailed information, check out:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

[Add your license information here]