File size: 1,407 Bytes
b84549f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
import sys

from .others import get_cur_time_str, is_in_jupyter_nb
from .file import ensure_dir


logger = logging.getLogger('zedl')
logger.setLevel(logging.DEBUG)
logger.propagate = False

if not is_in_jupyter_nb():
    formatter = logging.Formatter("%(asctime)s - %(filename)s[%(lineno)d] - %(levelname)s: %(message)s")
else:
    formatter = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s")
log_dir_path = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), './log')
if not os.path.exists(log_dir_path):
    os.mkdir(log_dir_path)

# file log
cur_time_str = get_cur_time_str()
log_file_path = os.path.join(log_dir_path, cur_time_str[0:8], cur_time_str[8:] + '.log')
ensure_dir(log_file_path)
file_handler = logging.FileHandler(log_file_path, mode='a')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# cmd log
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

logging.getLogger('nni').setLevel(logging.ERROR)

# copy file content to log file
with open(os.path.abspath(sys.argv[0]), 'r') as f:
    content = f.read()
    logger.debug('entry file content: ---------------------------------')
    logger.debug('\n' + content)
    logger.debug('entry file content: ---------------------------------')