-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.py
75 lines (63 loc) · 2.54 KB
/
logger.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
-----------------------
LEVELS:
-----------------------
DEBUG - monitoring HLR API stages (turn off by default), DB work
INFO - regular events (monitoring)
SUCCESS - HLR response received
WARNINGS - untrasted user activity, bad responses, wrong user typing, etc.
ERRORS - connection crashing, no money, etc.
CRITICAL - total crash, stop app error
-----------------------
ACTION FILTERS:
-----------------------
admin_do - administrator activity (action with permission)
user_do - user activity
response_full - HLR result (full dict for file log)
response_short - HLR result (only main status for console view)
request - new message from user (admin includes) with phone numbers to check
regular - DB call, service functions, info events, etc.
"""
import sys
from loguru import logger
logger.remove() # clean default sink print
"""
Log files rules
"""
logger.add("hlr_errors.log",
rotation='1 MB',
format="{time:DD.MM.YYYY}\t{time:HH:mm:ss}\t{level} (from {module}): {message}",
level='WARNING')
logger.add("hlr_responses.log",
rotation='1Mb',
format="{time:DD.MM.YYYY}\t{time:HH:mm:ss}\t{message}",
filter=lambda record: record["extra"] == {'action': 'response_full'})
logger.add("hlr_debug.log",
rotation='1Mb',
format="{time:DD.MM.YYYY}\t{time:HH:mm:ss}\t{level} (from {module}): {message}",
level="DEBUG",
encoding='UTF-8')
"""
Console print rules
"""
# Warnings, Errors, Critical events
logger.add(sys.stdout,
format="<red>{time:DD.MM.YYYY} {time:HH:mm:ss} {level} (from {module}): {message}</red>",
level='WARNING')
# Admin actions
logger.add(sys.stdout,
format="{time:DD.MM.YYYY} {time:HH:mm:ss} <yellow>{level} (from {module}): {message}</yellow>",
filter=lambda record: record["extra"] == {'action': 'admin_do'})
# Users actions
logger.add(sys.stdout,
format="{time:DD.MM.YYYY} {time:HH:mm:ss} {level}: {message}",
filter=lambda record: record["extra"] == {'action': 'user_do'})
# New HLR response
logger.add(sys.stdout,
format="{time:DD.MM.YYYY} {time:HH:mm:ss} <magenta>{level}: {message}</magenta>",
filter=lambda record: record["extra"] == {'action': 'response_short'})
# New HLR request
logger.add(sys.stdout,
format="{time:DD.MM.YYYY} {time:HH:mm:ss} <cyan>REQUEST: {message}</cyan>",
filter=lambda record: record["extra"] == {'action': 'request'})
logger.debug('Logger starts')