-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace print statements with logging
- defaults to only console logging - defaults to WARNING level - configurable with: `export APP_LOG_LEVEL=[INFO|DEBUG]`
- Loading branch information
Showing
3 changed files
with
74 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
*~ | ||
*.swp | ||
logs/ | ||
|
||
dist/* | ||
*/*.egg-info/* | ||
__pycache__ | ||
.DS_Store | ||
.DS_Store |
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,42 @@ | ||
import logging | ||
from logging.handlers import TimedRotatingFileHandler | ||
import os | ||
import socket | ||
from datetime import datetime | ||
|
||
LOG_FILE_BACKUP_COUNT = int(os.getenv('LOG_FILE_BACKUP_COUNT', '30')) | ||
LOG_ROTATION = "midnight" | ||
|
||
timestamp = datetime.today().strftime('%Y-%m-%d') | ||
|
||
|
||
def configure_logger(name): # pragma: no cover | ||
log_level = os.getenv("APP_LOG_LEVEL", "WARNING") | ||
log_dir = os.getenv("LOG_DIR", "logs/") | ||
# create log directory if it doesn't exist | ||
if not os.path.exists(log_dir): | ||
os.makedirs(log_dir) | ||
|
||
log_file_path = os.path.join(log_dir, "jp2_remediator.log") | ||
formatter = logging.Formatter( | ||
'%(levelname)s - %(asctime)s - %(name)s - %(message)s') | ||
|
||
console_handler = logging.StreamHandler() | ||
console_handler.setFormatter(formatter) | ||
|
||
logger = logging.getLogger(name) | ||
logger.addHandler(console_handler) | ||
# Defaults to console logging | ||
if os.getenv("CONSOLE_LOGGING_ONLY", "true") == "false": | ||
# make log_file_path if it doesn't exist | ||
# os.makedirs(log_file_path, exist_ok=True) | ||
file_handler = TimedRotatingFileHandler( | ||
filename=log_file_path, | ||
when=LOG_ROTATION, | ||
backupCount=LOG_FILE_BACKUP_COUNT | ||
) | ||
file_handler.setFormatter(formatter) | ||
logger.addHandler(file_handler) | ||
|
||
logger.setLevel(log_level) | ||
return logger |
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