Python logging and rotating files

Don’t use logging.basicConfig, use WatchedFileHandler. Here’s how to use it. import time import logging import logging.handlers def log_setup(): log_handler = logging.handlers.WatchedFileHandler(‘my.log’) formatter = logging.Formatter( ‘%(asctime)s program_name [%(process)d]: %(message)s’, ‘%b %d %H:%M:%S’) formatter.converter = time.gmtime # if you want UTC time log_handler.setFormatter(formatter) logger = logging.getLogger() logger.addHandler(log_handler) logger.setLevel(logging.DEBUG) log_setup() logging.info(‘Hello, World!’) import os os.rename(‘my.log’, ‘my.log-old’) logging.info(‘Hello, New … Read more

UTF-8 In Python logging, how?

Having code like: raise Exception(u’щ’) Caused: File “/usr/lib/python2.7/logging/__init__.py”, line 467, in format s = self._fmt % record.__dict__ UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-3: ordinal not in range(128) This happens because the format string is a byte string, while some of the format string arguments are unicode strings with non-ASCII characters: >>> “%(message)s” … Read more

Changing logging’s ‘basicConfig’ which is already set

If you look in the Python sources for logging/__init__.py, you’ll see that basicConfig() sets the handlers on the root logger object by calling addHandler(). If you want to start from scratch, you could remove all existing handlers and then call basicConfig() again. # Example to remove all root logger handlers and reconfigure. (UNTESTED) import logging … Read more

Python: Logging TypeError: not all arguments converted during string formatting

You cannot use new-style formatting when using the logging module; use %s instead of {}. logging.info(‘date=%s’, date) The logging module uses the old-style % operator to format the log string. See the debug method for more detail. If you really want to use str.format() string formatting, consider using custom objects that apply the formatting ‘late’, … Read more

How to log error to file, and not fail on exception

You could use the logging module: import logging logging.basicConfig(filename=”/tmp/myapp.log”, level=logging.DEBUG, format=”%(asctime)s %(levelname)s %(name)s %(message)s”) logger=logging.getLogger(__name__) try: 1/0 except ZeroDivisionError as err: logger.error(err) Running the script writes in /tmp/myapp.log: % cat /tmp/myapp.log 2010-08-01 17:50:45,960 ERROR __main__ integer division or modulo by zero

How can I log a dictionary into a log file?

Simple solution that works The logging functions will convert the ‘%s’ to string representation (and if the object happens to be a container, then repr() will be used for the contained objects) import logging logging.basicConfig(level=logging.DEBUG, filename=”sample.log”) logging.debug(‘Sample dict log: %s’, {‘name’ : “John”, ‘age’: 10}) How it shows in the log file: DEBUG:root:Sample dict log: … Read more