13👍
ANSWER HAS BEEN UPDATED -May 15, 2013 –
see bottom for additional logging option
If you want to have a single log file – use syslog, let it handle multiplexing all the inputs into a single file. Having multiple processes appending to a single file is ugly, even with multiprocessing’s workarounds.
Aside from the advantage of thread / process safe ‘downmixing’ of various streams of logging information, you can always specify a remote host to send the logs to if you wish, as well it makes log-file rotation a breeze as your clients are writing to either a domain socket or UDP socket – they don’t have to wait while you manage the files underneath them. And better yet, you won’t lose messages.
Used in combination with a syslog daemon like syslog-ng, you can do lots of fancy slicing and dicing, message relaying, duplicate message filtering, etc.
Long story short – syslog is better than managing your own log file (in my opinion), the best argument against syslog is, you don’t ‘own’ the server (and, ostensibly the log files may be off limits to you).
If you want to be super awesome, send your log data to splunk and you’ll take your game to the next level. Most folks use Splunk for IT log aggregation, but syslogging from your application into splunk is a shortcut to awesome data mining capabilities to understand performance bottlenecks, use patterns and much more.
#!/usr/bin/python
import logging
from logging.handlers import SysLogHandler
# Setup
logger = logging.getLogger( "mything" )
hdlr = SysLogHandler( address = '/dev/log', facility = SysLogHandler.LOG_USER )
logger.addHandler( hdlr )
formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s')
hdlr.setFormatter( formatter )
logger.setLevel( logging.INFO )
logger.info( 'hello Laverne!' )
logger.debug( 'The Great Ragu has taken ill!' )
NEW CONTENT – May 15, 2013
There is an additional option worth mentioning if you have the infrastructure / tenacity to set it up – Sentry, which has libraries available for Python (as well as Javascript and others), which provides a centralized location for you to send errors to for monitoring. It looks neat.