Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger creation removed from __init__.py #27

Closed
wants to merge 2 commits into from

Conversation

epogrebnyak
Copy link
Contributor

Fixes #11

@danijar
Copy link
Owner

danijar commented Aug 10, 2019

As mentioned in #11, I think the logger should be created on import so that the user can do logging.getLogger('handout') to change the log level etc. What do you think?

handout/tools.py Outdated

def get_logger():
logger = logging.getLogger('handout')
logger.setLevel(logging.INFO) # A user can change this level later.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding your comment for user levels, the user can change it globally by running this line, for example:

logger.setLevel(logging.DEBUG)

Also we actually do not provide any other logging messages than info().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, actually he wouldn't change the log level for that particular logger... need a rethink.

@epogrebnyak
Copy link
Contributor Author

epogrebnyak commented Aug 10, 2019

Here is an idea - one function is logger_config(), and it is called on import, the other fucniton in logger_provide() and it simply returns a proper logger in Handout.

This way the logger is available upon import and configurable by the user, but we keep __init__.py cleaner, more avoiding the hidden state.

@danijar
Copy link
Owner

danijar commented Aug 11, 2019

This seems a bit too complicated to me. I don't think moving the logic from __init__.py into tools.py makes it less hidden. On the contrary, now it's not clear anymore where and how often the logger is created. Moreover, I don't see the need for the get_logger() function that is just an alias for an existing function in the logging module. In other words, let's just add the one line to __init__.py to check whether there are any handlers already, if you don't mind.

@epogrebnyak
Copy link
Contributor Author

epogrebnyak commented Aug 11, 2019

Well, you can make a one-liner change in __init__.py, but the place is rather hidden, otherwise it would not have taken that much time to find the source of error for #11.

Something happening in __init__.py except for the imports in a bit shaky.

get_logger is not just a sugar for the logging function - it returns a specific logger named 'handout'. Otherwise it is a question how a programmer knows to use this name for the logger, also for configuring it to a different level?

moving the logic from init.py into tools.py makes it less hidden.

It does - there is a dedicated filelogger.py now. If I saw one in codebase, would have saved me a ton of time tracing the duplicate messages error, just as example.

@epogrebnyak
Copy link
Contributor Author

I can see some preference in keeping the codebase compact without logger.py though, so the decision is up to you.

@danijar danijar closed this in 8b5a769 Aug 11, 2019
danijar added a commit that referenced this pull request Aug 11, 2019
@danijar
Copy link
Owner

danijar commented Aug 11, 2019

You raise a valid point. I added the one line check and put a comment in the Handout class to tell contributors where to look for the logging configuration. As you said in your second comment, having a separate file for the logger seems a bit too much to me. There are many parts of the code that could all have separate files but every time we do that the codes gets more spread out and harder to edit. I'm closing the PR since I've added the fix -- thanks a lot for finding the bug in the first place!

@epogrebnyak
Copy link
Contributor Author

Dev comment is a good idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicated logging messages when importing module twice
2 participants