-
-
Notifications
You must be signed in to change notification settings - Fork 212
Changes proposed in #885. Don't register handlers by default. #889
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
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
81fa74d
Changes proposed in #885. Don't register handlers by default.
PGijsbers 3572841
Delay file creation until log emit. Correctly read from config.
PGijsbers de6a9e6
Remove loading/storing log level references.
PGijsbers e3ade22
_create_log_handlers now returns early if called a second time
PGijsbers 6a4ca29
Fix type errors.
PGijsbers 6424073
Update changelog.
PGijsbers 4def357
Test remove register file log handler to see if CI works.
PGijsbers 69fbe50
Undo last change. test server ssl works agian.
PGijsbers 719edec
Bump scikit-learn version to 0.22
PGijsbers 6f2b844
Scikit-learn 0.22 does not install properly.
PGijsbers ebc0a5f
Install scikit-learn through pip instead.
PGijsbers 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,47 +7,79 @@ | |
| import logging | ||
| import logging.handlers | ||
| import os | ||
| from typing import cast | ||
| from typing import Tuple, cast | ||
|
|
||
| from io import StringIO | ||
| import configparser | ||
| from urllib.parse import urlparse | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| openml_logger = logging.getLogger('openml') | ||
| console_handler = None | ||
| file_handler = None | ||
|
|
||
|
|
||
| def configure_logging(console_output_level: int, file_output_level: int): | ||
| """ Sets the OpenML logger to DEBUG, with attached Stream- and FileHandler. """ | ||
| # Verbosity levels as defined (https://github.com/openml/OpenML/wiki/Client-API-Standards) | ||
| # don't match Python values directly: | ||
| verbosity_map = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG} | ||
| def _create_log_handlers(): | ||
| """ Creates but does not attach the log handlers. """ | ||
| global console_handler, file_handler | ||
| if console_handler is not None or file_handler is not None: | ||
| logger.debug("Requested to create log handlers, but they are already created.") | ||
| return | ||
|
|
||
| openml_logger = logging.getLogger('openml') | ||
| openml_logger.setLevel(logging.DEBUG) | ||
| message_format = '[%(levelname)s] [%(asctime)s:%(name)s] %(message)s' | ||
| output_formatter = logging.Formatter(message_format, datefmt='%H:%M:%S') | ||
|
|
||
| console_stream = logging.StreamHandler() | ||
| console_stream.setFormatter(output_formatter) | ||
| console_stream.setLevel(verbosity_map[console_output_level]) | ||
| console_handler = logging.StreamHandler() | ||
| console_handler.setFormatter(output_formatter) | ||
|
|
||
| one_mb = 2**20 | ||
| one_mb = 2 ** 20 | ||
| log_path = os.path.join(cache_directory, 'openml_python.log') | ||
| file_stream = logging.handlers.RotatingFileHandler(log_path, maxBytes=one_mb, backupCount=1) | ||
| file_stream.setLevel(verbosity_map[file_output_level]) | ||
| file_stream.setFormatter(output_formatter) | ||
| file_handler = logging.handlers.RotatingFileHandler( | ||
| log_path, maxBytes=one_mb, backupCount=1, delay=True | ||
| ) | ||
| file_handler.setFormatter(output_formatter) | ||
|
|
||
| openml_logger.addHandler(console_stream) | ||
| openml_logger.addHandler(file_stream) | ||
| return console_stream, file_stream | ||
|
|
||
| def _convert_log_levels(log_level: int) -> Tuple[int, int]: | ||
| """ Converts a log level that's either defined by OpenML/Python to both specifications. """ | ||
| # OpenML verbosity level don't match Python values directly: | ||
| openml_to_python = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG} | ||
| python_to_openml = {logging.DEBUG: 2, logging.INFO: 1, logging.WARNING: 0, | ||
| logging.CRITICAL: 0, logging.ERROR: 0} | ||
| # Because the dictionaries share no keys, we use `get` to convert as necessary: | ||
| openml_level = python_to_openml.get(log_level, log_level) | ||
| python_level = openml_to_python.get(log_level, log_level) | ||
| return openml_level, python_level | ||
|
|
||
|
|
||
| def _set_level_register_and_store(handler: logging.Handler, log_level: int): | ||
| """ Set handler log level, register it if needed, save setting to config file if specified. """ | ||
| oml_level, py_level = _convert_log_levels(log_level) | ||
| handler.setLevel(py_level) | ||
|
|
||
| if openml_logger.level > py_level or openml_logger.level == logging.NOTSET: | ||
PGijsbers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| openml_logger.setLevel(py_level) | ||
|
|
||
| if handler not in openml_logger.handlers: | ||
| openml_logger.addHandler(handler) | ||
|
|
||
|
|
||
| def set_console_log_level(console_output_level: int): | ||
| """ Set console output to the desired level and register it with openml logger if needed. """ | ||
| global console_handler | ||
| _set_level_register_and_store(cast(logging.Handler, console_handler), console_output_level) | ||
|
|
||
|
|
||
| def set_file_log_level(file_output_level: int): | ||
| """ Set file output to the desired level and register it with openml logger if needed. """ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "register it with openml logger" is implementation detail. Maybe just saying that "configured logging to a file (if not already) and sets the desired level". |
||
| global file_handler | ||
| _set_level_register_and_store(cast(logging.Handler, file_handler), file_output_level) | ||
|
|
||
|
|
||
| # Default values (see also https://github.com/openml/OpenML/wiki/Client-API-Standards) | ||
| _defaults = { | ||
| 'apikey': None, | ||
| 'server': "https://www.openml.org/api/v1/xml", | ||
| 'verbosity': 0, # WARNING | ||
| 'file_verbosity': 2, # DEBUG | ||
| 'cachedir': os.path.expanduser(os.path.join('~', '.openml', 'cache')), | ||
| 'avoid_duplicate_runs': 'True', | ||
| 'connection_n_retries': 2, | ||
|
|
@@ -176,9 +208,7 @@ def _setup(): | |
|
|
||
|
|
||
| def _parse_config(): | ||
| """Parse the config file, set up defaults. | ||
| """ | ||
|
|
||
| """ Parse the config file, set up defaults. """ | ||
| config = configparser.RawConfigParser(defaults=_defaults) | ||
|
|
||
| if not os.path.exists(config_file): | ||
|
|
@@ -189,6 +219,7 @@ def _parse_config(): | |
| "create an empty file there." % config_file) | ||
|
|
||
| try: | ||
| # The ConfigParser requires a [SECTION_HEADER], which we do not expect in our config file. | ||
| # Cheat the ConfigParser module by adding a fake section header | ||
| config_file_ = StringIO() | ||
| config_file_.write("[FAKE_SECTION]\n") | ||
|
|
@@ -255,7 +286,4 @@ def set_cache_directory(cachedir): | |
| ] | ||
|
|
||
| _setup() | ||
|
|
||
| _console_log_level = cast(int, _defaults['verbosity']) | ||
| _file_log_level = cast(int, _defaults['file_verbosity']) | ||
| console_log, file_log = configure_logging(_console_log_level, _file_log_level) | ||
| _create_log_handlers() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.