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

[Feature Request & Solution] Troublehooting guide to prevent duplicated log messages #2898

Open
2 tasks
Daraan opened this issue Apr 30, 2024 · 0 comments
Open
2 tasks
Labels
enhancement Enhanvement request

Comments

@Daraan
Copy link

Daraan commented Apr 30, 2024

(If you are looking for a solution for this problem see end of this post)

🚀 Feature Request

Motivation

Is your feature request related to a problem? Please describe.

Users (me included) regularly run into the problem that logs are duplicated, e.g.: #1656 #1012.

The source of this problem is that other modules (e.g. lightning) have their own logging handlers. Hydra adds its console handler to the root logger resulting in logging messages being processed twice.

I've outlined solutions below, however it was some struggle to come up with them, a better documentation / troubleshooting guide for this problem in the documentation (https://hydra.cc/docs/configure_hydra/logging/ or https://hydra.cc/docs/tutorials/basic/running_your_app/logging/) would be helpful

Pitch

Describe the solution you'd like

A (partial) integration of the solutions provided below into the documentary to easier solve the duplicated logging message.
And / or, providing an extra job_logging yaml file option taking care of it.

Are you willing to open a pull request? (See CONTRIBUTING)

I could think of providing a .yaml file for this, however I am not yet sure about these points:

  • how should the file be named? Maybe non-root
  • How should the actual logger be named? Maybe __main__?
    This would work with the already promoted getLogger(__file__), however it would need a notice to use getLogger("__main__").getChild(__file__) or similar.

Additional context

✅ Solutions for duplicated log console

The problem is that hydra adds handlers on top of modules that also implement their own handlers.

One solution is to stop the other loggers manually from propagating to the root or to remove the handlers from other modules

Do not use root handler / hydra handler

# Will use the plain format by their module
logging.getLogger("lightning").propagate = False
logging.getLogger("torch").propagate = False

Do not use module handlers - only the hydra logger

sublogger = logging.getLogger("lightning")
for handler in sublogger.handlers:
    sublogger.remove(handler)

Adjust the config

Likely better is to prevent the console handler to be added to root via hydra, however you need to add you own handler to the logger you are using!

hydra:
  job_logging:
     root: 
        handlers: ['file']

For example use a non-root logger named "project_logger".
Note: you need to use logging.getLogger("project_logger") or logging.getLogger("project_logger").getChild(__file__) to access it.

hydra:
  job_logging:
    version : 1
    disable_existing_loggers : True
    loggers :
      project_logger : 
        level: 'INFO'
        handlers: ['console', 'file']
        # If propagate is True, the 'file' handler of root will be used
        propagate: False
    root :
      handlers : ['file']
@Daraan Daraan added the enhancement Enhanvement request label Apr 30, 2024
@Daraan Daraan changed the title [Feature Request] Troublehooting guide to prevent duplicated log messages [Feature Request & Solution] Troublehooting guide to prevent duplicated log messages Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhanvement request
Projects
None yet
Development

No branches or pull requests

1 participant