-
Notifications
You must be signed in to change notification settings - Fork 101
Logger Update #43
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
Logger Update #43
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d52614
feat: can set logging through argument
justinmerrell 142c123
fix: change log import
justinmerrell 164f21e
fix: rename to log and fix tests
justinmerrell 5fb1dbe
fix: log tests
justinmerrell 2bec030
Update test_module_logger.py
justinmerrell 1b76228
Update test_module_logger.py
justinmerrell 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ env | |
.env | ||
.vscode | ||
test_wrapper.py | ||
test_logging.py | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
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
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 was deleted.
Oops, something went wrong.
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,113 @@ | ||
''' | ||
PodWorker | modules | logging.py | ||
|
||
Log Levels (Level - Value - Description) | ||
|
||
NOTSET - 0 - No logging is configured, the logging system is effectively disabled. | ||
DEBUG - 1 - Detailed information, typically of interest only when diagnosing problems. (Default) | ||
INFO - 2 - Confirmation that things are working as expected. | ||
WARN - 3 - An indication that something unexpected happened. | ||
ERROR - 4 - Serious problem, the software has not been able to perform some function. | ||
''' | ||
|
||
import os | ||
from dotenv import load_dotenv | ||
|
||
env_path = os.path.join(os.getcwd(), '.env') | ||
load_dotenv(env_path) # Load environment variables | ||
|
||
LOG_LEVELS = ['NOTSET', 'DEBUG', 'INFO', 'WARN', 'ERROR'] | ||
|
||
|
||
def _validate_log_level(log_level): | ||
''' | ||
Checks the debug level and returns the debug level name. | ||
''' | ||
if isinstance(log_level, str): | ||
log_level = log_level.upper() | ||
|
||
if log_level not in LOG_LEVELS: | ||
raise ValueError(f'Invalid debug level: {log_level}') | ||
|
||
return log_level | ||
|
||
if isinstance(log_level, int): | ||
if log_level < 0 or log_level > 4: | ||
raise ValueError(f'Invalid debug level: {log_level}') | ||
|
||
return LOG_LEVELS[log_level] | ||
|
||
raise ValueError(f'Invalid debug level: {log_level}') | ||
|
||
|
||
class RunPodLogger: | ||
'''Singleton class for logging.''' | ||
|
||
__instance = None | ||
level = _validate_log_level(os.environ.get('RUNPOD_DEBUG_LEVEL', 'DEBUG')) | ||
|
||
def __new__(cls): | ||
if RunPodLogger.__instance is None: | ||
RunPodLogger.__instance = object.__new__(cls) | ||
return RunPodLogger.__instance | ||
|
||
def set_level(self, new_level): | ||
''' | ||
Set the debug level for logging. | ||
Can be set to the name or value of the debug level. | ||
''' | ||
self.level = _validate_log_level(new_level) | ||
self.info(f'Log level set to {self.level}') | ||
|
||
def log(self, message, message_level='INFO'): | ||
''' | ||
Log message to stdout if RUNPOD_DEBUG is true. | ||
''' | ||
if self.level == 'NOTSET': | ||
return | ||
|
||
level_index = LOG_LEVELS.index(self.level) | ||
if level_index > LOG_LEVELS.index(message_level) and message_level != 'TIP': | ||
return | ||
|
||
print(f'{message_level.ljust(7)}| {message}', flush=True) | ||
return | ||
|
||
def secret(self, secret_name, secret): | ||
''' | ||
Censors secrets for logging. | ||
Replaces everything except the first and last characters with * | ||
''' | ||
secret = str(secret) | ||
redacted_secret = secret[0] + '*' * (len(secret)-2) + secret[-1] | ||
self.info(f"{secret_name}: {redacted_secret}") | ||
|
||
def debug(self, message): | ||
''' | ||
debug log | ||
''' | ||
self.log(message, 'DEBUG') | ||
|
||
def info(self, message): | ||
''' | ||
info log | ||
''' | ||
self.log(message, 'INFO') | ||
|
||
def warn(self, message): | ||
''' | ||
warn log | ||
''' | ||
self.log(message, 'WARN') | ||
|
||
def error(self, message): | ||
''' | ||
error log | ||
''' | ||
self.log(message, 'ERROR') | ||
|
||
def tip(self, message): | ||
''' | ||
tip log | ||
''' | ||
self.log(message, 'TIP') |
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,51 @@ | ||
''' Tests for runpod.serverless.modules.rp_logger ''' | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
|
||
from runpod.serverless.modules import rp_logger | ||
|
||
|
||
class TestLogger(unittest.TestCase): | ||
''' Tests for rp_logger ''' | ||
|
||
def test_default_log_level(self): | ||
''' | ||
Tests that the default log level is DEBUG | ||
''' | ||
logger = rp_logger.RunPodLogger() | ||
|
||
self.assertEqual(logger.level, "DEBUG") | ||
|
||
def test_singleton(self): | ||
''' | ||
Tests that the logger is a singleton | ||
''' | ||
logger1 = rp_logger.RunPodLogger() | ||
logger2 = rp_logger.RunPodLogger() | ||
|
||
self.assertIs(logger1, logger2) | ||
|
||
def test_set_log_level(self): | ||
''' | ||
Tests that the log level can be set | ||
''' | ||
logger = rp_logger.RunPodLogger() | ||
|
||
logger.set_level("INFO") | ||
self.assertEqual(logger.level, "INFO") | ||
|
||
logger.set_level("WARN") | ||
self.assertEqual(logger.level, "WARN") | ||
|
||
def test_call_log(self): | ||
''' | ||
Tests that the logger can be called | ||
''' | ||
log = rp_logger.RunPodLogger() | ||
|
||
with patch("runpod.serverless.modules.rp_logger.RunPodLogger.log") as mock_log: | ||
|
||
log.warn("Test log message") | ||
|
||
mock_log.assert_called_once_with("Test log message", "WARN") |
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.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information