-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/SK-558 | Adds Python's logging framework to the client (#484)
* Cleaned up client logging. Using Python's built-in logging framework instead of print statements. Also updated the language a bit to make it more consistent.
- Loading branch information
1 parent
36cf7f7
commit 192f3f1
Showing
11 changed files
with
130 additions
and
112 deletions.
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,56 @@ | ||
import logging | ||
import logging.config | ||
|
||
import urllib3 | ||
|
||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | ||
logging.getLogger("urllib3").setLevel(logging.ERROR) | ||
|
||
handler = logging.StreamHandler() | ||
logger = logging.getLogger() | ||
logger.addHandler(handler) | ||
logger.setLevel(logging.DEBUG) | ||
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S') | ||
handler.setFormatter(formatter) | ||
|
||
|
||
def set_log_level_from_string(level_str): | ||
""" | ||
Set the log level based on a string input. | ||
""" | ||
# Mapping of string representation to logging constants | ||
level_mapping = { | ||
'CRITICAL': logging.CRITICAL, | ||
'ERROR': logging.ERROR, | ||
'WARNING': logging.WARNING, | ||
'INFO': logging.INFO, | ||
'DEBUG': logging.DEBUG, | ||
} | ||
|
||
# Get the logging level from the mapping | ||
level = level_mapping.get(level_str.upper()) | ||
|
||
if not level: | ||
raise ValueError(f"Invalid log level: {level_str}") | ||
|
||
# Set the log level | ||
logger.setLevel(level) | ||
|
||
|
||
def set_log_stream(log_file): | ||
""" | ||
Redirect the log stream to a specified file, if log_file is set. | ||
""" | ||
if not log_file: | ||
return | ||
|
||
# Remove existing handlers | ||
for h in logger.handlers[:]: | ||
logger.removeHandler(h) | ||
|
||
# Create a FileHandler | ||
file_handler = logging.FileHandler(log_file) | ||
file_handler.setFormatter(formatter) | ||
|
||
# Add the file handler to the logger | ||
logger.addHandler(file_handler) |
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
Oops, something went wrong.