[Python-Dev] Bug? syslog.openlog using ident "python" by default.

Sean Reifschneider jafo at tummy.com
Thu Mar 18 12:58:44 CET 2010


I'd appreciate some opinions on this.  Personally, I'm in the "the current
code is buggy" camp.  :-)  I can code up the changes to the syslog module
if we decide that's the right way to go.  Looks like Raymond, Guido, and I
are the last ones to do syslog-specific changes to this module in the last
15 years.

If you call:

   from syslog import syslog, openlog
   syslog('My error message')

Something like the following gets logged:

   Mar 18 05:20:22 guin python: My error message
                        ^^^^^^

Where I'm annoyed by the "python" in the above.  This is pulled from
the C argv[0].  IMHO, what we really want is the Python sys.argv[0].
Because of this feature, I always have to do the following when I'm using
syslog:

   openlog(os.path.basename(sys.argv[0]))

I would propose changing the Python syslog() call to do the C equivalent of:

   if openlog_hasnt_been_called_before_now:
      if sys.argv[0]:
         syslog.openlog(os.path.basename(sys.argv[0]))

In other words, if there's a script name and openlog hasn't already been
called, call openlog with the basename of the script name.

This is effectively what happens in normal C code that calls syslog.

Note that the Python syslog.openlog says that the default for ident is
"(usually) 'syslog'".

The benefit of this change is that you get a more identifiable ident string
in Python programs, so when you look at the logs you can tell what script
it came from, not just that it came from some Python program.  One way of
looking at it would be that the syslog module logging "python" as the
program name is a bug.

The downside I see is that there might be some users doing log scraping
depending on this (IMHO, buggy) log ident.

Something else just occurred to me though, a nice enhancement would be for
openlog() to have a "None" default for ident, something like:

   orig_openlog = openlog
   def openlog(ident = None, logopt = 0, facility = LOG_USER)
      if ident == None:
         if sys.argv[0]:
            ident = os.path.basename(sys.argv[0])
         else:
            ident = 'python'
      orig_openlog(ident, logopt, facility)

So you could call openlog and rely on the default ident (sys.argv[0]),
and set the logopt and facility.

The gory details of why this is occurring are: If you don't call
"openlog()" before "syslog()", the system syslog() function calls
something similar to: "openlog(basename(argv[0]))", which causes the
"ident" of the syslog messages to be "python" rather than the specific
program.

Thoughts?

Thanks,
Sean
-- 
 "Every increased possession loads us with new weariness."
                 -- John Ruskin
Sean Reifschneider, Member of Technical Staff <jafo at tummy.com>
tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability



More information about the Python-Dev mailing list