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 initial logger support #24

Merged
merged 13 commits into from
Jul 4, 2023
Merged

Add initial logger support #24

merged 13 commits into from
Jul 4, 2023

Conversation

XapaJIaMnu
Copy link
Contributor

Will finish this after other PRs are in and then i'll also fix the tests:
Currently the output looks something like this:

[2023-06-29 17:09:41] [Trainer] [INFO] Starting stage start
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 0
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 1
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 2
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 3
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 4
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 5
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 6
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 7
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 8
[2023-06-29 17:09:41] [Trainer] [INFO] Reading clean for epoch 9

@XapaJIaMnu XapaJIaMnu requested a review from jelmervdl June 29, 2023 16:13
Copy link
Contributor

@jelmervdl jelmervdl left a comment

Choose a reason for hiding this comment

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

There's a warn() from the warnings package somewhere in the codebase as well. Should we also replace that one with logging.warning()?

src/opustrainer/trainer.py Show resolved Hide resolved
src/opustrainer/trainer.py Show resolved Hide resolved
src/opustrainer/trainer.py Show resolved Hide resolved
tests/test_logger.py Outdated Show resolved Hide resolved
# Check if the log is the same as the reference log. This is more complicated as we have a time field
with open('contrib/test-data/test_enzh_config_plain_expected.log', 'r', encoding='utf-8') as reffile:
reference: List[str] = reffile.readlines()
# Loglist has one extra `\n` compared to reference list, due to stderr flushing an extra empty line?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as i can tell, this is harmless.

@XapaJIaMnu XapaJIaMnu marked this pull request as ready for review July 4, 2023 02:38
@XapaJIaMnu
Copy link
Contributor Author

There's a warn() from the warnings package somewhere in the codebase as well. Should we also replace that one with logging.warning()?

Done. Ready to review now.

handlers.append(logging.StreamHandler(stream=stderr))
if outputfilename is not None:
handlers.append(logging.FileHandler(filename=outputfilename))
# This is the only logger we'd ever use. However during testing, due to the context, logger can't be recreated,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not entirely happy about this, but the alternative is to create different loggers which I don't want to do.

"""Sets up the logger with the necessary settings. Outputs to both file and stderr"""
loggingformat = '[%(asctime)s] [Trainer] [%(levelname)s] %(message)s'
handlers: List[logging.StreamHandler[TextIO] | logging.StreamHandler[TextIOWrapper]] = []
# disable_stderr is to be used only when testing the logger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

grumpy noises

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you want strings for loglevel? Why not just export the log levels we support and reference logger.WARNING instead of "WARNING" in code? Additional upside is that you can't reference log levels that don't exist because the symbols to do so are not there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why StreamHandler[TextIO]? I can't find any reference of StreamHandler inheriting Generic.

Btw If you're okay with it I want to go through your changes and make them Python 3.8 compatible (which basically means not using generics where they're not supported, and using Union instead of |)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My type checker flagged it as an incompatible type assignment and this was the inferred type. I guess UNION should be used instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the WARNING I thought it'd be easier to use a text based log level from modifiers, in case users want to add their own logging, rather than getting to python internals.

That being said, logging.getLevelNamesMapping() is only available in python 3.11 so I shouldn't be using it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, since we define the level at runtime, I had to have str -> loglevel conversion anyways..

Copy link
Contributor

@jelmervdl jelmervdl left a comment

Choose a reason for hiding this comment

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

There's a line print(f"[Trainer] Empty field in {self.dataset.name} line:\"{line}\", skipping...") on trainer.py:212 that should probably also be a warning to the logger.

"""Sets up the logger with the necessary settings. Outputs to both file and stderr"""
loggingformat = '[%(asctime)s] [Trainer] [%(levelname)s] %(message)s'
handlers: List[logging.StreamHandler[TextIO] | logging.StreamHandler[TextIOWrapper]] = []
# disable_stderr is to be used only when testing the logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you want strings for loglevel? Why not just export the log levels we support and reference logger.WARNING instead of "WARNING" in code? Additional upside is that you can't reference log levels that don't exist because the symbols to do so are not there.

"""Sets up the logger with the necessary settings. Outputs to both file and stderr"""
loggingformat = '[%(asctime)s] [Trainer] [%(levelname)s] %(message)s'
handlers: List[logging.StreamHandler[TextIO] | logging.StreamHandler[TextIOWrapper]] = []
# disable_stderr is to be used only when testing the logger
Copy link
Contributor

Choose a reason for hiding this comment

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

Why StreamHandler[TextIO]? I can't find any reference of StreamHandler inheriting Generic.

Btw If you're okay with it I want to go through your changes and make them Python 3.8 compatible (which basically means not using generics where they're not supported, and using Union instead of |)

@XapaJIaMnu
Copy link
Contributor Author

Go ahead and do your changes, I think i'm good with everything. Runs on CSD3

@jelmervdl
Copy link
Contributor

Looks like 3.8 isn't complaining about the weird generics your type checker is introducing (I checked the source, they don't make any sense to me, but okay). Fixed an encoding argument that's only available since 3.9. But now tests are okay on macOS/python3.8.

LGTM! 🥳

@XapaJIaMnu XapaJIaMnu merged commit f695e04 into main Jul 4, 2023
@XapaJIaMnu XapaJIaMnu deleted the logger branch July 4, 2023 12:55
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.

2 participants