This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhelper.py
131 lines (111 loc) · 4.08 KB
/
helper.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
#!/usr/bin/python3
import logging
import os
import time
def import_tf(device_id=-1, verbose=False):
"""
import tensorflow, set tensorflow graph load device, set tensorflow log level, return tensorflow instance
:param device_id: GPU id
:param verbose: tensorflow logging level
:return: tensorflow instance
"""
# set visible gpu, -1 is cpu
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' if device_id < 0 else str(device_id)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0' if verbose else '3'
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.DEBUG if verbose else tf.logging.ERROR)
return tf
def set_logger(context, log_dir, verbose=False):
"""
set logger
:param context: logger name
:param log_dir: log file dir
:param verbose: log level
:return: python logging instance
"""
return TNLog(log_dir, context, verbose)
class TNLog(object):
def __init__(self, log_dir, context, verbose):
if not os.path.exists(log_dir):
os.mkdir(log_dir)
self.context = context
self.root_log_dir = log_dir
self.verbose = verbose
self.formatter = logging.Formatter(
'%(asctime)s-%(levelname)s:' + self.context + ':%(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# create log dir
self.log_dir = time.strftime('%Y-%m-%d', time.localtime())
self._create_logger()
def _create_logger(self):
"""
create logger
:return:
"""
self.loggers = {}
if not os.path.exists(os.path.join(self.root_log_dir, self.log_dir)):
os.mkdir(os.path.join(self.root_log_dir, self.log_dir))
handlers = {logging.DEBUG: os.path.join(os.path.join(self.root_log_dir, self.log_dir), 'DEBUG.log'),
logging.INFO: os.path.join(os.path.join(self.root_log_dir, self.log_dir), 'INFO.log'),
logging.WARNING: os.path.join(os.path.join(self.root_log_dir, self.log_dir), 'WARNING.log'),
logging.ERROR: os.path.join(os.path.join(self.root_log_dir, self.log_dir), 'ERROR.log')}
levels = handlers.keys()
for level in levels:
logger = logging.getLogger(str(level))
logger.setLevel(level)
file_handler = logging.FileHandler(handlers[level], encoding='utf-8')
file_handler.setLevel(level)
file_handler.setFormatter(self.formatter)
logger.handlers = []
logger.addHandler(file_handler)
self.loggers[level] = logger
def _change_file_handler(self):
"""
change file handler
:return:
"""
if not time.strftime('%Y-%m-%d', time.localtime()) == self.log_dir:
self.loggers = {}
self.log_dir = time.strftime('%Y-%m-%d', time.localtime())
self._create_logger()
def info(self, msg):
"""
log info level
:param msg: log message
:return:
"""
print(self._time+'-INFO:%s:%s' % (self.context, msg), flush=True)
self._change_file_handler()
self.loggers[logging.INFO].info(msg)
def error(self, msg):
"""
log error level
:param msg: log message
:return:
"""
print(self._time+'-ERROR:%s:%s' % (self.context, msg), flush=True)
self._change_file_handler()
self.loggers[logging.ERROR].error(msg)
def debug(self, msg):
"""
log debug level
:param msg: log message
:return:
"""
if self.verbose:
print(self._time+'-DEBUG:%s:%s' % (self.context, msg), flush=True)
self._change_file_handler()
self.loggers[logging.DEBUG].debug(msg)
def warning(self, msg):
"""
log warning level
:param msg: log message
:return:
"""
print(self._time + '-ERROR:%s:%s' % (self.context, msg), flush=True)
self._change_file_handler()
self.loggers[logging.WARNING].error(msg)
@property
def _time(self):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())