How to set an identity string when using logging.SysLogHandler in Python 2.6?

I have a log configured using logging.fileConfig (). I have a root log going to a handler that uses SysLogHandler ('/ dev / log', handlers.SysLogHandler.LOG_USER)

All this works fine, and I see the log entries in /var/log/user.log

The question is how to set the syslog id string to something other than python? It seems that the syslog module in the standard lib allows you to install this when you open the log, but the logging handler does not offer this function.

Will the solution be a subclass of SysLogHandler and use the syslog library inside the emit method? This is only a unix program, so using syslog directly does not pose portability problems.

+5
source share
4 answers

AFAIK, the identity string is an artifact of the syslog API, see this page . It just uses C argv [0], which, of course, will be "python".

I am surprised that you are using this with a SysLogHandlerdomain socket, since the message sent to syslog daemons via domain or TCP sockets is just a priority line with <angle brackets> followed by a formatted message and a NUL byte. There is no identification string specified SysLogHandlersince it does not use the syslog API (which has some thread safety issues in some versions, IIRC).

+3

, , syslog.

Python 3.3, SysLogHandler .ident ; ".".

:

import logging
from logging.handlers import SysLogHandler

h = SysLogHandler(address=('some.destination.com',514), facility=SysLogHandler.LOG_LOCAL6)
h.setFormatter(
    logging.Formatter('%(name)s %(levelname)s %(message)s')
)
h.ident = 'conmon'

syslog = logging.getLogger('syslog')
syslog.setLevel(logging.DEBUG)
syslog.addHandler(h)

syslog.debug('foo syslog message')
+4

Syslog, RFC3164, ("foo:" ) TAG.

MSG , TAG CONTENT    . TAG    , .

Python..

import logging
from logging.handlers import SysLogHandler

h = SysLogHandler(address='/dev/log')
h.setFormatter(logging.Formatter('foo: %(message)s'))
logging.getLogger().addHandler(h)

logging.error('bar')

.. syslog-

connect(3, {sa_family=AF_UNIX, sun_path="/dev/log"}, 10) = 0
sendto(3, "<11>foo: bar\0", 13, 0, NULL, 0) = 13
close(3)

, , systemd.

Dec 13 14:48:20 laptop foo[1928]: bar

:

{
  ..
  "PRIORITY" : "3",
  "SYSLOG_FACILITY" : "1",
  "SYSLOG_IDENTIFIER" : "foo",
  "MESSAGE" : "bar",
  "_PID" : "1928",
}

Py2.6, 2.7, 3.4, 3.5 Systemd syslog. syslog ( RFC3164). , , , python SysLogHandler RFC5424.

+3

Python 2.7 - :

class MySysLogHandler(logging.handlers.SysLogHandler):
    def __init__(self):
        super(MySysLogHandler, self).__init__(address='/dev/log')
    def emit(self, record):
        priority = self.encodePriority(self.facility, self.mapPriority(record.levelname))
        record.ident = "My[" + str(priority) + "]:"
        super(MySysLogHandler, self).emit(record)

handler = MySysLogHandler()
handler.formatter = logging.Formatter(fmt="%(ident)s %(levelname)s: %(message)s")
logging.root.addHandler(handler)
logging.info("hello world")

This will lead to the creation in syslog:

Sep 3 16:28:53 hostname My[14]: INFO: hello world

+2
source

All Articles