-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b6a2a01
Add initial logger support
XapaJIaMnu 4da4712
Always log to stderr
XapaJIaMnu 22e49e0
Merge branch 'main' into logger
XapaJIaMnu 5bf35a7
Fix existing tests
XapaJIaMnu f994a74
Make typing happy
XapaJIaMnu e08d605
Log tests
XapaJIaMnu c80581e
Extra comment in test
XapaJIaMnu 451a22e
Move things around and test log_once functionality
XapaJIaMnu 04937ee
Make tests more proper and remove warnings package usage
XapaJIaMnu c45fdc8
Test the log level as well
XapaJIaMnu 80b3252
Make it workable on older python
XapaJIaMnu b9c8545
Backport to 3.8
jelmervdl 6d0ff04
Small formatting fix
XapaJIaMnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[2023-07-04 02:48:11] [Trainer] [INFO] Starting stage start | ||
[2023-07-04 02:48:11] [Trainer] [INFO] Reading clean for epoch 0 | ||
[2023-07-04 02:48:11] [Trainer] [INFO] Reading clean for epoch 1 | ||
[2023-07-04 02:48:11] [Trainer] [INFO] Reading clean for epoch 2 | ||
[2023-07-04 02:48:11] [Trainer] [INFO] Reading clean for epoch 3 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 4 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 5 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 6 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 7 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 8 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] Reading clean for epoch 9 | ||
[2023-07-04 02:48:12] [Trainer] [INFO] waiting for trainer to exit. Press ctrl-c to be more aggressive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from io import TextIOWrapper | ||
import logging | ||
from sys import stderr | ||
from typing import List, TextIO | ||
from functools import lru_cache | ||
|
||
def getLogLevel(name: str) -> int: | ||
"""Incredibly, i can't find a function that will do this conversion, other | ||
than setLevel, but setLevel doesn't work for calling the different log level logs.""" | ||
if name.upper() in logging.getLevelNamesMapping(): | ||
return logging.getLevelNamesMapping()[name.upper()] | ||
else: | ||
logging.log(logging.WARNING, "unknown log level level used: " + name + " assuming warning...") | ||
return logging.WARNING | ||
|
||
def log(msg: str, loglevel: str = "INFO") -> None: | ||
level = getLogLevel(loglevel) | ||
logging.log(level, msg) | ||
|
||
|
||
@lru_cache(None) | ||
def log_once(msg: str, loglevel: str = "INFO") -> None: | ||
"""A wrapper to log, to make sure that we only print things once""" | ||
log(msg, loglevel) | ||
|
||
|
||
def setup_logger(outputfilename: str | None = None, loglevel: str = "INFO", disable_stderr: bool=False) -> None: | ||
"""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 | ||
# When testing the logger directly, we don't want to write to stderr, because in order to read | ||
# our stderr output, we have to use redirect_stderr, which however makes all other tests spit | ||
# as it interferes with unittest' own redirect_stderr. How nice. | ||
if not disable_stderr: | ||
handlers.append(logging.StreamHandler(stream=stderr)) | ||
if outputfilename is not None: | ||
handlers.append(logging.FileHandler(filename=outputfilename)) | ||
logging.basicConfig(handlers=handlers, encoding='utf-8', level=getLogLevel(loglevel), format=loggingformat, datefmt='%Y-%m-%d %H:%M:%S') | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grumpy noises
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 ofStreamHandler
inheritingGeneric
.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|
)There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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..