-
Notifications
You must be signed in to change notification settings - Fork 1
/
OurLog.py
69 lines (60 loc) · 2.31 KB
/
OurLog.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
# -*- coding: utf-8 -*-
import logging
import getpass
import sys
import os
from config import dirs
import datetime
class OurLog(object):
def __init__(self, init_file=None):
user = getpass.getuser()
self.logger = logging.getLogger(user)
self.level=logging.DEBUG #debug < info< warning< error< critical
self.logger.setLevel(self.level)
if init_file == None:
logFile = sys.argv[1]
else:
logFile = init_file
today = datetime.datetime.now()
logFile=dirs['LOGDIR']+logFile[:-4]+'_'+str(today.year)+'_'+str(today.month)+'_'+str(today.day)+'.log'
if not os.path.exists(dirs['LOGDIR']):
os.makedirs(dirs['LOGDIR'])
self.formatter = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s')
self.filelogHand = logging.FileHandler(logFile, encoding="utf8")
self.filelogHand.setFormatter(self.formatter)
self.filelogHand.setLevel(self.level)
self.logHandSt = logging.StreamHandler()
self.logHandSt.setLevel(self.level)
self.logHandSt.setFormatter(self.formatter)
def debug(self, msg):
self.logger.handlers.clear()
self.logger.addHandler(self.filelogHand)
self.logger.addHandler(self.logHandSt)
self.logger.debug(msg)
def info(self, msg):
self.logger.handlers.clear()
self.logger.addHandler(self.filelogHand)
self.logger.addHandler(self.logHandSt)
self.logger.info(msg)
def warn(self, msg):
self.logger.handlers.clear()
self.logger.addHandler(self.filelogHand)
self.logger.addHandler(self.logHandSt)
self.logger.warning(msg)
def error(self, msg):
self.logger.handlers.clear()
self.logger.addHandler(self.filelogHand)
self.logger.addHandler(self.logHandSt)
self.logger.error(msg)
def critical(self, msg):
self.logger.handlers.clear()
self.logger.addHandler(self.filelogHand)
self.logger.addHandler(self.logHandSt)
self.logger.critical(msg)
if __name__=='__main__':
mylog=OurLog()
mylog.debug("I'm debug")
mylog.info("I'm info")
mylog.warn("I'm warning")
mylog.error("I'm error")
mylog.critical("I'm critical")