简体   繁体   中英

python logging: sending StreamHandler to file from command line

Let's say I have a script like the following, and I can't edit it :

import logging

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler()

logger.addHandler(handler)

logger.debug('debug log')
logger.info('info log')
logger.warning('warning log')
logger.error('error log')

later I call this script with:

python log_example.py > file.log

But it isn't populating the file.log file, but outputting the logs in console.

Q: Is there a way to, despite configuring logging handler to StreamHandler , still output the logs into a file from command line?

Try the &> syntax, as in Pijnappel's answer to this question . This redirects all output (not just stdout) to the file.

python log_example.py &> file.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM