12

When I run this on my mac:

import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

syslog_address = '/var/run/syslog'
logger.addHandler(logging.handlers.SysLogHandler(syslog_address))
logger.error("What the crap?")

It shows up like this in the syslog:

Oct 18 19:02:06 nick Unknown[4294967295] <Error>: What the crap?

Why is it Unknown? Shouldn't it be smart enough to name itself after the script's name?

Nick Retallack
  • 18,986
  • 17
  • 92
  • 114

3 Answers3

15

I think the APP-NAME (which indicates the source) is an optional component in the syslog header. The latest version of SysLogHandler (for Python 3.3) includes support for an APP-NAME (called ident as per the C syslog API), but it's not available in earlier versions. See this Python issue.

If you prepend your script name to all messages, you will get the desired effect. For example,

logger.error('foo: What\'s up?')

will display e.g.

19/10/2011 13:51:17 foo[2147483647] What's up?

in the log.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • According to the python issue you can set the property `handler.ident` to a string and it will be automatically prepended to all messages. – Hubro Feb 07 '17 at 08:53
  • @Hubro true, that's a later addition to the logging code - it was added as a resulted of the linked issue. This solution is for older Python versions. – Vinay Sajip Feb 07 '17 at 15:20
  • Correction after testing, it's not enough to just give `handler.ident` the value `"app_name"`, as it will just prepend it as-is, and you end up with a log line like `"app_nameThis is a test"`. You have to include `": "` manually. – Hubro Feb 07 '17 at 20:40
6

To get what you need, you should add a formatter to the handler so you don't have to manually format every message yourself.

import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

syslog_address = '/var/run/syslog'

handler = logging.handlers.SysLogHandler(syslog_address)
# create the formatter
formatter = logging.Formatter('%(name)s: [%(levelname)s] %(message)s')
# plug this formatter into your handler(s)
handler.setFormatter(formatter)

logger.addHandler(handler)
logger.error("What the crap?")

You should now find that you see entries in syslog as you'd expect:

Jul  4 14:34:40 ip-127-0-0-1 script_name.py: [ERROR] What the crap?
Autumn
  • 398
  • 2
  • 7
Splee
  • 249
  • 3
  • 6
  • 2
    It seems that the API for handlers differs between some versions of Python. I just had to do the following to handle our 2 environments: try: handler.addFormatter(formatter) except AttributeError, e: handler.formatter = formatter – Splee Jul 05 '12 at 22:51
3

It can be found all list which word matches what in the list you can find at this link

If you need more you can have a look further example:

from logging.handlers import SysLogHandler
import logging

def log(self, severity=logging.DEBUG, message=None):
    """
    Log utility for system wide logging needs
    @param severity Log severity
    @param message Log message
    @return
    """
    logger = logging.getLogger()
    logger.setLevel(severity)
    syslog = SysLogHandler(address="/dev/log")
    syslog.setFormatter(
          logging.Formatter("%(module)s-%(processName)s[%(process)d]: %(name)s: %(message)s")
    )
    logger.addHandler(syslog)
    logger.log(severity, message)

It is quite simple and I use this method as a global logging package in my projects.

fth
  • 2,478
  • 2
  • 30
  • 44