C Collections

clogger · Section 3

clog_set_field

clog_set_field - attach or update a persistent structured field

C Collections (libccollections, -lccollections)

#include <ccollections/clogger.h>
 
ccol_retval clog_set_field(clog logger, const char *key,
                             const char *value);

Attaches key with value as a persistent field.  The field then appears
in every later log line of this logger, between the func= and msg=
tokens:
 
  ts=... level=INFO proc=prog(pid):tname(tid) src=... func=... key=value msg=...
 
Both key and value are copied, so you can free or change your own copies
after the call and the logger won't notice.
 
If the key already exists it gets the new value, and otherwise a new
field is added.  A call that fails changes nothing: A new key is not
added and an existing key keeps its value.
 
Every character of a key must be a printable US-ASCII character
(0x21-0x7e) other than '=', ']', '"' and '\\'.  A key is refused when
it is NULL or empty, or when it holds one of those forbidden characters,
whitespace, an ASCII control character or any byte that is not ASCII.
A key is refused as well when it names one of the fixed tokens that
every log line already carries (ts, level, proc, src, func, msg, bt and
bt_error), because it would make a duplicate key= token in logfmt, a
duplicate JSON key, or a duplicate RFC 5424 SD-PARAM-NAME inside one
SD-ELEMENT.
 
A value that holds a space, an '=', a '"', a '\\', a control character
or a byte that is not well-formed UTF-8 is double-quoted in the logfmt
output, and those characters are escaped with a backslash:
  - '"' -> \"
  - '\\' -> \\
  - ASCII control chars -> \n, \r, \t, or \xNN
  - C1 controls (U+0080 to U+009F) -> \u0080 to \u009f
  - a byte that is not part of a well-formed UTF-8 sequence -> U+FFFD
 
The exact escape syntax depends on the output format (see
clog_set_format(3)).  JSON uses \uNNNN Unicode escapes, and RFC 5424
syslog escapes a '"', a '\\' and a ']' besides a control character.
In every format, each byte of a value that is not well-formed UTF-8 is
replaced with the Unicode replacement character (U+FFFD) instead of
going out raw, so every record is valid UTF-8, which JSON text and an
RFC 5424 SD-PARAM-VALUE both require.  Every format also escapes a C1
control, because a terminal can read it as the start of a control
sequence.
An RFC 5424 SD-PARAM-NAME longer than 32 characters becomes its first
23 characters, a '~', and a hash of the full key in 8 hex digits,
instead of a plain 32-character prefix, so two different long keys with
the same prefix get different SD-PARAM-NAMEs.
In every format a control character is always neutralized, a literal
newline and a carriage return included, so a field value can never
split one record over several lines.

logger  Logger handle.
key     Field name, copied by the call.  It must follow the rules
        above.
value   Field value, copied by the call.  It must not be NULL, and
        it may be any text.

ccol_success            The field is set.
ccol_invalid_args       key or value is NULL, or key is empty, holds
                        a character that is not allowed, or names a
                        fixed token.  Nothing changes.
ccol_not_enough_memory  The copy of the key or the value cannot be
                        allocated.  Nothing changes.

Thread-safe, because the mutex of the logger protects the call.
Every line that goes out visits each attached field once, so keep the
number of fields small if logging is on a hot path.
Asserts when logger is CLOG_NULL, stale or already closed.

clog_remove_field(3), clog_clear_fields(3)