-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
executable file
·56 lines (48 loc) · 1.32 KB
/
log.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 Zimeng Qiu <[email protected]>
# Licensed under the Apache License v2.0 - http://www.apache.org/licenses/
"""
Log streamers
"""
import logging
import os
import configs
_log_file = '{}/{}.log'.format(configs.log_dir, configs.timestamp)
def init_logger():
"""
Logger initializer
Returns:
logger
"""
if not os.path.exists(configs.log_dir):
try:
os.mkdir(configs.log_dir)
except IOError:
print("Can not create log file directory.")
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
fh = logging.FileHandler(_log_file)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
logger.addHandler(fh)
logger.addHandler(ch)
return logger
def p_logger(s, print_=True, log_=True):
"""
Print function redirection
Args:
s: stream text
print_: flag, will not print on screen if set to False
log_: flag, will not write into log file if set to False
Returns:
None
"""
if print_:
print(s)
if log_:
with open(_log_file, 'a+') as f_log:
f_log.write(s + '\n')