-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (37 loc) · 1.38 KB
/
main.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
import time
import datetime
# class OneLineExceptionFormatter(logging.Formatter):
# def format(self, record):
# result = super().format(record)
# if record.exc_text:
# result = result.replace("\n", " | ")
# return result
import logging
import logging.config
# Say i have saved my configuration under ./myconf.conf
logging.config.fileConfig('logging_conf.conf')
logger = logging.getLogger(__name__)
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('logger_new.log')
fh.setLevel(logging.DEBUG)
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# formatter = OneLineExceptionFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# fh.setFormatter(formatter)
# ch.setFormatter(formatter)
logger.addHandler(fh)
# logger.addHandler(ch)
def error_function():
raise Exception('Error info')
while True:
logger.info('Logger info msg '+str(datetime.datetime.now()))
logger.debug('Logger info debug '+str(datetime.datetime.now()))
logger.info('Some extra value msg before', extra={'extra_key':'before'})
try:
error_function()
except:
logger.exception("Some exception occure")
time.sleep(10)
logger.info('Some extra value msg after', extra={'extra_key':'after'})