forked from DingGuodong/LinuxBashShellScriptForOps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyLogging_u2.py
More file actions
59 lines (50 loc) · 2.02 KB
/
Copy pathpyLogging_u2.py
File metadata and controls
59 lines (50 loc) · 2.02 KB
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
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File: LinuxBashShellScriptForOps:pyLogging_u2.py
User: Guodong
Create Date: 2017/7/3
Create Time: 8:45
# TimedRotatingFileHandler, rotating log file with date, like 'name.log.2017-07-03'
"""
import time
import os
import logging
import logging.handlers
import sys
def initLoggerWithRotate(logPath="/var/log", logName=None, singleLogFile=True):
current_time = time.strftime("%Y%m%d%H")
logRootPath = logPath # logRootPath known as logTopPath
if logName is not None and not singleLogFile:
logFullPath = os.path.join(logRootPath, logName)
logFilename = logName + "_" + current_time + ".log"
elif logName is not None and singleLogFile:
logFullPath = os.path.join(logRootPath, logName)
logFilename = logName + ".log"
else:
logFullPath = logRootPath
logName = "default"
logFilename = logName + ".log"
if not os.path.exists(logFullPath):
os.makedirs(logFullPath)
logFilename = os.path.join(logFullPath, logFilename)
else:
logFilename = os.path.join(logFullPath, logFilename)
logger = logging.getLogger(logName)
log_formatter = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d %(name)s %(levelname)s: %(message)s",
"%Y-%m-%d %H:%M:%S")
file_handler = logging.handlers.TimedRotatingFileHandler(logFilename, when='d', interval=1, backupCount=30)
file_handler.setFormatter(log_formatter)
stream_handler = logging.StreamHandler(sys.stderr)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__ == '__main__':
log_path = "/var/log"
log_name = "." + os.path.splitext(os.path.basename(__file__))[0]
log = initLoggerWithRotate(logPath="/var/log", logName=log_name, singleLogFile=True)
log.setLevel(logging.INFO)
log.info("if you can see this message, it means success")