commit 137ceb2: [Feature] Allow to log severity level explicitly

Vsevolod Stakhov vsevolod at highsecure.ru
Thu May 13 14:00:05 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-05-13 14:58:11 +0100
URL: https://github.com/rspamd/rspamd/commit/137ceb2eb102229ebbbd06bef2681d689d035647 (HEAD -> master)

[Feature] Allow to log severity level explicitly

---
 src/libserver/cfg_file.h              |  1 +
 src/libserver/cfg_rcl.c               | 14 ++++++++++++++
 src/libserver/logger/logger.c         | 24 ++++++++++++++++++++++++
 src/libserver/logger/logger_console.c | 25 +++++++++++++++++++------
 src/libserver/logger/logger_file.c    | 25 +++++++++++++++++++------
 src/libserver/logger/logger_private.h |  1 +
 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;
@@ -1778,6 +1783,15 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
 				0,
 				NULL,
 				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",
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,


More information about the Commits mailing list