-
Notifications
You must be signed in to change notification settings - Fork 33
[Logging] Support use of loguru #454
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aca180c
support loguru
kylesayrs d634e52
add loguru
kylesayrs 3629d07
fix quality
kylesayrs 7b98675
address
kylesayrs 3aca1ac
Merge remote-tracking branch 'origin' into kylesayrs/loguru
kylesayrs 7391fc4
[Utils] Improve type hints for `deprecated`, only log once (#455)
kylesayrs 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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Logger configuration for Compressed Tensors. | ||
""" | ||
|
||
import os | ||
import sys | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, Optional | ||
|
||
from loguru import logger | ||
|
||
|
||
__all__ = ["LoggerConfig", "configure_logger", "logger"] | ||
|
||
|
||
# used by `support_log_once`` | ||
_logged_once = set() | ||
|
||
|
||
@dataclass | ||
class LoggerConfig: | ||
disabled: bool = False | ||
clear_loggers: bool = True | ||
console_log_level: Optional[str] = "INFO" | ||
log_file: Optional[str] = None | ||
log_file_level: Optional[str] = None | ||
|
||
|
||
def configure_logger(config: Optional[LoggerConfig] = None): | ||
""" | ||
Configure the logger for Compressed Tensors. | ||
This function sets up the console and file logging | ||
as per the specified or default parameters. | ||
|
||
Note: Environment variables take precedence over the function parameters. | ||
|
||
:param config: The configuration for the logger to use. | ||
:type config: LoggerConfig | ||
""" | ||
logger_config = config or LoggerConfig() | ||
|
||
# env vars get priority | ||
if bool(os.getenv("COMPRESSED_TENSORS_LOG_DISABLED")): | ||
logger_config.disabled = True | ||
if bool(os.getenv("COMPRESSED_TENSORS_CLEAR_LOGGERS")): | ||
logger_config.clear_loggers = True | ||
if (console_log_level := os.getenv("COMPRESSED_TENSORS_LOG_LEVEL")) is not None: | ||
logger_config.console_log_level = console_log_level.upper() | ||
if (log_file := os.getenv("COMPRESSED_TENSORS_LOG_FILE")) is not None: | ||
logger_config.log_file = log_file | ||
if (log_file_level := os.getenv("COMPRESSED_TENSORS_LOG_FILE_LEVEL")) is not None: | ||
logger_config.log_file_level = log_file_level.upper() | ||
|
||
if logger_config.disabled: | ||
logger.disable("compressed_tensors") | ||
return | ||
|
||
logger.enable("compressed_tensors") | ||
|
||
if logger_config.clear_loggers: | ||
logger.remove() | ||
|
||
if logger_config.console_log_level: | ||
# log as a human readable string with the time, function, level, and message | ||
logger.add( | ||
sys.stdout, | ||
level=logger_config.console_log_level.upper(), | ||
format="{time} | {function} | {level} - {message}", | ||
filter=support_log_once, | ||
) | ||
|
||
if logger_config.log_file or logger_config.log_file_level: | ||
log_file = logger_config.log_file or "compressed_tensors.log" | ||
log_file_level = logger_config.log_file_level or "INFO" | ||
# log as json to the file for easier parsing | ||
logger.add( | ||
log_file, | ||
level=log_file_level.upper(), | ||
serialize=True, | ||
filter=support_log_once, | ||
) | ||
|
||
|
||
def support_log_once(record: Dict[str, Any]) -> bool: | ||
""" | ||
Support logging only once using `.bind(log_once=True)` | ||
|
||
``` | ||
logger.bind(log_once=False).info("This will log multiple times") | ||
logger.bind(log_once=False).info("This will log multiple times") | ||
logger.bind(log_once=True).info("This will only log once") | ||
logger.bind(log_once=True).info("This will only log once") # skipped | ||
``` | ||
""" | ||
log_once = record["extra"].get("log_once", False) | ||
level = getattr(record["level"], "name", "none") | ||
message = hash(str(level) + record["message"]) | ||
|
||
if log_once and message in _logged_once: | ||
return False | ||
|
||
if log_once: | ||
_logged_once.add(message) | ||
kylesayrs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return True | ||
|
||
|
||
# invoke logger setup on import with default values enabling console logging with INFO | ||
# and disabling file logging | ||
configure_logger(config=LoggerConfig()) |
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
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.