I've found some similar example to log messages to multiple files. But nothing quite for what I want. I am using the logging module and I am logging all info messages to the console and a file. (I will turn off the console logging at a later date).
However I wish to expand this to log all info messages to file.log and all error messages to file.err. Even better if its possible to log all messages to file.log (error and info) and then all err messages to a separate file.
Is this possible?
I can't answer my own question for 7 hours apparently, so I'll just update my original question.
I feel stupid now. After several hours trying examples before posting I found this 'again'. Its like the task of typing out what I was trying to do triggered something when reading the examples.
http://docs.python.org/2/howto/logging-cookbook.html
This outputs everything at INFO level and above to one log file (log_file) and then also prints logging.error and logging.warning messages to a separate file (err_file)
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
filemode='w')
log_error = logging.FileHandler(err_file)
log_error.setLevel(logging.WARNING)
log_info = logging.FileHandler(log_file)
log_info.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
# tell the handler to use this format
log_error.setFormatter(formatter)
log_info.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(log_info)
logging.getLogger('').addHandler(log_error)
Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is the first go-to point for most developers when it comes to logging.
Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger. The arguments are interpreted as for debug().
test_log.py:
import logging
def get_logger(
LOG_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
LOG_NAME = '',
LOG_FILE_INFO = 'file.log',
LOG_FILE_ERROR = 'file.err'):
log = logging.getLogger(LOG_NAME)
log_formatter = logging.Formatter(LOG_FORMAT)
# comment this to suppress console output
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(log_formatter)
log.addHandler(stream_handler)
file_handler_info = logging.FileHandler(LOG_FILE_INFO, mode='w')
file_handler_info.setFormatter(log_formatter)
file_handler_info.setLevel(logging.INFO)
log.addHandler(file_handler_info)
file_handler_error = logging.FileHandler(LOG_FILE_ERROR, mode='w')
file_handler_error.setFormatter(log_formatter)
file_handler_error.setLevel(logging.ERROR)
log.addHandler(file_handler_error)
log.setLevel(logging.INFO)
return log
def main():
my_logger = get_logger()
my_logger.info('This is an INFO message')
my_logger.warning('This is a WARNING message')
my_logger.error('This is an ERROR message')
if __name__ == '__main__':
main()
Output
$ python test_log.py
2013-09-20 11:52:07,096 root INFO This is an INFO message
2013-09-20 11:52:07,096 root WARNING This is a WARNING message
2013-09-20 11:52:07,096 root ERROR This is an ERROR message
$ cat file.log
2013-09-20 11:52:07,096 root INFO This is an INFO message
2013-09-20 11:52:07,096 root WARNING This is a WARNING message
2013-09-20 11:52:07,096 root ERROR This is an ERROR message
$ cat file.err
2013-09-20 11:52:07,096 root ERROR This is an ERROR message
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With