-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
48 lines (38 loc) · 1.2 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
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import os
import time
import logging
import utils.singleton as singleton
@singleton.Singleton
class Logger(object):
def __init__(self):
self.handler = None
self.formatter = None
self.logPath = './log'
self.filename = ''
self.level = logging.INFO
self.loggerMap = {}
def init(self):
self.createPath()
filenameFormat = self.logPath + '/%Y-%m-%d.log'
self.filename = time.strftime(filenameFormat)
self.handler = logging.FileHandler(self.filename, 'a+')
self.formatter = logging.Formatter(
'[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s',
'%H:%M:%S')
self.handler.setFormatter(self.formatter)
def getLogger(self, name):
logger = logging.getLogger(name)
if not name in self.loggerMap:
logger.addHandler(self.handler)
logger.setLevel(level=self.level)
self.loggerMap[name] = True
return logger
def createPath(self):
if os.path.exists(self.logPath):
return
os.mkdir(self.logPath)
def getLogger(name):
log = Logger()
return log.getLogger(name)