Skip to content

Commit 2ce9e8f

Browse files
committed
Add option to skip message with certain syslog level
1 parent e4e05af commit 2ce9e8f

7 files changed

+95
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Optional settings
108108
ExcludeSyslogFacility=
109109
A list of strings. Specifies the syslog facilities to skip forwarding. Possible values are: "kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news", "uucp", "cron", "authpriv", "ftp", "ntp", "security", "console", "solaris-cron", "local0", "local1", "local2", "local3", "local4", "local5", "local6" and "local7".
110110

111+
ExcludeSyslogLevel=
112+
A list of strings. Specifies the syslog levels to skip forwarding. Possible values are: "emerg", "alert", "crit", "err", "warning", "notice", "info" and "debug".
113+
111114
**EXAMPLE**
112115

113116
Example 1.UDP Multicast
@@ -149,14 +152,15 @@ UseSysLogStructuredData=yes
149152
UseSysLogMsgId=yes
150153
```
151154

152-
Example 5. Skipping messages with facility AUTH or AUTHPRIV
155+
Example 5. Skipping messages with facility AUTH or AUTHPRIV and messages with level DEBUG
153156

154157
``` toml
155158
[Network]
156159
Address=192.168.8.101:514
157160
#Protocol=udp
158161
LogFormat=rfc3339
159162
ExcludeSyslogFacility=auth authpriv
163+
ExcludeSyslogLevel=debug
160164
```
161165

162166
Example 6. TLS with certificate authentocation mode

conf/netlogd.conf.in

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
#NoDelay=no
1818
#SendBuffer=
1919
#ExcludeSyslogFacility=
20+
#ExcludeSyslogLevel=

src/netlog/netlog-conf.c

+42
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,48 @@ int config_parse_syslog_facility(const char *unit,
240240
return 0;
241241
}
242242

243+
int config_parse_syslog_level(const char *unit,
244+
const char *filename,
245+
unsigned line,
246+
const char *section,
247+
unsigned section_line,
248+
const char *lvalue,
249+
int ltype,
250+
const char *rvalue,
251+
void *data,
252+
void *userdata) {
253+
Manager *m = userdata;
254+
uint8_t val = 0;
255+
int r;
256+
257+
assert(filename);
258+
assert(lvalue);
259+
assert(rvalue);
260+
assert(data);
261+
assert(m);
262+
263+
for (const char *p = rvalue;;) {
264+
_cleanup_free_ char *word = NULL;
265+
266+
r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX);
267+
if (r < 0) {
268+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue);
269+
return 0;
270+
}
271+
if (r == 0)
272+
break;
273+
274+
r = syslog_level_from_string(word);
275+
if (r < 0) {
276+
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syslog level '%s', ignoring", word);
277+
} else
278+
val |= UINT8_C(1) << r;
279+
}
280+
281+
m->excluded_syslog_levels = val;
282+
return 0;
283+
}
284+
243285
int manager_parse_config_file(Manager *m) {
244286
int r;
245287

src/netlog/netlog-conf.h

+11
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,15 @@ int config_parse_syslog_facility(const char *unit,
7373
void *data,
7474
void *userdata);
7575

76+
int config_parse_syslog_level(const char *unit,
77+
const char *filename,
78+
unsigned line,
79+
const char *section,
80+
unsigned section_line,
81+
const char *lvalue,
82+
int ltype,
83+
const char *rvalue,
84+
void *data,
85+
void *userdata);
86+
7687
int manager_parse_config_file(Manager *m);

src/netlog/netlog-gperf.gperf

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ Network.KeepAliveProbes, config_parse_unsigned, 0, off
3333
Network.NoDelay, config_parse_bool, 0, offsetof(Manager, no_delay)
3434
Network.SendBuffer, config_parse_iec_size, 0, offsetof(Manager, send_buffer)
3535
Network.ExcludeSyslogFacility, config_parse_syslog_facility, 0, offsetof(Manager, excluded_syslog_facilities)
36+
Network.ExcludeSyslogLevel, config_parse_syslog_level, 0, offsetof(Manager, excluded_syslog_levels)

src/netlog/netlog-manager.c

+17
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ static const char *const syslog_facility_table[_SYSLOG_FACILITY_MAX] = {
7373

7474
DEFINE_STRING_TABLE_LOOKUP(syslog_facility, SysLogFacility);
7575

76+
static const char *const syslog_level_table[_SYSLOG_LEVEL_MAX] = {
77+
[SYSLOG_LEVEL_EMERGENCY] = "emerg",
78+
[SYSLOG_LEVEL_ALERT] = "alert",
79+
[SYSLOG_LEVEL_CRITICAL] = "crit",
80+
[SYSLOG_LEVEL_ERROR] = "err",
81+
[SYSLOG_LEVEL_WARNING] = "warning",
82+
[SYSLOG_LEVEL_NOTICE] = "notice",
83+
[SYSLOG_LEVEL_INFORMATIONAL] = "info",
84+
[SYSLOG_LEVEL_DEBUG] = "debug",
85+
};
86+
87+
DEFINE_STRING_TABLE_LOOKUP(syslog_level, SysLogLevel);
88+
7689
typedef struct ParseFieldVec {
7790
const char *field;
7891
size_t field_len;
@@ -222,6 +235,10 @@ static int manager_read_journal_input(Manager *m) {
222235
r = safe_atou(priority, &sev);
223236
if (r < 0)
224237
log_debug("Failed to parse syslog priority: %s", priority);
238+
else if (sev < _SYSLOG_LEVEL_MAX && ((UINT8_C(1) << sev) & m->excluded_syslog_levels)) {
239+
log_debug("Skipping message with excluded syslog level %s.", syslog_level_to_string(sev));
240+
return 0;
241+
}
225242

226243
if (sev > LOG_DEBUG)
227244
sev = JOURNAL_DEFAULT_SEVERITY;

src/netlog/netlog-manager.h

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ typedef enum SysLogFacility {
5959
_SYSLOG_FACILITY_INVALID = -EINVAL,
6060
} SysLogFacility;
6161

62+
/* RFC 5424 Section 6.2.1 */
63+
typedef enum SysLogLevel {
64+
SYSLOG_LEVEL_EMERGENCY = 0,
65+
SYSLOG_LEVEL_ALERT = 1,
66+
SYSLOG_LEVEL_CRITICAL = 2,
67+
SYSLOG_LEVEL_ERROR = 3,
68+
SYSLOG_LEVEL_WARNING = 4,
69+
SYSLOG_LEVEL_NOTICE = 5,
70+
SYSLOG_LEVEL_INFORMATIONAL = 6,
71+
SYSLOG_LEVEL_DEBUG = 7,
72+
_SYSLOG_LEVEL_MAX,
73+
_SYSLOG_LEVEL_INVALID = -EINVAL,
74+
} SysLogLevel;
75+
6276
typedef struct Manager Manager;
6377

6478
struct Manager {
@@ -89,6 +103,7 @@ struct Manager {
89103
char *server_name;
90104

91105
uint32_t excluded_syslog_facilities;
106+
uint8_t excluded_syslog_levels;
92107

93108
/* journal */
94109
int journal_watch_fd;
@@ -159,3 +174,6 @@ int log_format_from_string(const char *s) _pure_;
159174

160175
const char *syslog_facility_to_string(SysLogFacility v) _const_;
161176
SysLogFacility syslog_facility_from_string(const char *s) _pure_;
177+
178+
const char *syslog_level_to_string(SysLogLevel v) _const_;
179+
SysLogLevel syslog_level_from_string(const char *s) _pure_;

0 commit comments

Comments
 (0)