Skip to content

Commit 91322d9

Browse files
Liad NoamLiad Noam
Liad Noam
authored and
Liad Noam
committed
Changed logging config to be received by separate parameters for simplicity, removed option to receive configuration as a dict, added .vscode to gitignore
1 parent 88713de commit 91322d9

File tree

7 files changed

+53
-89
lines changed

7 files changed

+53
-89
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ dmypy.json
134134

135135
# Pyre type checker
136136
.pyre/
137+
138+
# VSCode
139+
.vscode/

.vscode/settings.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

app/config.py.example

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
2222
ICAL_VERSION = '2.0'
2323
PRODUCT_ID = '-//Our product id//'
2424

25-
# email
25+
# EMAIL
2626
email_conf = ConnectionConfig(
2727
MAIL_USERNAME=os.getenv("MAIL_USERNAME") or "user",
2828
MAIL_PASSWORD=os.getenv("MAIL_PASSWORD") or "password",
@@ -35,15 +35,12 @@ email_conf = ConnectionConfig(
3535
)
3636

3737
# LOGGER
38-
LOGGER = {
39-
"logger": {
40-
"path": "./var/log",
41-
"filename": "calendar.log",
42-
"level": "error",
43-
"rotation": "20 days",
44-
"retention": "1 month",
45-
"format": ("<level>{level: <8}</level> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
46-
" - <cyan>{name}</cyan>:<cyan>{function}</cyan>"
47-
" - <level>{message}</level>")
48-
}
49-
}
38+
LOG_PATH = "./var/log"
39+
LOG_FILENAME = "calendar.log"
40+
LOG_LEVEL = "error"
41+
LOG_ROTATION_INTERVAL = "20 days"
42+
LOG_RETENTION_INTERVAL = "1 month"
43+
LOG_FORMAT = ("<level>{level: <8}</level>"
44+
" <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
45+
" - <cyan>{name}</cyan>:<cyan>{function}</cyan>"
46+
" - <level>{message}</level>")

app/internal/logger_customizer.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
21
import sys
3-
from typing import Union, Dict
42

53
from pathlib import Path
64
from loguru import logger, _Logger as Logger
@@ -13,34 +11,38 @@ class LoggerConfigError(Exception):
1311
class LoggerCustomizer:
1412

1513
@classmethod
16-
def make_logger(cls, config_file_or_dict: Union[Path, Dict],
17-
logger_name: str) -> Logger:
18-
"""Creates a loguru logger from given configuration path or dict.
14+
def make_logger(cls, log_path: Path,
15+
log_filename: str,
16+
log_level: str,
17+
log_rotation_interval: str,
18+
log_retention_interval: str,
19+
log_format: str) -> Logger:
20+
"""Creates a logger from given configurations
1921
2022
Args:
21-
config_file_or_dict (Union[Path, Dict]): Path to logger
22-
configuration file or dictionary of configuration
23-
logger_name (str): Logger instance created from configuration
23+
log_path (Path): Path where the log file is located
24+
log_filename (str):
25+
26+
log_level (str): The level we want to start logging from
27+
log_rotation_interval (str): Every how long the logs
28+
would be rotated
29+
log_retention_interval (str): Amount of time in words defining
30+
how long the log will be kept
31+
log_format (str): The logging format
2432
2533
Raises:
2634
LoggerConfigError: Error raised when the configuration is invalid
2735
2836
Returns:
2937
Logger: Loguru logger instance
3038
"""
31-
32-
config = cls.load_logging_config(config_file_or_dict)
3339
try:
34-
logging_config = config.get(logger_name)
35-
logs_path = logging_config.get('path')
36-
log_file_path = logging_config.get('filename')
37-
3840
logger = cls.customize_logging(
39-
file_path=Path(logs_path) / Path(log_file_path),
40-
level=logging_config.get('level'),
41-
retention=logging_config.get('retention'),
42-
rotation=logging_config.get('rotation'),
43-
format=logging_config.get('format')
41+
file_path=Path(log_path) / Path(log_filename),
42+
level=log_level,
43+
retention=log_retention_interval,
44+
rotation=log_rotation_interval,
45+
format=log_format
4446
)
4547
except (TypeError, ValueError) as err:
4648
raise LoggerConfigError(
@@ -90,21 +92,3 @@ def customize_logging(cls,
9092
)
9193

9294
return logger
93-
94-
@classmethod
95-
def load_logging_config(cls, config: Union[Path, Dict]) -> Dict:
96-
"""Loads logging configuration from file or dict
97-
98-
Args:
99-
config (Union[Path, Dict]): Path to logging configuration file
100-
101-
Returns:
102-
Dict: Configuration parsed as dictionary
103-
"""
104-
if isinstance(config, Path):
105-
with open(config) as config_file:
106-
used_config = json.load(config_file)
107-
else:
108-
used_config = config
109-
110-
return used_config

app/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
from app.dependencies import (
77
MEDIA_PATH, STATIC_PATH, templates)
88
from app.routers import agenda, event, profile, email, invitation
9-
109
from app.internal.logger_customizer import LoggerCustomizer
11-
1210
from app import config
1311

1412
models.Base.metadata.create_all(bind=engine)
@@ -18,7 +16,12 @@
1816
app.mount("/media", StaticFiles(directory=MEDIA_PATH), name="media")
1917

2018
# Configure logger
21-
logger = LoggerCustomizer.make_logger(config.LOGGER, "logger")
19+
logger = LoggerCustomizer.make_logger(config.LOG_PATH,
20+
config.LOG_FILENAME,
21+
config.LOG_LEVEL,
22+
config.LOG_ROTATION_INTERVAL,
23+
config.LOG_RETENTION_INTERVAL,
24+
config.LOG_FORMAT)
2225
app.logger = logger
2326

2427

tests/logger_fixture.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@
1010

1111
@pytest.fixture(scope='module')
1212
def logger_instance():
13-
_logger = LoggerCustomizer.make_logger(config.LOGGER, "logger")
14-
15-
return _logger
16-
17-
18-
@pytest.fixture(scope='module')
19-
def logger_external_configuration():
20-
from pathlib import Path
21-
config_path = Path(__file__).parent
22-
config_path = config_path.absolute() / 'logging_config.json'
23-
_logger = LoggerCustomizer.make_logger(config_path, "logger1")
13+
_logger = LoggerCustomizer.make_logger(config.LOG_PATH,
14+
config.LOG_FILENAME,
15+
config.LOG_LEVEL,
16+
config.LOG_ROTATION_INTERVAL,
17+
config.LOG_RETENTION_INTERVAL,
18+
config.LOG_FORMAT)
2419

2520
return _logger
2621

tests/test_logger.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
import pytest
44

55
from app.internal.logger_customizer import LoggerCustomizer, LoggerConfigError
6+
from app import config
67

78

89
class TestLogger:
9-
@staticmethod
10-
def test_configuration_file(caplog, logger_external_configuration):
11-
with caplog.at_level(logging.ERROR):
12-
logger_external_configuration.error('Testing configuration ERROR')
13-
assert 'Testing configuration' in caplog.text
14-
1510
@staticmethod
1611
def test_log_debug(caplog, logger_instance):
1712
with caplog.at_level(logging.DEBUG):
@@ -38,18 +33,10 @@ def test_log_critical(caplog, logger_instance):
3833

3934
@staticmethod
4035
def test_bad_configuration():
41-
bad_config = {
42-
"logger": {
43-
"path": "./var/log",
44-
"filename": "calendar.log",
45-
"level": "eror",
46-
"rotation": "20 days",
47-
"retention": "1 month",
48-
"format": ("<level>{level: <8}</level> "
49-
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
50-
"- <cyan>{name}</cyan>:<cyan>{function}</cyan>"
51-
" - <level>{message}</level>")
52-
}
53-
}
5436
with pytest.raises(LoggerConfigError):
55-
LoggerCustomizer.make_logger(bad_config, 'logger')
37+
LoggerCustomizer.make_logger(config.LOG_PATH,
38+
config.LOG_FILENAME,
39+
'eror',
40+
config.LOG_ROTATION_INTERVAL,
41+
config.LOG_RETENTION_INTERVAL,
42+
config.LOG_FORMAT)

0 commit comments

Comments
 (0)