Skip to content

mccright/PythonLoggingExamples

 
 

Repository files navigation

My Python Logging Reminders

These examples are just reminders/demonstrations to help me get started on any given python script.

For a more detailed description of Python logging refer to the official manual. There is a useful diagram outlining the flow of log event information in loggers and handlers at: https://docs.python.org/3.10/howto/logging.html#logging-flow. And don't ignore the logging cookbook if you have the urge to do your own thing https://docs.python.org/3/howto/logging-cookbook.html

Approaches

  • The old print way:
def log(loglevel, component, message):
    print(f"{datetime.now(timezone('UCT'))} \
[{loglevel}] {component}: {message}", file=sys.stderr)

Then log something with:

log("INFO", __file__, f"Using important_var: {important_var}")
  • The standard python logging module: trylogging/LogTestLogging.py
    Usage:
    from LogTestLogging import init_logging
    logger = init_logging()
    logger.info('info testing message')

  • The loguru module -- because it was so easy to configure: tryloguru/LogTestLoGuru.py
    Usage:
    from LogTestLoGuru import init_logging
    logger = init_logging()
    logger.info('info testing message')

  • requestsDebugging: A useful approach to requests debug logging especially valuable when attempting to deal with a poorly documented or otherwise opaque API. This is well explained by Ben Hoey at bhoey.com.

  • The next...

Install

None. These are not modules, just little code reminders.
That said, tryloguru/LogTestLoGuru.py requires pip3 install loguru --user.

What Should You Log?

See some ideas at: https://github.com/mccright/rand-notes/blob/master/Application-Logging.md

Explore later

Additional References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages