Making Python loggers output all messages to stdout in addition to log file
in stackoverflow ∙
3 mins read
∙ Tags: Python, Logging, Error-logging
Last modified at:
Updated:
Making Python loggers output all messages to stdout in addition to log file
Question by Ben
Is there a way to make Python logging using the logging
module automatically output things to stdout in addition to the log file where they are supposed to go? For example, I’d like all calls to logger.warning
, logger.critical
, logger.error
to go to their intended places but in addition always be copied to stdout
. This is to avoid duplicating messages like:
# file: 'question.py'
mylogger.critical("something failed")
print "something failed"
Answer by Milovan Tomašević
For more detailed explanations - great documentation at that link. For example: It’s easy, you only need to set up two loggers.
# file: 'answer.py'
import sys
import logging
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('my_log_info.log')
sh = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
fh.setFormatter(formatter)
sh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(sh)
def hello_logger():
logger.info("Hello info")
logger.critical("Hello critical")
logger.warning("Hello warning")
logger.debug("Hello debug")
if __name__ == "__main__":
hello_logger()
Output - terminal:
# file: 'terminal'
[Mon, 10 Aug 2020 12:44:25] INFO [TestLoger.py.hello_logger:15] Hello info
[Mon, 10 Aug 2020 12:44:25] CRITICAL [TestLoger.py.hello_logger:16] Hello critical
[Mon, 10 Aug 2020 12:44:25] WARNING [TestLoger.py.hello_logger:17] Hello warning
[Mon, 10 Aug 2020 12:44:25] DEBUG [TestLoger.py.hello_logger:18] Hello debug
Output - in file:
Output
UPDATE: color terminal
Package:
# file: 'terminal'
pip install colorlog
Code:
# file: 'answer.py'
import sys
import logging
import colorlog
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('my_log_info.log')
sh = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
fh.setFormatter(formatter)
sh.setFormatter(colorlog.ColoredFormatter('%(log_color)s [%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S'))
logger.addHandler(fh)
logger.addHandler(sh)
def hello_logger():
logger.info("Hello info")
logger.critical("Hello critical")
logger.warning("Hello warning")
logger.debug("Hello debug")
logger.error("Error message")
if __name__ == "__main__":
hello_logger()
Output
Recommendation:
Complete logger configuration from INI
file, which also includes setup for stdout
and debug.log
:
handler_file
level=WARNING
handler_screen
level=DEBUG
Improve this page:
Share on:
Keep going!
Keep going ×2!
Give me more!
Thank you, thank you
Far too kind!
Never gonna give me up?
Never gonna let me down?
Turn around and desert me!
You're an addict!
Son of a clapper!
No way
Go back to work!
This is getting out of hand
Unbelievable
PREPOSTEROUS
I N S A N I T Y
FEED ME A STRAY CAT
Share on:
Comments 💬
Templates (for web app):
Loading…
Permalink