Skip to content

logging documentation is tough for beginners #98731

Closed
@quicknir

Description

@quicknir

Something of a perennial issue that I've seen is that folks:

a) complain that logging is hard to use, hard to configure. Somebody called logging an "advanced" module recently.
b) a lot of real world code gets written not following what most people with even moderate experience consider the most basic of best practices. Namely:
- using logging.warning instead of logger = logging.getLogger(__name__); logger.warning..
- library code behaving like it "owns" the root logger: making changes on the root logger, calling logging.basicConfig, heck even calling logging.warning falls into this category, even though it seems totally innocuous, since it can call basicConfig

Now, I think the logging module is fantastic, and that the documentation is generally excellent, and I don't think it's hard to use. However, when idiomatic usage of logging is not really that difficult, I struggle to understand why there isn't an example of idiomatic, correct usage, right on the very first page, as the first code example that people see (instead of showing logging.warning, which is something that should almost never be used).

It's somewhat hard to tell someone that logging isn't an advanced module, when I say "it's easy, just do logger = logging.getLogger(name) at the top of each file, and then..." but such code is only shown as a block in the "Advanced" section of the tutorial, which itself is a small link off the main page.

I am not suggesting any dramatic change to the overall documentation; but it is a bit dramatic I suppose insofar as I think it's important to change the very first thing that people see when they look at logging. Namely, on that very first screen, I would like to see code blocks like this:

# myapp.py
import logging
import mylib

logger = logging.getLogger(__name__)

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)
    logger.info('Started')
    mylib.do_something()
    logger.info('Finished')

if __name__ == '__main__':
    main()
# mylib.py
import logging
logger = logging.getLogger(__name__)

def do_something():
    logger.debug('Debug message')
    logger.warning('Warning message')

Along with a few accompanying sentences, before or after, explaining the basic ideas here (and yes, it should only take a few sentences), namely:

  • most files should only need to create a logger, and then use logger methods, and not have any other interactions.
  • main should be the place where logger configuration is handled (generally, and for simple use cases)
  • encourage the user to play around with the log levels

In my view, this is all of the starting information you need to start using logging well, at the very start. I use logging quite a lot, but 95% of the time at least, my real world production code still uses logging in this basic way, and it works fantastically. People new to logging often are not going to stick around for 30 minutes and read most of the documentation; let's make sure that if they only read one screen worth of docs on their first visit, it leaves them writing code they'll be happy with later.

I'm happy to submit a PR with a draft of the new first section (and slight removal of duplicated material in the tutorial) if there is some receptiveness to the ideas here.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions