Skip to content

Instantly share code, notes, and snippets.

@FulcronZ
Created July 16, 2016 05:56
Show Gist options
  • Save FulcronZ/22fe2d8b4f126094db4a366a71bdb65c to your computer and use it in GitHub Desktop.
Save FulcronZ/22fe2d8b4f126094db4a366a71bdb65c to your computer and use it in GitHub Desktop.
Python Logging Module Experiment
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