封装linux几个操作
作者:互联网
#!/usr/bin/python
#coding=gbk
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
import subprocess
import logging
import os
import logging.handlers
import re
def logger(appName, rootstdout=True, handlerList=None):
log_fmt = "%(asctime)s --%(name)s-- [%(levelname)s]:\n%(message)s"
c_fmt = "%(asctime)s --%(name)s-- %(filename)s.%(funcName)s():line %(lineno)d [%(levelname)s]:\n%(message)s"
date_format = "%Y-%m-%d %H:%M:%S %a"
logging.basicConfig(level=logging.DEBUG,
format=c_fmt,
datefmt=date_format,
)
levels = []
if isinstance(handlerList, list):
if handlerList.__contains__("I") or handlerList.__contains__("Info"):
levels.append("Info")
if handlerList.__contains__("D") or handlerList.__contains__("Debug"):
levels.append("Debug")
if handlerList.__contains__("E") or handlerList.__contains__("Error"):
levels.append("Error")
if handlerList.__contains__("W") or handlerList.__contains__("Warning"):
levels.append("Warning")
if levels:
stamp = "dailylog.log"
logsdir = os.path.join(os.getcwd(), "logs")
if os.path.exists(logsdir):
for p in levels:
if os.path.exists(os.path.join(logsdir, p)):
pass
else:
os.mkdir(os.path.join(logsdir, p))
else:
os.mkdir(logsdir)
for p in levels:
os.mkdir(os.path.join(logsdir, p))
f_dict = {}
for i in levels:
filename = os.path.join(logsdir, i, stamp)
f_dict[i] = filename
logger = logging.getLogger(appName)
for k, v in f_dict.items():
handler = logging.handlers.TimedRotatingFileHandler(filename=v, when='MIDNIGHT', interval=1,
标签:__,contains,封装,linux,handlerList,levels,path,操作,os 来源: https://www.cnblogs.com/zhaobobo10/p/10969857.html