Skip to content

Commit 02a07f3

Browse files
committed
Slight modifications to RotatingCLPFileHandler settings
1 parent a6dd459 commit 02a07f3

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

application/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from werkzeug.serving import WSGIRequestHandler
2828

2929
global logger
30+
os.makedirs('logs', exist_ok=True)
3031

3132
try:
3233
with open('log_config.yaml') as config_file:

application/logger.py renamed to application/log_handlers/handlers.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
from clp_logging.handlers import CLPFileHandler, CLPLogLevelTimeout, EOF_CHAR, FLUSH_FRAME
55
from datetime import datetime
66

7+
78
class RotatingCLPFileHandler(CLPFileHandler):
89
"""
910
Extends `CLPFileHandler` to support file size-based log rotation.
1011
"""
1112

1213
def __init__(
13-
self,
14-
filename_prefix: str,
15-
log_dir: Path,
16-
max_bytes: int,
17-
backup_count: int = 5,
18-
mode: str = "ab",
19-
enable_compression: bool = True,
20-
timestamp_format: str = "%Y%m%d_%H%M%S",
21-
loglevel_timeout: Optional[CLPLogLevelTimeout] = None,
14+
self,
15+
filename_prefix: str,
16+
log_dir: Path,
17+
max_bytes: int,
18+
backup_count: int = 20,
19+
mode: str = "ab",
20+
enable_compression: bool = True,
21+
timestamp_format: str = "%Y-%m-%d_%H-%M-%S",
22+
loglevel_timeout: Optional[CLPLogLevelTimeout] = None,
2223
) -> None:
2324
self.filename_prefix = filename_prefix
2425
self.log_dir = Path(log_dir)
@@ -39,7 +40,7 @@ def __init__(
3940
timestamp_format=timestamp_format,
4041
loglevel_timeout=loglevel_timeout,
4142
)
42-
43+
4344
def _generate_log_filename(self) -> Path:
4445
"""
4546
Generate a log filename with the timestamp and prefix.
@@ -59,7 +60,6 @@ def _should_rotate(self) -> bool:
5960
except FileNotFoundError:
6061
return False
6162

62-
6363
def _rotate(self) -> None:
6464
"""
6565
Perform log rotation by finalizing the current stream, creating a new log file,
@@ -125,4 +125,4 @@ def _write(self, loglevel: int, msg: str) -> None:
125125
"""
126126
if self._should_rotate():
127127
self._rotate()
128-
super()._write(loglevel, msg)
128+
super()._write(loglevel, msg)

log_config.yaml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,17 @@ formatters:
77
datefmt: '%Y-%m-%d %H:%M:%S'
88

99
handlers:
10-
file:
11-
class: logging.FileHandler
12-
level: NOTSET
13-
formatter: default
14-
filename: 'example.log' # Output file path
15-
encoding: utf-8
16-
mode: w # 'w' to overwrite each time, 'a' to append
17-
1810
CLP_file:
19-
class: application.logger.RotatingCLPFileHandler
20-
level: NOTSET
11+
class: application.log_handlers.handlers.RotatingCLPFileHandler
12+
level: DEBUG
2113
formatter: default
22-
filename_prefix: 'ictrl_'
14+
filename_prefix: 'ictrl'
2315
log_dir: 'logs'
2416
max_bytes: 5242880 # Rotate after 5 MB
2517
backup_count: 20 # Keep 20 backup files
2618
enable_compression: True
27-
timestamp_format: '%Y-%m-%d_%H-%M-%S'
19+
timestamp_format: '%Y-%m-%d'
2820

2921
root:
30-
level: NOTSET
31-
handlers: [file, CLP_file]
22+
level: DEBUG
23+
handlers: [CLP_file]

0 commit comments

Comments
 (0)