clogger · Overview
clogger
NAME
clogger - overview of the structured logger
SYNOPSIS
#include <ccollections/clogger.h>
DESCRIPTION
The module writes structured log records. A record is a sequence of
key=value pairs, in this order:
- a fixed header with the time, the level, the process, the thread
and the source location;
- the persistent fields of the logger;
- the message.
There are three formats: logfmt (the default), NDJSON and RFC 5424
syslog; see clog_set_format(3).
A logger writes to one of these outputs:
- a file descriptor that the caller owns (clog_create_from_fd_mp(3));
- a file that the logger opens itself (clog_create_from_file_path_mp(3)),
with optional size rotation, time rotation and gzip compression of
the rotated files.
Either kind of logger can log synchronously, or hand its writes to a
writer thread of its own (async mode, through a clog_async_cfg).
Handles
A clog is an opaque value handle, a uint64_t, and not a pointer, so never cast it to or from void *. CLOG_NULL, which is 0, means "no logger", and the open functions and clog_derive(3) give it on failure. You can test for it with if (!lg) or with lg == CLOG_NULL. Every function that takes a handle needs a live one. Given CLOG_NULL, a handle whose logger is closed or a stale handle, it stops the process with a diagnostic, and such a handle is never ignored silently. clog_derive(3) makes a second handle on the same output. A root logger and its derived loggers share the output target, the format, the syslog facility, the rotation, the async writer thread, the allocator and the lock that serialises the writes, while each handle has a minimum level and fields of its own.
Levels
CLOG_TRACE 0 The finest detail.
CLOG_DEBUG 1
CLOG_INFO 2 The usual minimum in production.
CLOG_WARN 3
CLOG_ERROR 4 Adds a backtrace.
CLOG_ALERT 5 An operator must act immediately (RFC 5424 severity
1). Adds a backtrace.
CLOG_FATAL 6 Adds a backtrace, then stops the process with
exit(EXIT_FAILURE). Ignores the minimum level.
CLOG_OFF 7 As a minimum level, stops all output except
CLOG_FATAL.
A record below the minimum level of the logger is dropped silently.
Each of the macros clog_trace(3) to clog_fatal(3) logs at one level,
and clog_set_level(3) changes the minimum at any time.
Record contents
The proc key has the form <name>(<pid>):<tname>(<tid>). The first part is the basename of the executable with the process ID, and the second is the name and the ID of the thread that logged. After ccol_set_thread_name(3) renames a thread, its records show the new name. In logfmt, a field value or a message goes in double quotes when it holds a space, an '=', a '"', a backslash, a control character or a byte that is not well-formed UTF-8, and those characters are escaped with a backslash (\", \\, \n, \r, \t, \xNN, or \u0080 to \u009f for a C1 control), so a message or a value can never split a record across lines. In every format each byte that is not part of a well-formed UTF-8 sequence becomes U+FFFD, which keeps every record valid UTF-8. JSON and syslog have their own escapes, described in clog_set_format(3) and clog_set_field(3). A record has a hard size limit of 16 MiB, and the library never writes a truncated or malformed record. A record above the limit is replaced with a well-formed record in the same format that keeps the header and says "log record too large to emit", and an allocation failure while a record is built gives a record that says "log record dropped", so a record never disappears without a trace. A "log record truncated: ..." record at CLOG_ERROR names the records that a write could not deliver. If the output refuses that record as well, as a full disk does, the logger writes it in front of the next write that the output accepts, at the next flush of an async logger, or at exit when nothing is written again, and it never writes a record onto the fragment that a cut write left; see clog_create_from_fd_mp(3).
Backtraces
clog_error(3), clog_alert(3) and clog_fatal(3) add a backtrace, which goes out as continuation lines with a tab indent in logfmt, as a "bt" array in JSON and as one message per frame in syslog. Link the program with -rdynamic to get symbol names. The capture works on platforms with execinfo.h (glibc, FreeBSD, macOS), and when it fails a marker takes the place of the frames ("#error backtrace unavailable" in logfmt, a "bt_error" key in JSON), so a missing backtrace is always visible.
Rotation
A rotating logger renames, compresses and deletes only its own rotated generations, and clog_create_from_file_path_mp(3) gives the exact test. A name whose timestamp is more than 30 days above both the clock and the newest name the logger made is not a generation, so the logger never renames, compresses or deletes it, and a CLOG_WARN record names it. When the system clock steps back by more than 30 days, a logger that opens the file after the step therefore leaves the generations written before the step unmanaged until the clock comes back within 30 days of their timestamps; until then the operator removes or renames them.
Memory management
clog_create_from_fd_mp(3) and clog_create_from_file_path_mp(3) take an optional ccol_memmgmt_procs, and the root logger and every logger derived from it allocate the shared output state, the write buffers, the message buffers and the fields through it. Two allocations belong to the library and not to one logger, so they don't go through it: The symbol text of a backtrace, which backtrace_symbols(3) allocates with malloc(3), and the small identity cache (program name, process and thread ID, thread name) that each thread fills with malloc(3) on its first record. Every logger of the process shares that cache, and it is freed when the thread exits. All four function pointers must be set. The library copies the struct, so you can free yours after the open.
Thread safety
Every function can be called from many threads at the same time on handles that stay open. One lock serialises the writes and the rotation of an output target, so the lines of two threads never mix and a backtrace always follows its own record. The level and the fields of a logger are synchronised apart from that lock. clog_close(3) is the exception, as it is for every handle type of the library: While it runs, no other thread may use or close the same handle. Closing one handle while a sibling handle (the root or another derived logger) logs is fine.
Process exit
The library registers an exit handler when it loads. When the process ends with exit(3) or a return from main(), that handler writes the records that each async logger holds and completes the queued compressions, all within a budget of 5 seconds, and clog_fatal(3) uses the same drain. _exit(2), a signal and abort(3) run no exit handler, so if the queued records matter, call clog_flush(3) or clog_close(3) first. clog_create_from_fd_mp(3) has the details.
fork(2)
There are two supported ways to use fork(2) with this module: - call fork(2) before you open a logger; - open a logger, and then call fork(2) and exec(3) immediately. A child that goes on without exec(3) while a logger from before the fork is live is not supported. As a defence in depth, an inherited handle still works in such a child. An async logger switches to synchronous writes there, because its writer thread does not exist in the child. This fallback is part of the fork protection of the library, which a build with -DCCOL_FORK_SAFETY_REQUIRED=0 leaves out, and a process from such a build must not fork while it holds a live async logger.
SIGPIPE and SIGXFSZ
A write to a pipe or a socket whose reader is gone causes SIGPIPE, and a write to a regular file at the file size limit of the process (RLIMIT_FSIZE) causes SIGXFSZ. With the default disposition either signal ends the process. By default the library keeps both signals from ending the process; for SIGXFSZ, the open of a regular file sets its disposition to SIG_IGN while it is still SIG_DFL. clog_set_signal_policy(3) describes how, and also the policy that leaves both signals to the application, which then has to ignore SIGXFSZ or block it in every thread that can write a log file.
SEE ALSO
clog_create_from_fd_mp(3), clog_create_from_file_path_mp(3), clog_derive(3), clog_close(3), clog_flush(3), clog_set_level(3), clog_get_level(3), clog_set_format(3), clog_get_format(3), clog_set_facility(3), clog_get_facility(3), clog_set_field(3), clog_remove_field(3), clog_clear_fields(3), clog_info(3), clog_error(3), clog_fatal(3), ccol_set_thread_name(3), clog_set_signal_policy(3)