Python logging to stdout and log file

前端 未结 3 451
一个人的身影
一个人的身影 2021-02-07 00:34

I am fairly new in python and starting to get into the logging module. I would like to have the message logged into a log file and outputting to the console. The code below prin

3条回答
  •  滥情空心
    2021-02-07 01:11

    After having used Waterboy's code for simultaneous logging to console and to file (see this thread) over and over in multiple Python packages, I finally cast it into a tiny standalone Python package, which you can find here:

    https://github.com/acschaefer/duallog

    The code is well documented and easy to use. Simply download the .py file and include it in your project, or install the whole package via python setup.py install.

    Using this package, your code would look like this:

    # Set up logging to console and file.
    import duallog
    duallog.setup(logdir='my_logs')
    
    # Generate log messages.
    import logging    
    logging.debug('debug message')
    logging.info('info message')
    logging.warn('warn message')
    logging.error('error message')
    logging.critical('critical message')
    

提交回复
热议问题