clogger · Section 3
clog_set_format
NAME
clog_set_format - change the output format at runtime
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/clogger.h> ccol_retval clog_set_format(clog logger, clog_format fmt);
DESCRIPTION
Updates the output format of the shared backing store. The format
lives in the shared store and not in the struct of each logger, so
the change is visible at once to every logger handle that writes to
the same file descriptor, the root logger and every derived logger.
There are three formats.
CLOG_FMT_LOGFMT (0)
The default. Every record is one logfmt line:
ts=<ISO-8601-UTC> level=<L> proc=<name>(<pid>):<tname>(<tid>)
src=<file>:<line> func=<fn> [fields] msg=<text>
clog_error, clog_alert and clog_fatal append backtrace
continuation lines after the main record, each indented by a tab.
In proc, <tname> and <tid> are the name and the ID of the thread
that wrote the record. Each thread reads them once, the first
time it logs, and ccol_set_thread_name(3) renames a thread and
updates that name. A value or a message that holds a space, an
'=', a '"', a backslash, a control character or a byte that is
not well-formed UTF-8 goes in double quotes. A control character
becomes \n, \r, \t or \xNN, a C1 control (U+0080 to U+009F)
becomes \u0080 to \u009f, and each byte that is not part of a
well-formed UTF-8 sequence becomes U+FFFD, so every record is
valid UTF-8.
CLOG_FMT_JSON (1)
The library writes each record as one NDJSON object followed by a
newline:
{"ts":"...","level":"...","proc":"name(pid):tname(tid)",
"src":"file:N","func":"...", [fields,] "msg":"..."}
Each structured field becomes a top-level JSON key. For
clog_error, clog_alert and clog_fatal the library embeds the
backtrace as a "bt" array of strings inside the same JSON object.
Before it embeds a string value (the message, and the key and the
value of every field), it checks that value as UTF-8 and replaces
a byte or a sequence of bytes that is not well-formed UTF-8, one
invalid byte at a time, with the Unicode replacement character
(U+FFFD). It writes a control character, the C1 controls U+0080
to U+009F included, as a \uNNNN escape (\n, \r and \t for
those three). So a message or a field value built from arbitrary
or binary data can never give a record with invalid Unicode
content.
CLOG_FMT_SYSLOG (2)
The RFC 5424 syslog format, which only a logger on an fd offers
(one from clog_create_from_fd or clog_create_from_fd_mp). On a
logger that sits on a file, clog_set_format() with CLOG_FMT_SYSLOG
returns ccol_invalid_args and the format stays as it is.
The library writes each record as one line:
<PRI>1 TIMESTAMP HOSTNAME APP-NAME PID MSGID [ccol ...] MSG\n
PRI = (facility * 8) + RFC 5424 severity (mapped from
clog_level: TRACE=7, DEBUG=7, INFO=6, WARN=4,
ERROR=3, ALERT=1, FATAL=0)
TIMESTAMP = ISO 8601 UTC (same format as logfmt/JSON)
HOSTNAME = the result of gethostname(2). The library keeps
only the bytes that are RFC 5424 PRINTUSASCII
(0x21-0x7e) and drops every other byte, a space
and a control character included, without
truncating the value at that point. It writes
"-" when this leaves no byte at all, or when
gethostname(2) itself fails.
APP-NAME = the name of the executable (the basename of
argv[0]), filtered to PRINTUSASCII in the same
way. The library writes "-" when this leaves no
byte at all.
PID = getpid(2)
MSGID = level string ("INFO", "ERROR", etc.)
[ccol ...] = RFC 5424 SD-ELEMENT with SD-ID "ccol" carrying
proc="<name>(<pid>):<tname>(<tid>)",
src="<file>:<line>", func="<fn>", and any fields
that clog_set_field() attached
MSG = the formatted message text
In MSG, and in every value of proc, src, func and a field, the
library escapes a control character, a literal newline and a
carriage return included, with a backslash as \n, \r, \t or
\xNN, and a C1 control (U+0080 to U+009F) as \u0080 to
\u009f. It writes a backslash itself as \\, so the escaped
text reads back without ambiguity: A real newline appears as \n,
and the two characters '\' and 'n' appear as \\n. logfmt uses
the same scheme for its own values. In MSG and in every
SD-PARAM-VALUE (the values of proc, src, func and the fields), the
library passes a well-formed UTF-8 sequence through unchanged and
replaces each byte that is not part of one with U+FFFD, as the
JSON format does, because RFC 5424 requires an SD-PARAM-VALUE to
be UTF-8. MSG carries no byte order mark, so it is the MSG-ANY
form of RFC 5424, whose bytes a receiver must not assume to be
UTF-8, even though the library writes only valid UTF-8 there.
HOSTNAME and APP-NAME can hold no byte that needs this escape,
because the filter above takes both down to PRINTUSASCII. So the
content of a record can never split that record across several
lines.
clog_set_facility() sets the facility, which defaults to
CLOG_SYSLOG_USER. The library writes a backtrace (which
clog_error, clog_alert and clog_fatal produce) as separate syslog
messages, one for each frame, and each of them carries the same
PRI, TIMESTAMP and MSGID as the original record. The MSG of a
frame message is "#<n> " followed by the text of frame n, escaped
with a backslash in the same way as MSG. Every frame shares the
TIMESTAMP of the original record instead of carrying its own write
time, and that is what lets a reader match a record with its own
backtrace frames.
Connect the fd to a syslog daemon before you call
clog_set_format(); on Linux that is usually a SOCK_DGRAM Unix
socket at /dev/log. Keep each message under 2 KiB to stay inside
the usual datagram limits of syslogd.
PARAMETERS
logger Logger handle. fmt CLOG_FMT_LOGFMT, CLOG_FMT_JSON, or CLOG_FMT_SYSLOG.
RETURN VALUE
ccol_success The format is set.
ccol_invalid_args clog_format does not name fmt (such as 9), or fmt
is CLOG_FMT_SYSLOG and the logger sits on a file.
The format stays as it is, and clog_get_format(3)
keeps reporting it.
NOTES
Thread-safe, because the mutex of the shared backing store protects the call. Asserts when logger is CLOG_NULL, stale or already closed.
SEE ALSO
clog_get_format(3), clog_set_facility(3), clog_create_from_fd_mp(3), clog_create_from_file_path_mp(3), clog_derive(3), ccol_set_thread_name(3)