Skip to content

Time Formatting ​

This example demonstrates how to customize timestamp formatting and timezones. Logly allows you to use standard date format strings or switch between Local and UTC time.

Code Example ​

zig
const std = @import("std");
const logly = @import("logly");

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

    // Enable colors on Windows
    _ = logly.Terminal.enableAnsiColors();

    const logger = try logly.Logger.init(allocator);
    defer logger.deinit();

    var config = logly.Config.default();

    // Example 1: Default format (YYYY-MM-DD HH:mm:ss.SSS)
    logger.configure(config);
    try logger.info("Default time format", @src());

    // Example 2: Custom time format
    // Supports standard format specifiers
    config.time_format = "HH:mm:ss";
    logger.configure(config);
    try logger.info("Short time format", @src());

    // Example 3: UTC timezone
    // Switch to UTC time instead of local time
    config.timezone = .utc;
    config.time_format = logly.Config.TimeFormat.iso8601;
    logger.configure(config);
    try logger.info("UTC time", @src());

    // Example 4: Local timezone offset in custom format (+HH:MM)
    config.timezone = .local;
    config.time_format = "YYYY-MM-DD HH:mm:ss ZZZ";
    logger.configure(config);
    try logger.info("Local timezone offset (ZZZ)", @src());

    // Example 5: Local timezone compact offset (+HHMM)
    config.time_format = "YYYY-MM-DD HH:mm:ss ZZ";
    logger.configure(config);
    try logger.info("Local timezone compact offset (ZZ)", @src());
}

ZZZ and ZZ are useful in production when logs from multiple regions are aggregated, because each event keeps an explicit timezone offset.

Expected Output ​

text
[2024-06-01 12:00:00] [INFO] Default time format
[12:00:00] [INFO] Short time format
[10:00:00] [INFO] UTC time
[2024-06-01 12:00:00 +02:00] [INFO] Local timezone offset (ZZZ)
[2024-06-01 12:00:00 +0200] [INFO] Local timezone compact offset (ZZ)

Released under the MIT License.