-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic logging.py
43 lines (40 loc) · 1.21 KB
/
basic 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
import logging
import os
import sys
logger = logging.getLogger()
def main():
logger.debug(f'This is a debug message')
logger.info(f'This is a info message')
logger.warning(f'This is a warning message')
logger.critical(f'This is a critical message')
logger.fatal(f'This is a fatal message')
logger.error(f'This is a error message')
if __name__ == '__main__':
log_file = f'{os.path.basename(__file__).split(".")[0]}.log'
clear_latest_log = True
#Clear log file
if clear_latest_log:
if os.path.exists(log_file):
open(log_file, 'w').close()
#Setup logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
encoding='utf-8',
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler(sys.stdout)
]
)
#Set specific logging levels
pass
logging.getLogger('sys').setLevel(logging.CRITICAL)
try:
main()
error = 0
except Exception as e:
logger.warning(f'A fatal error has occured due to {repr(e)}')
error = 1
finally:
exit(error)