Skip to main content

Python logging example

The native logging class in Python can be a little cumbersome and daunting to use. I've combined the use of ArgParse along with some simple use of logging as an example that solves a lot of problems I often want to solve:
I figure I'll refer to this example when I want to make use of logging in scripts in the future.

This example does the following:

  • Default logging level is to print warning, error, and fatal/critical error messages. Debugging and warning messages are not printed
  • Using one -v option prints all of the messages above including informational messages
  • Using two -v options prints all of the messages above including debugging messages
  • Using three or more -v options does not change the behavior from using two options. There is no error but there is no benefit to additional options either
  • You can specify an explicit log level and even reduce messages using -loglevel LEVEL where LEVEL must be one of:
    • DEBUG
    • INFO
    • WARNING
    • ERROR
    • CRITICAL
    The script support the use of FATAL which is just synonymous with CRITICAL.
    The -loglevel LEVEL and -v options are mutually exclusive.
  • There's no way to dispose of messages altogether through the options but it can be done by redirecting stderr to /dev/null
  • A custom format includes elements of the error that I want printed
  • Logging messages go to stderr

Usage

Here's the script in action:

$ ./log_example
2019-01-18 08:26:31,929 WARNING ./log_example:16 This is a warning message
2019-01-18 08:26:31,929 ERROR ./log_example:17 This is an error message
2019-01-18 08:26:31,929 CRITICAL ./log_example:18 This is a fatal/critical message
$ ./log_example -v
2019-01-18 08:26:33,893 INFO ./log_example:15 This is an informational message
2019-01-18 08:26:33,893 WARNING ./log_example:16 This is a warning message
2019-01-18 08:26:33,893 ERROR ./log_example:17 This is an error message
2019-01-18 08:26:33,894 CRITICAL ./log_example:18 This is a fatal/critical message
$ ./log_example -vv
2019-01-18 08:26:35,603 DEBUG ./log_example:14 This is a debugging message
2019-01-18 08:26:35,603 INFO ./log_example:15 This is an informational message
2019-01-18 08:26:35,603 WARNING ./log_example:16 This is a warning message
2019-01-18 08:26:35,603 ERROR ./log_example:17 This is an error message
2019-01-18 08:26:35,603 CRITICAL ./log_example:18 This is a fatal/critical message
$ ./log_example -vvvvvvvvv
2019-01-18 08:26:38,438 DEBUG ./log_example:14 This is a debugging message
2019-01-18 08:26:38,438 INFO ./log_example:15 This is an informational message
2019-01-18 08:26:38,438 WARNING ./log_example:16 This is a warning message
2019-01-18 08:26:38,438 ERROR ./log_example:17 This is an error message
2019-01-18 08:26:38,438 CRITICAL ./log_example:18 This is a fatal/critical message
$

Comments

Popular posts from this blog

Dynamic Python script loading

I have a bunch of toys and tools in a Git repository - I affectionately call this my toys repo . Most are just scripts that I use from a Unix (Cygwin or Git bash on Windoze) command line but there are some Python classes that I sometimes use in another script. Today at work, I was coming up with a new Python script that made use of a couple of my public classes. The script is good enough to share with my colleagues but I'm faced with the problem of my public classes: I imagine that most of my colleagues haven't even heard of my public classes and I don't expect them to download the entire repo just to get a couple of classes If I'm going to distribute the classes as separate files I introduce new problems: It could be confusing to have the files tag along. What is the user supposed to do with them? The answer is nothing - they should leave them alone and make sure they are in the same directory as the main script in case they decide to move th...

Python ArgParse

Maybe it's my use of the getopt() function in C/C++ but in my Python scripts, I've always just made do with the native getopt package and it's served me well. Over time, people have encouraged me to look at using ArgParse but I've always been kind of resistant because it was a little mysterious to me. I finally sat down and figured out how to do basic things I do a lot in scripts using getopt and worked up a simple script using ArgParse to do the same thing. There are definitely some advantages to ArgParse ! Here's the gist with this sample: I've made a few updates to the gist as I discover new features of which I want to take advantage: Mutually-exclusive options Script description etc. I will admit that since I've created this gist, I've been using ArgParse for new scripts. I can't whip out a script using ArgParse from memory yet (which I can do when I use getopt !) but it's becoming easier. I haven't gone back and...

Why I don't like Python 3

Why I don't like Python 3 Python 3 sucks.  I hate that they've made changes so that aren't backwards-compatible.  You can't run many Python 2 scripts using the Python 3 interpreter.  It's almost like they've gone out of their way to piss off developers. Here are some specific reasons why I hate Python 3: My biggest gripe: print is a function rather than a statement now.  I'm not saying it's not a good idea for print to be a function but why fuck with existing scripts?  Just keep the print statement as-is and create a new function with a distinct name. Data read from files comes back as bytes and you have to encode them.  More on this later but I think this is nonsense. You can't write a simple string to a file opened for binary.  Yes, I know about string.encode() but my point is I don't like having to use it. You can't turn off buffering when you open a text file for writing: open('foo', 'w', 0) throws  can...