-
Notifications
You must be signed in to change notification settings - Fork 0
/
f_logging.py
37 lines (31 loc) · 1.27 KB
/
f_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
"""Определяет логирование процесса."""
import os
import codecs
from typing import Union
from f_getconfig import getconfig
from datetime import datetime
timestamp = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
opt_logname: str = getconfig()['settings']['logname']
def check_logging_opt() -> Union[bool, str]:
"""Проверяет параметр логирования в конфиге."""
opt_logging: str = getconfig()['settings']['logging']
if opt_logging == 'True':
return True
elif opt_logging == 'False':
return False
else:
return 'ERR'
def writelog(logmsg: str) -> None:
"""Пишет лог, если так указано в конфиге."""
if os.path.isfile(opt_logname) is False:
pass
elif check_logging_opt() is True:
if logmsg == 'init':
with codecs.open(opt_logname, 'a', 'utf-8') as log:
log.write(f'\n\n{timestamp} - Start Program\n----------\n')
else:
with codecs.open(opt_logname, 'a', 'utf-8') as log:
log.write(f'{timestamp} - {logmsg}\n')
elif check_logging_opt() == 'ERR':
with codecs.open(opt_logname, 'a', 'utf-8') as log:
log.write(f'{timestamp} - Logging config error!\n')