Categories
Logging Processing
  • Github CLI
  • SSH
  • HTTPS
Clone Repo

Zig syslog client

CIWiki      

This is a syslog client library for Zig:

ProtocolsUDP, TCP
RFCSubset of RFC5424
Tested onMac, Windows, Linux

For the curious: IT Explained: Syslog

Hello, Zig!

When client code calls

    logger.write_info("Hello, Zig!");

syslog client sends following text message to syslog receiver process:

<190>1 2024-10-09T09:07:11+00:00 BLKF zigprocess 18548 1 - Hello, Zig!

Let’s see what this message consist of:

ValueRFC DefinitionDescription
190PRIVALPriority
1VERSIONAlways 1
2024-10-09T09:07:11+00:00TIMESTAMPFULL-DATE “T” FULL-TIME
BLKFHOSTNAMEHostname or ’-‘
zigprocessAPP-NAMEApplication name provided by caller
18548PROCIDProcess ID or ’-‘
1MSGIDMessage ID - sequential number generated automatically
-STRUCTURED-DATAAlways ’-‘
Hello, Zig!MSGMessage

Priority

         Priority = Facility * 8 + Severity 

Facility represents the machine process that created the Syslog event

rfc5424.FacilityValueDescription
.kern0kernel messages
.user1random user-level messages
.mail2mail system
.daemon3system daemons
.auth4security/authorization messages
.syslog5messages generated internally by syslogd
.lpr6line printer subsystem
.news7network news subsystem
.uucp8UUCP subsystem
.cron9clock daemon
.authpriv10security/authorization messages (private)
.ftp11ftp daemon
.local016local use 0
.local117local use 1
.local218local use 2
.local319local use 3
.local420local use 4
.local521local use 5
.local622local use 6
.local723local use 7

Severity describes the severity level of the syslog message in question.

Levelrfc5424.SeverityDescription
0.emergsystem is unusable
1.alertaction must be taken immediately
2.critcritical conditions
3.errerror conditions
4.warningwarning conditions
5.noticenormal but significant condition
6.infoinformational
7.debugdebug-level messages

Quiz

What are Facility and Severity of “Hello, Zig!” message?

For leisure time

Installation

Add syslog to build.zig.zon:

zig fetch --save=syslog git+https://github.com/g41797/syslog

Add syslog to build.zig:

    const syslog = b.dependency("syslog", .{
        .target = target,
        .optimize = optimize,
    });

    const lib = b.addStaticLibrary(..);
    lib.root_module.addImport("syslog", syslog.module("syslog"));

    const lib_unit_tests = b.addTest(...);
    lib_unit_tests.root_module.addImport("syslog", syslog.module("syslog"));

Import syslog:

const syslog = @import("syslog");

Usage

Configuration

syslog uses following configuration:

pub const SyslogOpts = struct {
    // application:
    name: []const u8 = "zigprocess",
    fcl: rfc5424.Facility = .local7,

    // transport:
    proto: Protocol = .udp,
    addr: []const u8 = "127.0.0.1",
    port: u16 = 514,
};

Initialization

    var logger: syslog.Syslog = .{};
    try logger.init(std.testing.allocator, .{
        .name = "runner",
        .fcl = .daemon
        .port = 12345,
    });
    defer logger.deinit();

After initialization you can call syslog on different threads.

Logging

There are two groups of APIs:

  • write: message is straight text
    pub inline fn write_<severity>(slog: *Syslog, msg: []const u8) !void {...}
    ....
    logger.write_debug("Hello, Zig!");
  • print: message will be formatted before send
    pub inline fn print_<severity>(slog: *Syslog, comptime fmt: []const u8, msg: anytype) !void {...}
    ....
    const name = "World";
    logger.print_debug("Hello, {s}!", .{name});

Filtering

Set filter:

    // disable send messages with .info & .debug severities
    logger.setfilter(.info);// disable send messages with .info & .debug severities 

Reset filter:

    logger.setfilter(null); 

License

MIT

About
Zig syslog client
Owner
g41797 (User)
Last Commit
2024-12-07
Latest Release
Latest Release Date
Created
2024-10-07