python常用代码: logging 和 无视locale强制东8区
2020-12-13 04:46
标签:form ini strftime add log one spl hang ctime python常用代码: logging 和 无视locale强制东8区 标签:form ini strftime add log one spl hang ctime 原文地址:https://www.cnblogs.com/yeyong/p/11122167.htmlimport logging
import pytz
from dateutil.tz import *
import datetime
import os
# pingcmd = ‘ping -c 5 www.baidu.com‘
cur_path = os.path.split(os.path.realpath(__file__))[0]
cur_file = os.path.split(os.path.realpath(__file__))[0]
logger = logging.getLogger("logger")
def initlog():
tzname = datetime.datetime.now(tzlocal()).tzname()
handler2 = logging.FileHandler(filename=os.path.join(cur_path,cur_file.replace(‘.py‘,‘.log‘)))
logger.setLevel(logging.DEBUG)
handler2.setLevel(logging.DEBUG)
if tzname == ‘UTC‘:
logging.Formatter.converter = beijing
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")
handler2.setFormatter(formatter)
logger.addHandler(handler2)
def beijing(sec,what):
beijing_time = datetime.datetime.now() + datetime.timedelta(hours=8)
return beijing_time.timetuple()
tz = pytz.timezone("Asia/Shanghai")def getts():
return datetime.datetime.now(tz).strftime("%Y.%m.%d %H:%M:%S")
文章标题:python常用代码: logging 和 无视locale强制东8区
文章链接:http://soscw.com/essay/29947.html