Skip to content
Snippets Groups Projects
Verified Commit 08af1ef8 authored by Renato Alves's avatar Renato Alves :seedling:
Browse files

Add logging session materials

parent 9e04329e
No related branches found
No related tags found
No related merge requests found
my.log
# Logging
## Usage
python intro_logging.py
or
python -m intro_logging
and with the current configuration you should see the output being saved to a
file `my.log` as well as printed to the console.
Edit `logging.conf` and re-run `intro_logging.py` to see how logged messages
get sent to different destinations depending on the order of loggers, their
level, propagate status and qualname.
## Useful links
* [logging HOWTO](https://docs.python.org/howto/logging.html) - from beginner to advanced
* [logging cookbook](https://docs.python.org/howto/logging-cookbook.html)
* [`logging`](https://docs.python.org/library/logging.html)
* [`logging.config`](https://docs.python.org/library/logging.config.html) - the many ways to configure the logging module
* [`logging.handlers`](https://docs.python.org/library/logging.handlers.html) - different ways to handle logging output
# -*- coding: utf-8 -*-
import logging
import logging.config
# Using a basic configuration
# logging.basicConfig(format="%(asctime)s %(levelname)-8s %(pathname)s: %(message)s",
# level=logging.DEBUG)
#
# or a configuration file to assist and allow customization
logging.config.fileConfig("logging.conf")
# more on configuring logging at:
# https://docs.python.org/howto/logging.html#configuring-logging
# Logs can be sent to different destinations, not just the console output
# You could send to a log server, a file, a different process, via email...
#
# For different handlers see:
# https://docs.python.org/library/logging.handlers.html
# The name passed to getLogger() can be used in logging.conf's qualname
# attribute to target the output of a specific logger
LOG = logging.getLogger(__name__)
def main():
# Logging uses an implicit severity level
# See: https://docs.python.org/library/logging.html#logging-levels
# CRITICAL
# ERROR
# WARNING
# INFO
# DEBUG
LOG.critical("HAAA!!! (running around with hands in the air!)")
LOG.error("OOPS!!")
# variables can be passed using %s as placeholder
variable = ("Bob", "is", "here")
LOG.warning("WARN: %s %s %s!!", *variable)
LOG.info("This is info")
LOG.debug("More verbosity")
other()
def other():
try:
1 / 0
except ZeroDivisionError:
LOG.exception("We shouldn't get zeros here")
LOG.critical("But life goes on")
if __name__ == "__main__":
main()
# Here you need at least one logger, one handler and one formatter
[loggers]
# The order defined here and in other sections is important
# messages flow from right to left
keys=root,file_log
[handlers]
keys=console,file
[formatters]
keys=my_formatter
[formatter_my_formatter]
# For additional formatting keywords see:
# https://docs.python.org/library/logging.html#logrecord-attributes
# The negative number next to the keywords defines white space padding
format=%(asctime)s %(levelname)-8s %(funcName)-5s %(pathname)s: %(message)s
[handler_console]
# For different types of handlers see:
# https://docs.python.org/library/logging.handlers.html
class=StreamHandler
level=DEBUG
formatter=my_formatter
# The order of arguments here corresponds to the arguments of StreamHandler
args=(sys.stdout,)
[handler_file]
# For different types of handlers see:
# https://docs.python.org/library/logging.handlers.html
class=FileHandler
level=DEBUG
formatter=my_formatter
# The order of arguments here corresponds to the arguments of FileHandler
# Use 'a' to preserve the logfile across executions and 'w' to empty the logfile
args=("my.log", 'w',)
[logger_root]
level=DEBUG
handlers=console
[logger_file_log]
level=DEBUG
handlers=file
# if propagate=0 messages are used and consumed by the current logger
# if propagate=1 they are used and passed to the next logger
propagate=1
# qualname restricts the current logger to only handle output of one target.
# The target name is specified in logging.getLogger("target")
qualname=__main__
# -*- coding: utf-8 -*-
print(__name__)
# vim: ai sts=4 et sw=4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment