journalctl: add --exclude-identifier option

This commit is contained in:
Sergei Zhmylev 2023-12-11 22:23:47 +03:00 committed by Luca Boccassi
parent 42301b6d96
commit 25aa35d465
8 changed files with 66 additions and 5 deletions

View file

@ -362,6 +362,18 @@
<xi:include href="version-info.xml" xpointer="v217"/></listitem> <xi:include href="version-info.xml" xpointer="v217"/></listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>-T</option></term>
<term><option>--exclude-identifier=<replaceable>SYSLOG_IDENTIFIER</replaceable></option></term>
<listitem><para>Exclude messages for the specified syslog identifier
<replaceable>SYSLOG_IDENTIFIER</replaceable>.</para>
<para>This parameter can be specified multiple times.</para>
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-p</option></term> <term><option>-p</option></term>
<term><option>--priority=</option></term> <term><option>--priority=</option></term>

View file

@ -46,9 +46,9 @@ _journalctl() {
--show-cursor --dmesg -k --pager-end -e -r --reverse --show-cursor --dmesg -k --pager-end -e -r --reverse
--utc -x --catalog --no-full --force --dump-catalog --utc -x --catalog --no-full --force --dump-catalog
--flush --rotate --sync --no-hostname -N --fields' --flush --rotate --sync --no-hostname -N --fields'
[ARG]='-b --boot -D --directory --file -F --field -t --identifier --facility [ARG]='-b --boot -D --directory --file -F --field -t --identifier
-M --machine -o --output -u --unit --user-unit -p --priority -T --exclude-identifier --facility -M --machine -o --output
--root --case-sensitive' -u --unit --user-unit -p --priority --root --case-sensitive'
[ARGUNKNOWN]='-c --cursor --interval -n --lines -S --since -U --until [ARGUNKNOWN]='-c --cursor --interval -n --lines -S --since -U --until
--after-cursor --cursor-file --verify-key -g --grep --after-cursor --cursor-file --verify-key -g --grep
--vacuum-size --vacuum-time --vacuum-files --output-fields' --vacuum-size --vacuum-time --vacuum-files --output-fields'
@ -107,7 +107,7 @@ _journalctl() {
--user-unit) --user-unit)
comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null) comps=$(journalctl -F '_SYSTEMD_USER_UNIT' 2>/dev/null)
;; ;;
--identifier|-t) --identifier|-t|--exclude-identifier|-T)
comps=$(journalctl -F 'SYSLOG_IDENTIFIER' 2>/dev/null) comps=$(journalctl -F 'SYSLOG_IDENTIFIER' 2>/dev/null)
;; ;;
--case-sensitive) --case-sensitive)

View file

@ -122,6 +122,7 @@ static bool arg_force = false;
static usec_t arg_since = 0, arg_until = 0; static usec_t arg_since = 0, arg_until = 0;
static bool arg_since_set = false, arg_until_set = false; static bool arg_since_set = false, arg_until_set = false;
static char **arg_syslog_identifier = NULL; static char **arg_syslog_identifier = NULL;
static char **arg_exclude_identifier = NULL;
static char **arg_system_units = NULL; static char **arg_system_units = NULL;
static char **arg_user_units = NULL; static char **arg_user_units = NULL;
static const char *arg_field = NULL; static const char *arg_field = NULL;
@ -146,6 +147,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_facilities, set_freep); STATIC_DESTRUCTOR_REGISTER(arg_facilities, set_freep);
STATIC_DESTRUCTOR_REGISTER(arg_verify_key, freep); STATIC_DESTRUCTOR_REGISTER(arg_verify_key, freep);
STATIC_DESTRUCTOR_REGISTER(arg_syslog_identifier, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_syslog_identifier, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_exclude_identifier, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_system_units, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_system_units, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_user_units, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_user_units, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_root, freep); STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
@ -380,6 +382,8 @@ static int help(void) {
" -u --unit=UNIT Show logs from the specified unit\n" " -u --unit=UNIT Show logs from the specified unit\n"
" --user-unit=UNIT Show logs from the specified user unit\n" " --user-unit=UNIT Show logs from the specified user unit\n"
" -t --identifier=STRING Show entries with the specified syslog identifier\n" " -t --identifier=STRING Show entries with the specified syslog identifier\n"
" -T --exclude-identifier=STRING\n"
" Hide entries with the specified syslog identifier\n"
" -p --priority=RANGE Show entries with the specified priority\n" " -p --priority=RANGE Show entries with the specified priority\n"
" --facility=FACILITY... Show entries with the specified facilities\n" " --facility=FACILITY... Show entries with the specified facilities\n"
" -g --grep=PATTERN Show entries with MESSAGE matching PATTERN\n" " -g --grep=PATTERN Show entries with MESSAGE matching PATTERN\n"
@ -519,6 +523,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
{ "header", no_argument, NULL, ARG_HEADER }, { "header", no_argument, NULL, ARG_HEADER },
{ "identifier", required_argument, NULL, 't' }, { "identifier", required_argument, NULL, 't' },
{ "exclude-identifier", required_argument, NULL, 'T' },
{ "priority", required_argument, NULL, 'p' }, { "priority", required_argument, NULL, 'p' },
{ "facility", required_argument, NULL, ARG_FACILITY }, { "facility", required_argument, NULL, ARG_FACILITY },
{ "grep", required_argument, NULL, 'g' }, { "grep", required_argument, NULL, 'g' },
@ -564,7 +569,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0); assert(argc >= 0);
assert(argv); assert(argv);
while ((c = getopt_long(argc, argv, "hefo:aln::qmb::kD:p:g:c:S:U:t:u:NF:xrM:i:", options, NULL)) >= 0) while ((c = getopt_long(argc, argv, "hefo:aln::qmb::kD:p:g:c:S:U:t:T:u:NF:xrM:i:", options, NULL)) >= 0)
switch (c) { switch (c) {
@ -958,6 +963,12 @@ static int parse_argv(int argc, char *argv[]) {
return log_oom(); return log_oom();
break; break;
case 'T':
r = strv_extend(&arg_exclude_identifier, optarg);
if (r < 0)
return log_oom();
break;
case 'u': case 'u':
r = strv_extend(&arg_system_units, optarg); r = strv_extend(&arg_system_units, optarg);
if (r < 0) if (r < 0)
@ -1572,6 +1583,19 @@ static int add_syslog_identifier(sd_journal *j) {
return 0; return 0;
} }
static int add_exclude_identifier(sd_journal *j) {
_cleanup_set_free_ Set *excludes = NULL;
int r;
assert(j);
r = set_put_strdupv(&excludes, arg_exclude_identifier);
if (r < 0)
return r;
return set_free_and_replace(j->exclude_syslog_identifiers, excludes);
}
#if HAVE_GCRYPT #if HAVE_GCRYPT
static int format_journal_url( static int format_journal_url(
const void *seed, const void *seed,
@ -2408,6 +2432,10 @@ static int run(int argc, char *argv[]) {
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to add filter for syslog identifiers: %m"); return log_error_errno(r, "Failed to add filter for syslog identifiers: %m");
r = add_exclude_identifier(j);
if (r < 0)
return log_error_errno(r, "Failed to add exclude filter for syslog identifiers: %m");
r = add_priorities(j); r = add_priorities(j);
if (r < 0) if (r < 0)
return r; return r;

View file

@ -87,6 +87,7 @@ struct sd_journal {
uint64_t current_field; uint64_t current_field;
Match *level0, *level1, *level2; Match *level0, *level1, *level2;
Set *exclude_syslog_identifiers;
uint64_t origin_id; uint64_t origin_id;

View file

@ -2327,6 +2327,8 @@ _public_ void sd_journal_close(sd_journal *j) {
hashmap_free_free(j->errors); hashmap_free_free(j->errors);
set_free(j->exclude_syslog_identifiers);
free(j->path); free(j->path);
free(j->prefix); free(j->prefix);
free(j->namespace); free(j->namespace);

View file

@ -523,6 +523,9 @@ static int output_short(
return 0; return 0;
} }
if (identifier && set_contains(j->exclude_syslog_identifiers, identifier))
return 0;
if (!(flags & OUTPUT_SHOW_ALL)) if (!(flags & OUTPUT_SHOW_ALL))
strip_tab_ansi(&message, &message_len, highlight_shifted); strip_tab_ansi(&message, &message_len, highlight_shifted);

View file

@ -0,0 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
[Unit]
Description=Verbose successful service
[Service]
ExecStart=/bin/echo success

View file

@ -107,6 +107,15 @@ systemctl start silent-success
journalctl --sync journalctl --sync
[[ -z "$(journalctl -b -q -u silent-success.service)" ]] [[ -z "$(journalctl -b -q -u silent-success.service)" ]]
# Test syslog identifiers exclusion
systemctl start verbose-success
journalctl --sync
[[ -n "$(journalctl -b -q -u verbose-success.service -t systemd)" ]]
[[ -n "$(journalctl -b -q -u verbose-success.service -t echo)" ]]
[[ -n "$(journalctl -b -q -u verbose-success.service -T systemd)" ]]
[[ -n "$(journalctl -b -q -u verbose-success.service -T echo)" ]]
[[ -z "$(journalctl -b -q -u verbose-success.service -T echo -T '(echo)' -T systemd -T '(systemd)' -T systemd-executor)" ]]
# Exercise the matching machinery # Exercise the matching machinery
SYSTEMD_LOG_LEVEL=debug journalctl -b -n 1 /dev/null /dev/zero /dev/null /dev/null /dev/null SYSTEMD_LOG_LEVEL=debug journalctl -b -n 1 /dev/null /dev/zero /dev/null /dev/null /dev/null
journalctl -b -n 1 /bin/true /bin/false journalctl -b -n 1 /bin/true /bin/false