Browse Source

log_systemd: added support to replace core syslog with logging to systemd

Daniel-Constantin Mierla 10 years ago
parent
commit
3133918bcd

+ 30 - 11
modules/log_systemd/README

@@ -23,13 +23,15 @@ Daniel-Constantin Mierla
               2.1. Kamailio Modules
               2.2. External Libraries or Applications
 
-        3. Functions
+        3. Core Logging
+        4. Functions
 
-              3.1. sd_journal_print(level, text)
+              4.1. sd_journal_print(level, text)
 
    List of Examples
 
-   1.1. sd_journal_print usage
+   1.1. Core Logging Replacement with Systemd
+   1.2. sd_journal_print usage
 
 Chapter 1. Admin Guide
 
@@ -41,14 +43,15 @@ Chapter 1. Admin Guide
         2.1. Kamailio Modules
         2.2. External Libraries or Applications
 
-   3. Functions
+   3. Core Logging
+   4. Functions
 
-        3.1. sd_journal_print(level, text)
+        4.1. sd_journal_print(level, text)
 
 1. Overview
 
-   This module provides logging to systemd journal from configuration
-   file.
+   It provides logging to systemd journal from the moment of loading this
+   module.
 
 2. Dependencies
 
@@ -66,11 +69,27 @@ Chapter 1. Admin Guide
    running Kamailio with this module loaded:
      * libsystemd
 
-3. Functions
+3. Core Logging
 
-   3.1. sd_journal_print(level, text)
+   This module can replace the syslog logging with sending the log
+   messages to systemd journal. The logging to systemd is started when
+   this module is loaded, before that the default syslog system is used.
 
-3.1.  sd_journal_print(level, text)
+   It requires that core parameters log_engine_type to be set to
+   'systemd'. It is not enabled if log_stderror=yes.
+
+   Example 1.1. Core Logging Replacement with Systemd
+...
+log_engine_type="systemd"
+...
+loadmodule "log_systemd.so"
+...
+
+4. Functions
+
+   4.1. sd_journal_print(level, text)
+
+4.1.  sd_journal_print(level, text)
 
    Print the text in the systemd journal at the provided level parameter.
 
@@ -81,7 +100,7 @@ Chapter 1. Admin Guide
 
    This function can be used from ANY_ROUTE.
 
-   Example 1.1. sd_journal_print usage
+   Example 1.2. sd_journal_print usage
 ...
    sd_journal_print("LOG_INFO", "R-URI is $ru\n");
 ...

+ 26 - 2
modules/log_systemd/doc/log_systemd_admin.xml

@@ -16,8 +16,8 @@
 	<section>
 	<title>Overview</title>
 	<para>
-		This module provides logging to systemd journal from configuration
-		file.
+		It provides logging to systemd journal from the moment
+		of loading this module.
 	</para>
 	</section>
 
@@ -52,6 +52,30 @@
 	</section>
 	</section>
 
+	<section>
+	<title>Core Logging</title>
+		<para>
+		This module can replace the syslog logging with sending the log messages
+		to systemd journal. The logging to systemd is
+		started when this module is loaded, before that the default
+		syslog system is used.
+		</para>
+		<para>
+		It requires that core parameters log_engine_type to be set to 'systemd'.
+		It is not enabled if log_stderror=yes.
+		</para>
+		<example>
+		<title><function>Core Logging Replacement with Systemd</function></title>
+		<programlisting format="linespecific">
+...
+log_engine_type="systemd"
+...
+loadmodule "log_systemd.so"
+...
+</programlisting>
+	    </example>
+	</section>
+
 	<section>
 	<title>Functions</title>
 	<section id="log_systemd.f.sd_journal_print">

+ 32 - 0
modules/log_systemd/log_systemd_mod.c

@@ -33,6 +33,9 @@
 
 MODULE_VERSION
 
+static int _lc_log_systemd = 0;
+void _lc_core_log_systemd(int lpriority, const char *format, ...);
+
 static int  mod_init(void);
 static int  child_init(int);
 static void mod_destroy(void);
@@ -65,7 +68,18 @@ struct module_exports exports = {
 	child_init      /* per child init function */
 };
 
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+	if(_km_log_engine_type==0)
+		return 0;
+
+	if(strcasecmp(_km_log_engine_type, "systemd")!=0)
+		return 0;
 
+	km_log_func_set(&_lc_core_log_systemd);
+	_lc_log_systemd = 1;
+	return 0;
+}
 
 /**
  * init module function
@@ -131,3 +145,21 @@ static int w_sd_journal_print(struct sip_msg* msg, char* lev, char* txt)
 
 	return 1;
 }
+
+#define LC_LOG_MSG_MAX_SIZE	16384
+void _lc_core_log_systemd(int lpriority, const char *format, ...)
+{
+	va_list arglist;
+	char obuf[LC_LOG_MSG_MAX_SIZE];
+	int n;
+	int priority;
+
+	/* Return on MASKed log priorities */
+	priority = LOG_PRI(lpriority);
+
+	va_start(arglist, format);
+	n = 0;
+	n += vsnprintf(obuf+n, LC_LOG_MSG_MAX_SIZE - n, format, arglist);
+	va_end(arglist);
+	sd_journal_print(priority, "%.*s", n, obuf);
+}