1SD_JOURNAL_PRINT(3) sd_journal_print SD_JOURNAL_PRINT(3)
2
3
4
6 sd_journal_print, sd_journal_printv, sd_journal_send, sd_journal_sendv,
7 sd_journal_perror, SD_JOURNAL_SUPPRESS_LOCATION - Submit log entries to
8 the journal
9
11 #include <systemd/sd-journal.h>
12
13 int sd_journal_print(int priority, const char *format, ...);
14
15 int sd_journal_printv(int priority, const char *format, va_list ap);
16
17 int sd_journal_send(const char *format, ...);
18
19 int sd_journal_sendv(const struct iovec *iov, int n);
20
21 int sd_journal_perror(const char *message);
22
24 sd_journal_print() may be used to submit simple, plain text log entries
25 to the system journal. The first argument is a priority value. This is
26 followed by a format string and its parameters, similar to printf(3) or
27 syslog(3). Note that currently the resulting message will be truncated
28 to LINE_MAX - 8. The priority value is one of LOG_EMERG, LOG_ALERT,
29 LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG, as
30 defined in syslog.h, see syslog(3) for details. It is recommended to
31 use this call to submit log messages in the application locale or
32 system locale and in UTF-8 format, but no such restrictions are
33 enforced. Note that log messages written using this function are
34 generally not expected to end in a new-line character. However, as all
35 trailing whitespace (including spaces, new-lines, tabulators and
36 carriage returns) are automatically stripped from the logged string, it
37 is acceptable to specify one (or more). Empty lines (after trailing
38 whitespace removal) are suppressed. On non-empty lines, leading
39 whitespace (as well as inner whitespace) is left unmodified.
40
41 sd_journal_printv() is similar to sd_journal_print() but takes a
42 variable argument list encapsulated in an object of type va_list (see
43 stdarg(3) for more information) instead of the format string. It is
44 otherwise equivalent in behavior.
45
46 sd_journal_send() may be used to submit structured log entries to the
47 system journal. It takes a series of format strings, each immediately
48 followed by their associated parameters, terminated by NULL. The
49 strings passed should be of the format "VARIABLE=value". The variable
50 name must be in uppercase and consist only of characters, numbers and
51 underscores, and may not begin with an underscore. (All assignments
52 that do not follow this syntax will be ignored.) The value can be of
53 any size and format. It is highly recommended to submit text strings
54 formatted in the UTF-8 character encoding only, and submit binary
55 fields only when formatting in UTF-8 strings is not sensible. A number
56 of well-known fields are defined, see systemd.journal-fields(7) for
57 details, but additional application defined fields may be used. A
58 variable may be assigned more than one value per entry. If this
59 function is used, trailing whitespace is automatically removed from
60 each formatted field.
61
62 sd_journal_sendv() is similar to sd_journal_send() but takes an array
63 of struct iovec (as defined in uio.h, see readv(3) for details) instead
64 of the format string. Each structure should reference one field of the
65 entry to submit. The second argument specifies the number of structures
66 in the array. sd_journal_sendv() is particularly useful to submit
67 binary objects to the journal where that is necessary. Note that this
68 function will not strip trailing whitespace of the passed fields, but
69 passes the specified data along unmodified. This is different from both
70 sd_journal_print() and sd_journal_send() described above, which are
71 based on format strings, and do strip trailing whitespace.
72
73 sd_journal_perror() is a similar to perror(3) and writes a message to
74 the journal that consists of the passed string, suffixed with ": " and
75 a human-readable representation of the current error code stored in
76 errno(3). If the message string is passed as NULL or empty string, only
77 the error string representation will be written, prefixed with nothing.
78 An additional journal field ERRNO= is included in the entry containing
79 the numeric error code formatted as decimal string. The log priority
80 used is LOG_ERR (3).
81
82 Note that sd_journal_send() is a wrapper around sd_journal_sendv() to
83 make it easier to use when only text strings shall be submitted. Also,
84 the following two calls are mostly equivalent:
85
86 sd_journal_print(LOG_INFO, "Hello World, this is PID %lu!", (unsigned long) getpid());
87
88 sd_journal_send("MESSAGE=Hello World, this is PID %lu!", (unsigned long) getpid(),
89 "PRIORITY=%i", LOG_INFO,
90 NULL);
91
92 Note that these calls implicitly add fields for the source file,
93 function name and code line where invoked. This is implemented with
94 macros. If this is not desired, it can be turned off by defining
95 SD_JOURNAL_SUPPRESS_LOCATION before including sd-journal.h.
96
97 syslog(3) and sd_journal_print() may largely be used interchangeably
98 functionality-wise. However, note that log messages logged via the
99 former take a different path to the journal server than the later, and
100 hence global chronological ordering between the two streams cannot be
101 guaranteed. Using sd_journal_print() has the benefit of logging source
102 code line, filenames, and functions as metadata along all entries, and
103 guaranteeing chronological ordering with structured log entries that
104 are generated via sd_journal_send(). Using syslog() has the benefit of
105 being more portable.
106
108 The five calls return 0 on success or a negative errno-style error
109 code. The errno(3) variable itself is not altered.
110
111 If systemd-journald(8) is not running (the socket is not present),
112 those functions do nothing, and also return 0.
113
115 All functions listed here are thread-safe and may be called in parallel
116 from multiple threads.
117
118 sd_journal_sendv() is "async signal safe" in the meaning of signal-
119 safety(7).
120
121 sd_journal_print, sd_journal_printv, sd_journal_send, and
122 sd_journal_perror are not async signal safe.
123
125 These APIs are implemented as a shared library, which can be compiled
126 and linked to with the libsystemd pkg-config(1) file.
127
129 systemd(1), sd-journal(3), sd_journal_stream_fd(3), syslog(3),
130 perror(3), errno(3), systemd.journal-fields(7), signal(7), socket(7)
131
132
133
134systemd 245 SD_JOURNAL_PRINT(3)