forked from aicpp/cloudsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
37 lines (28 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
#!/usr/bin/python
import os
import logging
import logging.handlers
import tempfile
"""
Logger helper
"""
def defaultLogFilePath(fileName):
""" Return temporary filepath with script name. use with __file__ """
scriptname = os.path.splitext(os.path.basename(fileName))[0]
tempdir = tempfile.gettempdir()
return os.path.join(tempdir, scriptname + '.log')
def createLogFileHandler(filePath, maxBytes=1048576, backupCount=3):
fileHandler = logging.handlers.RotatingFileHandler(filePath, maxBytes=maxBytes, backupCount=backupCount)
formatDt = u"%Y-%m-%d %H:%M:%S"
fmtFile = logging.Formatter(fmt=u"%(asctime)s.%(msecs)-3d %(module)-15s %(levelname)-7s %(message)s", datefmt=formatDt)
fileHandler.setFormatter(fmtFile)
return fileHandler
def createConsoleHandler():
conHandler = logging.StreamHandler()
conHandler.setLevel(logging.DEBUG)
fmtCon = logging.Formatter(fmt=u"%(asctime)s %(message)s", datefmt=u"%H:%M:%S")
conHandler.setFormatter(fmtCon)
return conHandler
def createFileRotationHandler(filePath, maxBytes=1048576, backupCount=3):
handler = logging.handlers.RotatingFileHandler(filePath, maxBytes=maxBytes, backupCount=backupCount)
return handler