question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging etiquette

See original GitHub issue

The current default logging behaviour by pyam is rude, if you actually want to configure logging.

Usually, one can expect a zen-like experience by just invoking

import logging
logging.basicConfig(level="ERROR")

But pyam’s initialization in https://github.com/IAMconsortium/pyam/blob/f071e790de7fdc3ab6a1135e4b31be7f395cff15/pyam/__init__.py#L14 and https://github.com/IAMconsortium/pyam/blob/f071e790de7fdc3ab6a1135e4b31be7f395cff15/pyam/__init__.py#L21-L26 does not respect these settings.

With those, one has to additionally issue

pyam.logger.setLevel(level="ERROR")

Annoyingly,

logging.getLogger().setLevel("ERROR")

will still show INFO messages coming from pyam, since they are already written out into stderr by the pyam logger.

That’s why the Configuring logging for a library section in the logging tutorial has this note:

It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers.

I see two alternatives for improvement:

Best practice

Move the initialization into a separate routine setup_logger and tell everyone to use

import pyam
pyam.setup_logging()

(setup_logging should take keyword arguments like level)

If they forget about it, it’s not that problematic either as long as we get in tandem rid of the NullHandler, because since python 3.2 the default behaviour of the logging library for a LogRecord which bubbles through the logging tree without encountering a Handler is to send the message to stderr, if it is at least a warning (without timestamp and loglevel), as described in What happens if no configuration is provided.

Looks like so: image

Least change (but still slightly rude)

Get the root logger in __init__.py and check whether the user has set up any handlers before, as like:

rootLogger = logging.getLogger()
if not rootLogger.hasHandlers():
    # add handlers to rootLogger

That way, the user experience for the average pyam user is unchanged, but one can configure logging, by using

logging.basicConfig(level="ERROR")

as long as one does it before import pyam.

And it is possible to make it shut up with

logging.getLogger().setLevel("ERROR")

even after the import is through.

Thoughts? @danielhuppmann @gidden @znicholls . I do strongly favor the Best practice option but would prepare a PR for either!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
giddencommented, Oct 29, 2020

Hi folks, thought it could be useful to chime in here. First, love the discussion =)

I think it’s important to highlight that there are largely three ‘user types’ for pyam:

  1. analysis users (work entirely in notebook environments, coming to pyam from the IPCC notebooks among other places)
  2. power users (using pyam in scripting environments)
  3. developers (us, actively updating pyam code)

Notebooks are a first-class environment for pyam where we want to support fairly verbose logging statements to help guide these users, hence the original (rude) design of high-jacking the logging.

However, it is fundamentally important to also provide first-class support for the other two user categories, and @coroa has provided a nice example of where we have failed to do so.

Therefore, I would support a solution that raises our log level sufficiently if in a notebook environment, which I think @coroa has also provided. What do others think?

0reactions
danielhuppmanncommented, Oct 29, 2020

Thanks a lot, I agree that 2a seems like a really nice solution and striking a great balance between our different user groups!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Geocaching Etiquette 201: Finding and Logging – Official Blog
Most geocachers would say that our game is pretty simple. Find the cache, sign the log, log the cache online. But when a...
Read more >
Logging: Rules of thumb - HelloTech - HelloFresh
Logging is one of the pillars of monitoring, together with metrics and tracing. In most cases, developers worry about logs only when ...
Read more >
Understanding Log Scales and Log Rules
A log rule is a mathematical formula or table that gives an estimate of the net yield of lumber in board feet for...
Read more >
Logging Etiquette : r/geocaching - Reddit
If I do log, I don't want to be rude, but I feel embarrassed if cachers from out of town come and look...
Read more >
The 5 Java logging rules - Java Code Geeks - 2022
The 5 Java logging rules · Rule 1. Logging is for readers · Rule 2. Match the logging levels with the execution environment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found