Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by
In python, When utilizing the logging module for logging purposes. Is it best practice to characterize a logger for each class?

Considering a few things would be repetitive, for example, document log area, I was considering abstracting logging to its own class and import an occurrence into every one of my classes required logging. Anyway, I don't know whether this is the best practice or not?
closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
When using the logging module in Python for logging purposes, it is generally recommended to create a logger for each class or module. This practice offers several benefits:

Modularity: Having a separate logger for each class/module allows for better organization and isolation of log messages. It helps in distinguishing logs specific to a particular component, making it easier to locate and analyze them.

Flexibility: Class/module-specific loggers offer greater flexibility in configuring logging levels, formatters, and handlers. Each component can have its own logging behavior, tailored to its unique requirements.

Code Readability: Having dedicated loggers for each class/module enhances code readability. Log messages are associated with their respective components, providing clear context and making the code easier to understand.

To address the concern of repetitive code, you can consider abstracting the logging functionality into a separate utility class or module. This utility class can handle common logging configurations, such as formatting and handlers. By importing an instance of this utility class into each class/module, you centralize the logging configuration while still maintaining individual loggers for each component.
0 votes
by (26.4k points)
edited by

Use JSON or YAML logging design/configuration - After Python 2.7, you can stack logging setup from a dict. It implies you can stack the logging design/configuration from a JSON or YAML document. 

Yaml Example -

version: 1

disable_existing_loggers: False

formatters:

    simple:

        format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

handlers:

    console:

        class: logging.StreamHandler

        level: DEBUG

        formatter: simple

        stream: ext://sys.stdout

    info_file_handler:

        class: logging.handlers.RotatingFileHandler

        level: INFO            

        formatter: simple

        filename: info.log

        maxBytes: 10485760 # 10MB

        backupCount: 20

        encoding: utf8

    error_file_handler:

        class: logging.handlers.RotatingFileHandler

        level: ERROR            

        formatter: simple

        filename: errors.log

        maxBytes: 10485760 # 10MB

        backupCount: 20

        encoding: utf8

loggers:

    my_module:

        level: ERROR

        handlers: [console]

        propagate: no

root:

    level: INFO

    handlers: [console, info_file_handler, error_file_handler]

Are you looking for a good python tutorial? Join the python course fast and gain more knowledge in python.

Watch this video tutorial for more information.

0 votes
by (25.7k points)
When using the logging module in Python for logging purposes, the best practice is to define a logger for each class or module. This approach allows for better organization and granularity in logging.

Defining a logger for each class/module has several advantages:

Modularity: Each class/module has its own logger, making it easier to track and isolate logs specific to that component. It provides a clear separation of logging concerns and helps in identifying the source of log messages.

Flexibility: By having class/module-specific loggers, you can easily configure logging levels, formatters, and handlers for each component independently. This gives you more flexibility in customizing logging behavior based on the specific needs of each class/module.

Code Readability: Having a dedicated logger for each class/module improves code readability. Log messages associated with a particular class/module are localized, making it easier to understand the context and purpose of the logs.

Regarding the concern of repetitive code, you can consider abstracting the logging functionality into a separate utility class or module. This utility class can encapsulate common logging configurations, such as log formatting and log handlers. You can then import an instance of this utility class into each class/module that requires logging. This approach centralizes the logging configuration while still allowing individual loggers for each component.
0 votes
by (19k points)
In Python's logging module, it is recommended to define a logger for each class or module. This offers modularity, flexibility, and improves code readability. To avoid repetition, you can abstract logging into a separate utility class or module.

Related questions

Browse Categories

...