aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2021-05-13 14:58:11 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2021-05-13 14:58:11 +0100
commit137ceb2eb102229ebbbd06bef2681d689d035647 (patch)
treebf1ad1d05e98bdff87589a7eb56945564bd9f419
parent967e5ee83cb1e7bb17dde33686a48d27617b1e0c (diff)
downloadrspamd-137ceb2eb102229ebbbd06bef2681d689d035647.tar.gz
rspamd-137ceb2eb102229ebbbd06bef2681d689d035647.zip
[Feature] Allow to log severity level explicitly
-rw-r--r--src/libserver/cfg_file.h1
-rw-r--r--src/libserver/cfg_rcl.c14
-rw-r--r--src/libserver/logger/logger.c24
-rw-r--r--src/libserver/logger/logger_console.c25
-rw-r--r--src/libserver/logger/logger_file.c25
-rw-r--r--src/libserver/logger/logger_private.h1
6 files changed, 78 insertions, 12 deletions
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 67f18e1e9..5525030b0 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -76,6 +76,7 @@ enum rspamd_log_cfg_flags {
RSPAMD_LOG_FLAG_USEC = (1 << 3),
RSPAMD_LOG_FLAG_RSPAMADM = (1 << 4),
RSPAMD_LOG_FLAG_ENFORCED = (1 << 5),
+ RSPAMD_LOG_FLAG_SEVERITY = (1 << 6),
};
struct rspamd_worker_log_pipe {
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index 4891c4194..fdd7e455a 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -238,6 +238,11 @@ rspamd_rcl_logging_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
cfg->log_flags |= RSPAMD_LOG_FLAG_COLOR;
}
+ val = ucl_object_lookup_any (obj, "severity", "log_severity", NULL);
+ if (val && ucl_object_toboolean (val)) {
+ cfg->log_flags |= RSPAMD_LOG_FLAG_SEVERITY;
+ }
+
val = ucl_object_lookup_any (obj, "systemd", "log_systemd", NULL);
if (val && ucl_object_toboolean (val)) {
cfg->log_flags |= RSPAMD_LOG_FLAG_SYSTEMD;
@@ -1780,6 +1785,15 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
0);
rspamd_rcl_add_doc_by_path (cfg,
"logging",
+ "Enable severity logging output (e.g. [error] or [warning])",
+ "log_severity",
+ UCL_BOOLEAN,
+ NULL,
+ 0,
+ NULL,
+ 0);
+ rspamd_rcl_add_doc_by_path (cfg,
+ "logging",
"Enable systemd compatible logging",
"systemd",
UCL_BOOLEAN,
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,