Classes and Functions
So far, we have seen the default logger named root
, which is used by the logging module whenever its functions are called directly like this: logging.debug()
. You can (and should) define your own logger by creating an object of the Logger
class, especially if your application has multiple modules. Let’s have a look at some of the classes and functions in the module.
The most commonly used classes defined in the logging module are the following:
-
Logger
: This is the class whose objects will be used in the application code directly to call the functions. -
LogRecord
: Loggers automatically createLogRecord
objects that have all the information related to the event being logged, like the name of the logger, the function, the line number, the message, and more. -
Handler
: Handlers send theLogRecord
to the required output destination, like the console or a file.Handler
is a base for subclasses likeStreamHandler
,FileHandler
,SMTPHandler
,HTTPHandler
, and more. These subclasses send the logging outputs to corresponding destinations, likesys.stdout
or a disk file. -
Formatter
: This is where you specify the format of the output by specifying a string format that lists out the attributes that the output should contain.
Out of these, we mostly deal with the objects of the Logger
class, which are instantiated using the module-level function logging.getLogger(name)
. Multiple calls to getLogger()
with the same name
will return a reference to the same Logger
object, which saves us from passing the logger objects to every part where it’s needed. Here’s an example:
import logging
logger = logging.getLogger('example_logger')
logger.warning('This is a warning')
This is what you’d get:
This is a warning
00:00
Up until this point we’ve been using the default logger, which is named root
. This is great for quick logging, but for more robust applications we need something more customizable.
00:13
The logging
module contains several classes we can instantiate to create our own customized logger.
00:21
The first two classes are called Logger
and LogRecord
. The Logger
class simply represents our custom logger. We’ll be instantiating this and then using custom methods in order to customize it.
00:36
Then we have LogRecord
, which we won’t actually be instantiating ourselves. Loggers automatically create LogRecord
objects for each event that we log. This LogRecord
contains information about the event, such as the logger name, the function, the line number, the message, and more.
00:58
Information from the LogRecord
s are what populate the log output that we see.
01:05
Next is the Handler
and the Formatter
. The handler’s job is to send the LogRecord
to the required output destination.
01:14
We won’t directly instantiate the Handler
class. Instead, we’ll instantiate a subclass. Each subclass of Handler
represents a destination. There’s StreamHandler
for outputting to destinations like stdout
, FileHandler
for outputting to a file, SMTPHandler
for emailing logging messages, and HTTPHandler
for sending log messages to a web server through GET
and POST
HTTP methods, and many more. Finally, the Formatter
class is used to specify the string format of the output, like we saw earlier.
01:54
Let’s see how we can create our own custom logger. We can use the getLogger()
method to create a new Logger
object. Here, 'example_logger'
is the name of our new logger, and we create a WARNING
level event by calling the warning()
method on this new logger, just as we did before.
02:15
But you’ll notice that the output of this program is just the message that says, This is a warning
. By default, our custom logger has no formatting, so we’re not seeing the logger name or severity level logged.
02:30
That’s where the Formatter
class will come in handy. Let’s see how we can use all of these classes together to create a simple logging system that can output both to stdout
and to a file simultaneously.
victorariasvanegas on June 3, 2020
Yeah your’re rigth, the mistake is here 02:12
Glenn Lehman on Jan. 13, 2023
The code in the text is correct.
Become a Member to join the conversation.
Kalesis on July 30, 2019
Hello, in the video the code is wrong. Dice “logging” instead of “logger”. regards