Dear all, I am trying to set up a logging system with the following specs:
- all messages should be printed to stdout and to a file (here called log.log). - we have two types of loggers that the code uses: 'normal' and 'short'. - The 'short' logger just prints less detail (it basically needs another 'formatter'; the code shown here is just a simplified version of the actual problem.) - the formatters appear to be attached to handlers in the logging module (if I interpret the module correctly) - the loggers have handlers, of which we have four: - file (for the 'normal' logger to 'log.log') - file-short (for the 'short' logger to 'log.log') - console (for the 'normal' logger to stdout) - console-short (for the 'short' logger to stdout) I have an example program (test.py) and the logging configuration file (log.conf) attached below (they are also available at http://www.soton.ac.uk/~fangohr/geheim/two_handlers_one_file) Here are the questions: (i) is this (as in the log.conf file) the right use of the logging module to achieve what I need? (ii) in particular, it appears we have two filehandlers that write to the same file (in mode 'a+'). While this seems to work fine in the examples I have tested, I'd like some independent advice on whether this is 'legal' (or whether it works by chance). (I have seen this not working when both files are opened with mode 'w'.) Many thanks for your time, and any advice. Best wishes, Hans Files: --------------- test.py: --------------- import logging.config logging.config.fileConfig('log.conf') normal_log = logging.getLogger('normal') short_log = logging.getLogger('short') normal_log.info("A message to file and console") short_log.info("A short message to file and console") ----------------- log.conf: ---------------- [loggers] keys=root,normal,short [handlers] keys=console,file,file-short,console-short [formatters] keys=consolef,filef,shortf [logger_root] level=INFO handlers=console,file [logger_normal] level=NOTSET handlers=console,file qualname=normal propagate=0 [logger_short] level=NOTSET handlers=file-short,console-short qualname=short propagate=0 [handler_console] class=StreamHandler level=NOTSET formatter=consolef args=(sys.stdout,) [handler_file] class=FileHandler level=NOTSET formatter=filef args=('log.log', 'a+') [handler_console-short] class=StreamHandler level=NOTSET formatter=shortf args=(sys.stdout,) [handler_file-short] class=FileHandler level=NOTSET formatter=shortf args=('log.log','a+') [formatter_consolef] format=%(name)11s:%(asctime)s %(levelname)7s %(message)s datefmt= [formatter_filef] format=%(name)11s:%(asctime)s %(levelname)7s %(message)s datefmt= [formatter_shortf] format=%(name)11s:%(levelname)7s %(message)s datefmt= _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor