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

Add console handler for logging #337

Open
niksirbi opened this issue Oct 31, 2024 · 6 comments · May be fixed by #412
Open

Add console handler for logging #337

niksirbi opened this issue Oct 31, 2024 · 6 comments · May be fixed by #412
Assignees
Labels
bug Something isn't working

Comments

@niksirbi
Copy link
Member

Our current logging configuration only adds a rotating file handler, and does not configure a console handler. This means:

  • All log messages at the DEBUG level and above are written to the log file.
  • No log messages are output to the console.

What we would actually want:

  • INFO, WARNING, and ERROR messages should be printed both to the log file and the console.
  • DEBUG messages should be written only to the log file.

To achieve this behaviour, we need to add a console handler to our logger and set the appropriate log levels for both handlers.

@niksirbi
Copy link
Member Author

We could also use loguru or fancylog to reduce the amount of logging configuration we have to do on our side.

If we go with fancylog, we'd have to first address this issue.

@sfmig
Copy link
Contributor

sfmig commented Nov 15, 2024

naive question:

is it a matter of adding a

import logging
logging.getLogger().setLevel(logging.INFO)

?

EDIT: we confirm this was indeed naive 😬

I had in my notes that this logging tutorial was useful so linking here in case it helps

@niksirbi niksirbi added bug Something isn't working and removed enhancement New optional feature labels Nov 15, 2024
@niksirbi niksirbi moved this from 🤔 Triage to 📝 Todo in movement progress tracker Nov 15, 2024
@niksirbi
Copy link
Member Author

Todays discussions with @sfmig about logging made me look into this issue again, and I think this may be a one-line-ish fix.

The problem is that the log_warning utility only logs the warning message, and doesn't actually emit a warning! 🤦🏼
This is totally on me!

It should be changed to sth like this:

def log_warning(message: str, logger_name: str = "movement"):
    """Log a warning message and emit a UserWarning.

    Parameters
    ----------
    message : str
        The warning message.
    logger_name : str, optional
        The name of the logger to use. Defaults to "movement".

    """
    logger = logging.getLogger(logger_name)
    logger.warning(message)
    warnings.warn(message, UserWarning, stacklevel=2)

@niksirbi niksirbi linked a pull request Feb 11, 2025 that will close this issue
7 tasks
@niksirbi
Copy link
Member Author

I've started attempting a fix in #412

I think it works, based on the fact that warning messages that we were missing before started appearing now.
For example, attempting to import sample_data without an internet now results in:

/Users/nsirmpilatze/Code/NIU/movement/movement/sample_data.py:105: UserWarning: Failed to download the newest sample metadata file. Will use the existing local version instead.

The absence of this message was what made us notice the problem in the first place, right @sfmig?

If I'm right #412 is sufficient to close this issue. The problem now is that tests now emit >100 warnings, because of all the times they hit a log_warning statement. We have to filter those somehow, to minimise pytest noise.

@sfmig
Copy link
Contributor

sfmig commented Feb 12, 2025

The absence of this message was what made us notice the problem in the first place, right @sfmig?

I think so, yes!

The problem now is that tests now emit >100 warnings

Would this be reduced if we set the logging level to INFO rather than DEBUG by default (so the opposite change of PR #411 )

@niksirbi
Copy link
Member Author

niksirbi commented Feb 12, 2025

Would this be reduced if we set the logging level to INFO rather than DEBUG by default (so the opposite change of PR #411 )

No that's unrelated I think. The warnings are emitted because of the warnings.warn statement I added in #411, not by the logging of those warnings (which was happening before anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: 📝 Todo
Development

Successfully merging a pull request may close this issue.

3 participants