Skip to content
Arvin's Blog

Hands-on experience on Python logging

2 min read

Basic Usage

  • import logging
    logging.basicConfig()
    logging.info("messages")
  • What does logging.basicConfig() do? => creates a StreamHandler with a default Formatter and adding it to the root logger.

Concepts & Flow

  • *LogRecord
  • *Filter
    • Both Handler and Logger can add filters
  • Formatter: different formatters for different handlers
  • Handler
    • StreamHandler
    • FileHandler
  • Logger
    • can link to multiple (shared) handlers (many-to-many relation)
    • has a manager
  • See Logging Flow for details

Why designed in this way? The benefits ⇒ Flexibility

  • Multiple Loggers arranged in a namespace hierarchy using dots as separators.
    • When propagate is true, foo.bar logger will propagate the messages to its parent, the foo logger.
  • Multiple destinations

The way I used it

More Tips & Tricks

More Q&A

  • How about logging in other programming languages?

References

© 2024 by Arvin's Blog. All rights reserved.
Theme by LekoArts