Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending the Python Logger

I'm looking for a simple way to extend the logging functionality defined in the standard python library. I just want the ability to choose whether or not my logs are also printed to the screen.

Example: Normally to log a warning you would call:

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s', filename='log.log', filemode='w')
logging.warning("WARNING!!!")

This sets the configurations of the log and puts the warning into the log

I would like to have something along the lines of a call like:

logging.warning("WARNING!!!", True)

where the True statement signifys if the log is also printed to stdout.

I've seen some examples of implementations of overriding the logger class

but I am new to the language and don't really follow what is going on, or how to implement this idea. Any help would be greatly appreciated :)

like image 740
Colton Phillips Avatar asked Aug 01 '26 14:08

Colton Phillips


1 Answers

The Python logging module defines these classes:

Loggers that emit log messages.
Handlers that put those messages to a destination.
Formatters that format log messages.
Filters that filter log messages.

A Logger can have Handlers. You add them by invoking the addHandler() method. A Handler can have Filters and Formatters. You similarly add them by invoking the addFilter() and setFormatter() methods, respectively.

It works like this:

import logging

# make a logger
main_logger = logging.getLogger("my logger")
main_logger.setLevel(logging.INFO)

# make some handlers
console_handler = logging.StreamHandler() # by default, sys.stderr
file_handler    = logging.FileHandler("my_log_file.txt")

# set logging levels
console_handler.setLevel(logging.WARNING)
file_handler.setLevel(logging.INFO)

# add handlers to logger
main_logger.addHandler(console_handler)
main_logger.addHandler(file_handler)

Now, you can use this object like this:

main_logger.info("logged in the FILE")
main_logger.warning("logged in the FILE and on the CONSOLE")

If you just run python on your machine, you can type the above code into the interactive console and you should see the output. The log file will get crated in your current directory, if you have permissions to create files in it.

I hope this helps!

like image 187
Dmitry Blotsky Avatar answered Aug 04 '26 05:08

Dmitry Blotsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!