-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlogger.py
executable file
·49 lines (41 loc) · 1.65 KB
/
logger.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 logging
import datetime
import time
import os
import sys
def get_handlers(log_file=True, log_stdout=True):
handlers = []
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%m-%d-%y')
if not os.path.isdir(os.path.join('.', 'log')):
os.mkdir(os.path.join('.', 'log'))
log_filepath = os.path.join('.', 'log', date + '.log')
log_fmt = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
if log_file:
file_handler = logging.FileHandler(log_filepath)
file_handler.setFormatter(log_fmt)
handlers.append(file_handler)
if log_stdout:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_fmt)
handlers.append(console_handler)
return handlers
def set_logger(name='root', log_file=True, log_stdout=True):
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%m-%d-%y')
if not os.path.isdir(os.path.join('.', 'log')):
os.mkdir(os.path.join('.', 'log'))
log_filepath = os.path.join('.', 'log', date + '.log')
log_fmt = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')
root_logger = logging.getLogger(name)
if log_file:
file_handler = logging.FileHandler(log_filepath)
file_handler.setFormatter(log_fmt)
root_logger.addHandler(file_handler)
if log_stdout:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_fmt)
root_logger.addHandler(console_handler)
def get_logger(name='root', log_file=True, log_stdout=True):
set_logger(name, log_file, log_stdout)
return logging.getLogger(name)