diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-05-13 14:58:11 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-05-13 14:58:11 +0100 |
commit | 137ceb2eb102229ebbbd06bef2681d689d035647 (patch) | |
tree | bf1ad1d05e98bdff87589a7eb56945564bd9f419 /src/libserver/logger | |
parent | 967e5ee83cb1e7bb17dde33686a48d27617b1e0c (diff) | |
download | rspamd-137ceb2eb102229ebbbd06bef2681d689d035647.tar.gz rspamd-137ceb2eb102229ebbbd06bef2681d689d035647.zip |
[Feature] Allow to log severity level explicitly
Diffstat (limited to 'src/libserver/logger')
-rw-r--r-- | src/libserver/logger/logger.c | 24 | ||||
-rw-r--r-- | src/libserver/logger/logger_console.c | 25 | ||||
-rw-r--r-- | src/libserver/logger/logger_file.c | 25 | ||||
-rw-r--r-- | src/libserver/logger/logger_private.h | 1 |
4 files changed, 63 insertions, 12 deletions
diff --git a/src/libserver/logger/logger.c b/src/libserver/logger/logger.c index 0ab6c4789..0c7400ebf 100644 --- a/src/libserver/logger/logger.c +++ b/src/libserver/logger/logger.c @@ -999,4 +999,28 @@ rspamd_log_line_need_escape (const guchar *src, gsize srclen) } return n; +} + +const gchar * +rspamd_get_log_severity_string (gint level_flags) +{ + unsigned int bitnum; + static const char *level_strs[G_LOG_LEVEL_USER_SHIFT] = { + "", /* G_LOG_FLAG_RECURSION */ + "", /* G_LOG_FLAG_FATAL */ + "crit", + "error", + "warn", + "notice", + "info", + "debug" + }; + level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION|G_LOG_FLAG_FATAL); +#ifdef __GNUC__ + /* We assume gcc >= 3 and clang >= 5 anyway */ + bitnum = __builtin_ffs (level_flags) - 1; +#else + bitnum = ffs (level_flags) - 1; +#endif + return level_strs[bitnum]; }
\ No newline at end of file diff --git a/src/libserver/logger/logger_console.c b/src/libserver/logger/logger_console.c index c7857bdfe..3e142fe27 100644 --- a/src/libserver/logger/logger_console.c +++ b/src/libserver/logger/logger_console.c @@ -28,6 +28,7 @@ static const gchar lf_chr = '\n'; struct rspamd_console_logger_priv { gint fd; gint crit_fd; + gboolean log_severity; gboolean log_color; gboolean log_rspamadm; gboolean log_tty; @@ -63,6 +64,7 @@ rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, priv = g_malloc0 (sizeof (*priv)); priv->log_color = (logger->flags & RSPAMD_LOG_FLAG_COLOR); + priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); priv->log_rspamadm = (logger->flags & RSPAMD_LOG_FLAG_RSPAMADM); if (priv->log_rspamadm) { @@ -224,12 +226,23 @@ rspamd_log_console_log (const gchar *module, const gchar *id, } else { if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - rspamd_log->process_type); + if (priv->log_severity) { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string (level_flags), + rspamd_log->pid, + rspamd_log->process_type); + } + else { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + rspamd_log->process_type); + } } else { r += rspamd_snprintf (tmpbuf + r, sizeof (tmpbuf) - r, diff --git a/src/libserver/logger/logger_file.c b/src/libserver/logger/logger_file.c index 3d5cc148b..81c44f8e7 100644 --- a/src/libserver/logger/logger_file.c +++ b/src/libserver/logger/logger_file.c @@ -36,6 +36,7 @@ struct rspamd_file_logger_priv { gboolean throttling; gchar *log_file; gboolean is_buffered; + gboolean log_severity; time_t throttling_time; guint32 repeats; guint64 last_line_cksum; @@ -360,6 +361,7 @@ rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, priv->log_file = g_strdup (cfg->log_file); } + priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); priv->fd = rspamd_try_open_log_fd (logger, priv, uid, gid, err); if (priv->fd == -1) { @@ -506,12 +508,23 @@ rspamd_log_file_log (const gchar *module, const gchar *id, r = 0; if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - cptype); + if (priv->log_severity) { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string (level_flags), + rspamd_log->pid, + cptype); + } + else { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + cptype); + } } else { r += rspamd_snprintf (tmpbuf + r, diff --git a/src/libserver/logger/logger_private.h b/src/libserver/logger/logger_private.h index cb06abe7c..92bdfdba7 100644 --- a/src/libserver/logger/logger_private.h +++ b/src/libserver/logger/logger_private.h @@ -168,6 +168,7 @@ bool rspamd_log_console_log (const gchar *module, const gchar *id, gsize mlen, rspamd_logger_t *rspamd_log, gpointer arg); +const gchar *rspamd_get_log_severity_string(gint level_flags); static const struct rspamd_logger_funcs console_log_funcs = { .init = rspamd_log_console_init, |