What are logs in Python

logging module


The loggingModule is part of the Standard Library (STL) pythons.
We start from the simplest, because it gets complicated by itself anyway.
My first resources are python Coobook (3rd edition) page 555, and the logging cookbok at http://docs.python.org/3/howto/logging-cookbook.html.

?> cat ./bsp/logging/log0.py '' 'call this script with interpreter log-message is output on the console (terminal)' '' import logging # logging.info is not output logging.info ("That is our first info ") # level 'warn' is default # + so this log-msg should be output logging.warning (" This is our first warning ")?> python3 ./bsp/logging/log0.py WARNING: root: This is our first warning?>

Not very helpful yet, but that was only the first micrometer on our logging voyage of discovery!
Let's look at another example:

?> cat ./bsp/logging/log1.py #! / usr / bin / env python3 '' 'here now the first variant, where a log message level info can be written into a specified log file, still WITHOUT formatting' '' import logging import time logging.basicConfig (level = logging.INFO , filename = '/ tmp / info_test.log') logging.info ("Program starts") logging.info ('Go to sleep for 10 seconds') time.sleep (10) logging.info ('I woke up again')?> python3 ./bsp/logging/log1.py?> # there was no output! ?> cat /tmp/info_test.log INFO: root: program starts INFO: root: go to sleep for 10 seconds INFO: root: I woke up again?>

We'll take care of the formatting later. Here it was once important to see that you can actually have it written out in a nice and clear way in a file.

The logle levels are arranged hierarchically, starting with DEBUG and ending with CRITICAL.

  • INFO

If in the basicConfig or in the configfile, for example, WARNING was specified as the loglevel, we achieve that DEBUG and INFO logs are NOT transferred to the logfile, but only from WARNING.
A basicConfig could look like this:

logging.basicConfig (filename = 'app.log', level = logging.ERROR)

Means logging. () (level in lower case) you can now attach your logging messages. An example:

?> cat bsp / logging / log2.py #! / usr / bin / env python3 '' 'logging.basicConfig and logging.

Here we came into contact with the formatting of the log lines - (date-time loglevel - message)
So it's not that difficult at all - you determine where the logfile should be and what the logfile should be called, and then, depending on the urgency, you can have log messages written to this logfile.

In the example script log2.py you can see that in the basicConfig as a log cabinet WARNING was specified - therefore the debug and info messages were NOT included in the log file.

The format string of the log messages is interesting (% s -> string,% r -> repr, i.e. enclosed with single quotes). But you can certainly do that with the format () method. Try it out right away and see whether the lines in the log file are appended to the existing ones or overwritten.

> cat bsp / logging / log2.py #! / usr / bin / env python3 '' 'logging.basicConfig and logging.

The two questions were answered - yes, you can use the format () method instead of format strings, and yes, new log lines are appended to existing ones.
With this knowledge we can now use logging in our scripts!
All that comes after that are refinements and even deeper knowledge that one can gradually acquire.

The basicConfig is therefore hardcoded in the script.
If you wanted to change the log barrier, which is set in the basicConfiga (or later in a separate config file), you can do this using:

logging.getLogger (). level = logging.DEBUG

Until I rework this page, the logging cookbook page listed above should be sufficient - it's good for a lot!

Here it goes to the top of the page and there it goes to the python start page