Completed
Push — master ( 931044...cb1112 )
by Andreas
22s queued 11s
created

f_logging.writelog()   B

Complexity

Conditions 8

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 14
rs 7.3333
c 0
b 0
f 0
cc 8
nop 1
1
"""Определяет логирование процесса."""
2
import os
3
import codecs
4
from f_getconfig import getconfig
5
from datetime import datetime
6
7
timestamp = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
8
9
opt_logging = getconfig()['settings']['logging']
10
opt_logname = getconfig()['settings']['logname']
11
12
13
def check_logging_opt():
14
    """Проверяет параметр логирования в конфиге."""
15
    if opt_logging is True:
16
        return True
17
    elif opt_logging is False:
18
        return False
19
    else:
20
        return 'ERR'
21
22
23
def writelog(logmsg):
24
    """Пишет лог, если так указано в конфиге."""
25
    if os.path.isfile(opt_logname) is False:
26
        pass
27
    elif check_logging_opt() is True:
28
        if logmsg == 'init':
29
            with codecs.open(opt_logname, 'a', 'utf-8') as log:
30
                log.write(f'\n\n{timestamp} - Start Program\n----------\n')
31
        else:
32
            with codecs.open(opt_logname, 'a', 'utf-8') as log:
33
                log.write(f'{timestamp} - {logmsg}\n')
34
    elif check_logging_opt() == 'ERR':
35
        with codecs.open(opt_logname, 'a', 'utf-8') as log:
36
            log.write('Logging config error!')
37