This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the log configuration -- direct to stdout (#38)
- Loading branch information
Showing
4 changed files
with
36 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[loggers] | ||
keys=root,src | ||
|
||
[handlers] | ||
keys=consoleHandler | ||
|
||
[formatters] | ||
keys=sampleFormatter | ||
|
||
[logger_root] | ||
level=INFO | ||
handlers=consoleHandler | ||
propagate=0 | ||
|
||
[logger_src] | ||
level=DEBUG | ||
qualname=src | ||
handlers=consoleHandler | ||
propagate=0 | ||
|
||
[handler_consoleHandler] | ||
class=StreamHandler | ||
level=DEBUG | ||
formatter=sampleFormatter | ||
args=(sys.stdout,) | ||
|
||
[formatter_sampleFormatter] | ||
format=%(asctime)s %(levelname)s %(name)s %(message)s |
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,22 +1,6 @@ | ||
"""Project Global Constants.""" | ||
import argparse | ||
import logging | ||
from pathlib import Path | ||
|
||
PROJECT_ROOT = Path(__file__).parent.parent | ||
QUERY_PATH = PROJECT_ROOT / Path("src/sql") | ||
|
||
|
||
def parse_log_level() -> str: | ||
"""Parses Global Log Level from runtime arguments (default = INFO)""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"-l", "--log-level", type=str, default="INFO", help="set log level" | ||
) | ||
args, _ = parser.parse_known_args() | ||
level: str = args.log_level.upper() | ||
logging.info(f"Global Log Level configured as {level}") | ||
return level | ||
|
||
|
||
LOG_LEVEL = parse_log_level() | ||
LOG_CONFIG_FILE = PROJECT_ROOT / Path("logging.conf") |
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,23 +1,15 @@ | ||
"""Easy universal log configuration """ | ||
import logging.config | ||
import sys | ||
from logging import Logger | ||
|
||
from src.environment import LOG_LEVEL | ||
from src.environment import LOG_CONFIG_FILE | ||
|
||
|
||
def set_log(name: str) -> Logger: | ||
""" | ||
Instantiates and returns a logger with the name defined by the caller. | ||
Used by any file which wants to inherit the global log level configuration | ||
and assigns the desired naming convention (usually `__name__`) | ||
""" | ||
"""Removes redundancy when setting log in each file""" | ||
|
||
log = logging.getLogger(name) | ||
try: | ||
log.setLevel(level=LOG_LEVEL) | ||
except ValueError: | ||
logging.error(f"Invalid log level: {LOG_LEVEL}") | ||
sys.exit(1) | ||
|
||
logging.config.fileConfig( | ||
fname=LOG_CONFIG_FILE.absolute(), disable_existing_loggers=True | ||
) | ||
return log |
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