-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_logging.py
47 lines (36 loc) · 1.48 KB
/
setup_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
__author__ = 'Khiem Doan'
__github__ = 'https://github.com/khiemdoan'
__email__ = '[email protected]'
import sys
from pathlib import Path
from loguru import logger
def setup_logging(filename: str) -> None:
logger.remove()
# Write all logs to console
time = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>'
level = '<level>{level: <8}</level>'
locate = '<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan>'
message = '<level>{message}</level>'
if _check_inside_container():
format = f'{level} | {locate} - {message}'
else:
format = f'{time} | {level} | {locate} - {message}'
logger.add(sys.stdout, level='INFO', enqueue=True, format=format, colorize=True)
default = {
'enqueue': True,
'rotation': '2MB',
'retention': 5,
'compression': 'tar.gz',
}
# Write only error logs to file
format = '{time} | {level: <8} | {name}:{function}:{line} - {message}'
logger.add(f'logs/{filename}-error.log', level='ERROR', format=format, **default)
# Write all logs to file
logger.add(f'logs/{filename}.log', level='INFO', serialize=True, **default)
def _check_inside_container() -> bool:
# https://stackoverflow.com/questions/23513045/how-to-check-if-a-process-is-running-inside-docker-container
if Path('/.dockerenv').is_file(): # Docker container
return True
if Path('/run/.containerenv').is_file(): # Podman container
return True
return False