Created
July 16, 2016 05:56
-
-
Save FulcronZ/22fe2d8b4f126094db4a366a71bdb65c to your computer and use it in GitHub Desktop.
Python Logging Module Experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
# Create a logger | |
logger = logging.getLogger('mylogger') | |
logger.setLevel(logging.INFO) | |
# Create a log handler printing to sys.stdout | |
consoleWarning = logging.StreamHandler() | |
consoleWarning.setLevel(logging.WARNING) | |
# Create a log formatter upcasing messages | |
class UpcaseFormatter(logging.Formatter): | |
def format(self, record): | |
return record.levelname + ": " + record.msg.upper() | |
# Bind the formatter to the handler | |
# upcaseFormatter = UpcaseFormatter() # no need to create a formatter instance in python3 | |
consoleWarning.setFormatter(UpcaseFormatter()) | |
# Finally bind to the logger | |
logger.addHandler(consoleWarning) | |
logger.debug("I just ran this line") | |
logger.info("foo() finished") | |
logger.warning("disk has %d left" % 15) | |
logger.error("error") | |
logger.critical("overheat") | |
loggerChild = logging.getLogger('mylogger.child') | |
loggerChild.setLevel(logging.DEBUG) | |
loggerChild.propagate = True | |
loggerChild.debug("Hello Papa") | |
loggerChild.info("Hello Papa!") | |
loggerChild.warning("Papa!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment