]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: log: support a new "short" format
authorWilly Tarreau <w@1wt.eu>
Mon, 12 Nov 2018 07:45:00 +0000 (08:45 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 12 Nov 2018 17:37:55 +0000 (18:37 +0100)
This format is meant to be used with local file descriptors. It emits
messages only prefixed with a level, removing all the process name,
system name, date and so on. It is similar to the printk() format used
on Linux. It's suitable to be sent to a local logger compatible with
systemd's output format.

Note that the facility is still required but not used, hence it is
suggested to use "daemon" to remind that it's a local logger.
Example :

    log stdout format short daemon          # send everything to stdout
    log stderr format short daemon notice   # send important events to stderr

doc/configuration.txt
include/types/log.h
src/log.c

index 9f4fb5934e22f7f9278bcc843afcce1355f2ed68..89fddc12075597c076ac88d625bc7dd3238cfb12 100644 (file)
@@ -856,7 +856,7 @@ log <address> [len <length>] [format <format>] <facility> [max level [min level]
           as non-blocking calls will be ignored. Also there will be no way to
           purge nor rotate this file without restarting the process. Note that
           the configured syslog format is preserved, so the output is suitable
-          for use with a TCP syslog server.
+          for use with a TCP syslog server. See also the "short" format below.
 
         - "stdout" / "stderr", which are respectively aliases for "fd@1" and
           "fd@2", see above.
@@ -886,11 +886,22 @@ log <address> [len <length>] [format <format>] <facility> [max level [min level]
     rfc5424   The RFC5424 syslog message format.
               (https://tools.ietf.org/html/rfc5424)
 
+    short     A message containing only a level between angle brackets such as
+              '<3>', followed by the text. The PID, date, time, process name
+              and system name are omitted. This is designed to be used with a
+              local log server. This format is compatible with what the systemd
+              logger consumes.
+
   <facility> must be one of the 24 standard syslog facilities :
 
-          kern   user   mail   daemon auth   syslog lpr    news
-          uucp   cron   auth2  ftp    ntp    audit  alert  cron2
-          local0 local1 local2 local3 local4 local5 local6 local7
+                 kern   user   mail   daemon auth   syslog lpr    news
+                 uucp   cron   auth2  ftp    ntp    audit  alert  cron2
+                 local0 local1 local2 local3 local4 local5 local6 local7
+
+             Note that the facility is ignored for the "short" format, but
+             still required as a positional field. It is recommended to use
+             "daemon" in this case to make it clear that it's only supposed to
+             be used locally.
 
   An optional level can be specified to filter outgoing messages. By default,
   all messages are sent. If a maximum level is specified, only messages with a
@@ -5136,7 +5147,7 @@ no log
                  ignored. Also there will be no way to purge nor rotate this
                  file without restarting the process. Note that the configured
                  syslog format is preserved, so the output is suitable for use
-                 with a TCP syslog server.
+                 with a TCP syslog server. See also the "short" format below.
 
                - "stdout" / "stderr", which are respectively aliases for "fd@1"
                  and "fd@2", see above.
@@ -5165,11 +5176,22 @@ no log
       rfc5424   The RFC5424 syslog message format.
                 (https://tools.ietf.org/html/rfc5424)
 
+      short     A message containing only a level between angle brackets such as
+                '<3>', followed by the text. The PID, date, time, process name
+                and system name are omitted. This is designed to be used with a
+                local log server. This format is compatible with what the
+                systemd logger consumes.
+
     <facility> must be one of the 24 standard syslog facilities :
 
-                 kern   user   mail   daemon auth   syslog lpr    news
-                 uucp   cron   auth2  ftp    ntp    audit  alert  cron2
-                 local0 local1 local2 local3 local4 local5 local6 local7
+                   kern   user   mail   daemon auth   syslog lpr    news
+                   uucp   cron   auth2  ftp    ntp    audit  alert  cron2
+                   local0 local1 local2 local3 local4 local5 local6 local7
+
+               Note that the facility is ignored for the "short" format, but
+               still required as a positional field. It is recommended to use
+               "daemon" in this case to make it clear that it's only supposed
+               to be used locally.
 
     <level>    is optional and can be specified to filter outgoing messages. By
                default, all messages are sent. If a level is specified, only
@@ -5196,6 +5218,8 @@ no log
 
   Example :
     log global
+    log stdout format short daemon          # send everything to stdout
+    log stderr format short daemon notice   # send important events to stderr
     log 127.0.0.1:514 local0 notice         # only send important events
     log 127.0.0.1:514 local0 notice notice  # same but limit output level
     log "${LOCAL_SYSLOG}:514" local0 notice   # send to local server
index c394d773456ec31748506aed6c76f5da49c6c967..3813cfbdeaee06c27d5319cb0f3db23d00bedc2d 100644 (file)
@@ -41,6 +41,7 @@ extern const char *log_levels[];
 enum {
        LOG_FORMAT_RFC3164 = 0,
        LOG_FORMAT_RFC5424,
+       LOG_FORMAT_SHORT,
        LOG_FORMATS,          /* number of supported log formats, must always be last */
 };
 
index c8ee6a94326af9597557a71d5d4e415a74e85850..2aef5520fcf1c49f7301a94573d2876179f2dfff 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -67,7 +67,14 @@ static const struct log_fmt log_formats[LOG_FORMATS] = {
                        .sep1 = { .area = " ",   .data = 1 },
                        .sep2 = { .area = " - ", .data = 3 }
                }
-       }
+       },
+       [LOG_FORMAT_SHORT] = {
+               .name = "short",
+               .pid = {
+                       .sep1 = { .area = "",  .data = 0 },
+                       .sep2 = { .area = " ", .data = 1 },
+               }
+       },
 };
 
 #define FD_SETS_ARE_BITFIELDS
@@ -1329,7 +1336,8 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
                const struct logsrv *logsrv = tmp;
                int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
                        &logfdunix : &logfdinet;
-               char *pid_sep1 = NULL, *pid_sep2 = NULL;
+               char *pid_sep1 = "", *pid_sep2 = "";
+               char logheader_short[3];
                int sent;
                int maxlen;
                int hdr_max = 0;
@@ -1383,6 +1391,18 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
                        sd_max = sd_size; /* the SD part allowed only in RFC5424 */
                        break;
 
+               case LOG_FORMAT_SHORT:
+                       /* all fields are known, skip the header generation */
+                       hdr = logheader_short;
+                       hdr[0] = '<';
+                       hdr[1] = '0' + MAX(level, logsrv->minlvl);
+                       hdr[2] = '>';
+                       hdr_ptr = hdr;
+                       hdr_max = 3;
+                       maxlen = logsrv->maxlen - hdr_max;
+                       max = MIN(size, maxlen) - 1;
+                       goto send;
+
                default:
                        continue; /* must never happen */
                }