Message6670

Author whistler11783
Recipients whistler11783
Date 2011-10-12.20:53:01
SpamBayes Score 5.551115e-16
Marked as misclassified No
Message-id <1318452781.7.0.873811772249.issue1808@psf.upfronthosting.co.za>
In-reply-to
Content
Okay...I did a little poking around in the logging code. As it turns out because os.getpid() is not implemented in Jython the value for 'process' in the format string is None which causes the formatting to fail because format operator is looking for a number to format.

The logging module does not handle None correctly for formatting the process because it always is an integer in Python because os.getpid() is implemented...The format suggestion for process is '%{process}d'.

What bugs me is that a check is in the code to set process to None if os.getPid() is not implemented but a None value causes an exception in the formatting operation if you use the suggested formatting for process which is '%{process}d'.  

To workaround this issue in Jython the format suggestion for process should be '%{process}s' if you want to re-use your code between Jython and Python(like me)

See output for evidence:
>>> import logging
>>> import logging.handlers as LH
>>> log_file='/tmp/spam.txt'
>>> lhandler = LH.TimedRotatingFileHandler(log_file, when='D', interval=7, backupCount=4)
>>> root_logger = logging.getLogger('')
>>> root_logger.setLevel(logging.DEBUG)
>>> formatter = logging.Formatter('%(asctime)s:%(process)d %(levelname)s %(message)s', "%Y%m%d%H%M%S")
>>> lhandler.setFormatter(formatter)
>>> root_logger.addHandler(lhandler)
>>> logging.debug('sdasddsadadas')
Inspecting the record.__dict__ shows...
{'exc_text': None, 'filename': '<stdin>', 'thread': 2L, 'exc_info': None, 'threadName': u'MainThread', 'msecs': 571.0000991821289, 'args': (), 'created': 1318450970.571, 'levelname': 'DEBUG', 'lineno': 1, 'module': '<stdin>', 'name': 'root', 'levelno': 10, 'pathname': '<stdin>', 'process': None, 'msg': 'sdasddsadadas', 'relativeCreated': 96216.00008010864, 'funcName': '<module>'}
Traceback (most recent call last):
  File "/jython2.5.2/Lib/logging/__init__.py", line 745, in emit
    msg = self.format(record)
  File "/jython2.5.2/Lib/logging/__init__.py", line 631, in format
    return fmt.format(record)
  File "/jython2.5.2/Lib/logging/__init__.py", line 422, in format
    s = self._fmt % record.__dict__
TypeError: int argument required
>>> import os
>>> os.getpid()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getpid'
>>> dir(os)
['EX_OK', 'F_OK', 'O_APPEND', 'O_CREAT', 'O_EXCL', 'O_RDONLY', 'O_RDWR', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'W_OK', 'X_OK', '__all__', '__doc__', '__file__', '__name__', '_exists', '_exit', '_get_exports_list', '_get_shell_commands', '_name', '_native_posix', '_posix_impl', '_wrap_close', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'close', 'curdir', 'defpath', 'devnull', 'environ', 'errno', 'error', 'extsep', 'fdatasync', 'fdopen', 'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'getenv', 'geteuid', 'getgid', 'getlogin', 'getpgrp', 'getppid', 'getuid', 'isatty', 'kill', 'lchmod', 'lchown', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'setpgrp', 'setsid', 'stat', 'stat_result', 'strerror', 'symlink', 'sys', 'system', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'waitpid', 'walk', 'write']
>>> exit()
 
A workaround until os.getpid() is implemented is use format '%{process}s' instead of the suggestion of '%{process}d' in the format string.

e.g.:
>>> import logging
>>> import logging.handlers as LH
>>> log_file='/tmp/spam.txt'
>>> lhandler = LH.TimedRotatingFileHandler(log_file, when='D', interval=7, backupCount=4)
>>> root_logger = logging.getLogger('')
>>> root_logger.setLevel(logging.DEBUG)
>>> formatter = logging.Formatter('%(asctime)s:%(process)s %(levelname)s %(message)s', "%Y%m%d%H%M%S")
>>> lhandler.setFormatter(formatter)
>>> root_logger.addHandler(lhandler)
>>> logging.debug('sdsddsdsdsd')
>>> exit()
-bash-3.00$ cat /tmp/spam.txt
20111012143723:None DEBUG sdsddsdsdsd
-bash-3.00$
History
Date User Action Args
2011-10-12 20:53:01whistler11783setmessageid: <1318452781.7.0.873811772249.issue1808@psf.upfronthosting.co.za>
2011-10-12 20:53:01whistler11783setrecipients: + whistler11783
2011-10-12 20:53:01whistler11783linkissue1808 messages
2011-10-12 20:53:01whistler11783create