本文整理汇总了Python中logging.handlers.TimedRotatingFileHandler方法的典型用法代码示例。如果您正苦于以下问题:Python handlers.TimedRotatingFileHandler方法的具体用法?Python handlers.TimedRotatingFileHandler怎么用?Python handlers.TimedRotatingFileHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging.handlers
的用法示例。
在下文中一共展示了handlers.TimedRotatingFileHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def logger():
logger = logging.getLogger('testlog')
logHandler = TimedRotatingFileHandler(filename="logfile", when="midnight")
logFormatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
logHandler.setFormatter(logFormatter)
if not logger.handlers:
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
streamhandler.setFormatter(formatter)
logger.addHandler(streamhandler)
logger.addHandler(logHandler)
# logger.error(message)
return logger
示例2: InitiateLogger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def InitiateLogger(name="Timeline"):
Constants.TIMELINE_LOGGER = name
Timeline_logger = logging.getLogger(name)
Timeline_stream = logging.StreamHandler()
LogFormat = logging.Formatter("%(asctime)s [%(levelname)s]\t : %(message)s", "%H:%M")
Timeline_stream.setFormatter(LogFormat)
Timeline_logger.addHandler(Timeline_stream)
Timeline_logger.setLevel(logging.DEBUG)
handler = TimedRotatingFileHandler('./logs/TimelineLogs.log', when="d", interval=1)
Timeline_logger.addHandler(handler)
Timeline_logger.debug("Timeline Logger::Initiated")
return Timeline_logger
示例3: __init__
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def __init__(self,filename=os.path.join(config_dir,'logs/PyOne.running.log'),level='debug',when='D',backCount=3,fmt='%(asctime)s - %(levelname)s: %(message)s'):
self.filename=filename
self.level=level
self.logger = logging.getLogger(self.filename)
format_str = logging.Formatter(fmt)#设置日志格式
self.logger.setLevel(self.level_relations.get(self.level))#设置日志级别
self.stream_handler = logging.StreamHandler()#往屏幕上输出
self.stream_handler.setFormatter(format_str) #设置屏幕上显示的格式
self.file_handler = handlers.TimedRotatingFileHandler(filename=filename,when=when,backupCount=backCount,encoding='utf-8')#往文件里写入#指定间隔时间自动生成文件的处理器
#实例化TimedRotatingFileHandler
#interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
# S 秒
# M 分
# H 小时、
# D 天、
# W 每星期(interval==0时代表星期一)
# midnight 每天凌晨
self.file_handler.setFormatter(format_str)#设置文件里写入的格式
self.logger.addHandler(self.stream_handler) #把对象加到logger里
self.logger.addHandler(self.file_handler)
示例4: set_file
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def set_file(filename):
logger = logging.getLogger()
os.getcwd()
handler = TimedRotatingFileHandler(filename, 'D', 1, 7)
fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
formatter = logging.Formatter(fmt=fmt, datefmt='%m/%d/%Y %H:%M:%S')
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
# 屏幕输出
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.INFO)
logger.addHandler(console)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
示例5: get_handler
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def get_handler(class_name, level=None, log_dir=None, log_type=None):
if not log_type:
if not LoggerFactory.LOG_DIR or not class_name:
return logging.StreamHandler()
if not log_dir:
log_file = os.path.join(LoggerFactory.LOG_DIR, "{}.log".format(class_name))
else:
log_file = os.path.join(log_dir, "{}.log".format(class_name))
else:
log_file = os.path.join(log_dir, "fate_flow_{}.log".format(log_type) if level == LoggerFactory.LEVEL else 'error.log')
formatter = logging.Formatter(LoggerFactory.FORMAT)
handler = TimedRotatingFileHandler(log_file,
when='D',
interval=1,
backupCount=14,
delay=True)
if level:
handler.level=level
handler.setFormatter(formatter)
return handler
示例6: _setup_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def _setup_logger(self, level, log_file):
"""Setup log level and log file if set"""
if logger.handlers:
return
level = getattr(logging, level.upper())
logger.setLevel(level)
formatter = logging.Formatter(
'[%(levelname)s] %(asctime)s - %(module)s.%(funcName)s() - %(message)s')
handler = logging.StreamHandler()
logger.addHandler(handler)
handler.setFormatter(formatter)
if not log_file:
return
try:
handler = TimedRotatingFileHandler(log_file)
except IOError:
logger.error("Could not write to %s, falling back to stdout",
log_file)
else:
logger.addHandler(handler)
handler.setFormatter(formatter)
示例7: init_logging
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def init_logging(logger, level, log_file=None):
fmt = "%(asctime)s - %(pathname)s - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
level = getattr(logging, level.upper())
logger.setLevel(level)
if log_file:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(log_file, when="midnight", interval=1, backupCount=30)
else:
handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(formatter)
logger.addHandler(handler)
##############################################################################
# Configuration
示例8: init_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def init_logger(background=False):
logger.setLevel(logging.DEBUG)
logging.getLogger('peewee').setLevel(logging.INFO)
logging.getLogger('apscheduler').setLevel(logging.INFO)
logging.getLogger('PyQt5').setLevel(logging.INFO)
# create logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# create handlers
fh = TimedRotatingFileHandler(os.path.join(LOG_DIR, 'vorta.log'),
when='d',
interval=1,
backupCount=5)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
if background:
pass
else: # log to console, when running in foreground
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
示例9: __init__
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def __init__(self, *args, **kwargs):
handlers.TimedRotatingFileHandler.__init__(self, *args, **kwargs)
self._failure = False
if not os.path.exists(self._filename):
return
timestamp = os.stat(self._filename).st_mtime
# Use a stand-aside file metadata time instead of the last
# modification of the log file itself, as the stock
# implementation does.
# This is to work-around the increasing rotation intervals
# on process restart.
self.rollover_at = self.computeRollover(timestamp)
示例10: doRollover
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def doRollover(self):
try:
handlers.TimedRotatingFileHandler.doRollover(self)
# note log file creation time
if os.path.exists(self._filename):
os.unlink(self._filename)
open(self._filename, 'w').close()
self._failure = False
except IOError as exc:
# File rotation seems to fail, postpone the next run
timestamp = time.time()
self.rollover_at = self.computeRollover(timestamp)
if not self._failure:
self._failure = True
error('Failed to rotate log/timestamp file '
'%s: %s' % (self._filename, exc))
示例11: instantiate
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def instantiate(p):
print("*** instantiate ***")
print(p)
with rlock:
global logger
logger = logging.getLogger("freepydius-logger")
logger.setLevel(logging.INFO)
handler = TimedRotatingFileHandler(_LOG_FILE,
when="midnight",
interval=1)
formatter = logging.Formatter("%(asctime)s %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
log = Log("INSTANCE")
log.log(( ('Response', 'created'), ))
# return 0 for success or -1 for failure
return 0
示例12: setup_logging
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def setup_logging():
logger = logging.getLogger("")
logger.setLevel(logging.WARNING)
file_handler = handlers.TimedRotatingFileHandler(
os.path.expanduser("~/.mate-i3-applet.log"),
when="D",
backupCount=1,
delay=True,
)
file_handler.setFormatter(
logging.Formatter(
'[%(levelname)s] %(asctime)s: %(message)s',
"%Y-%m-%d %H:%M:%S",
)
)
logger.addHandler(file_handler)
sys.excepthook = exception_handler
示例13: __setFileHandler__
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def __setFileHandler__(self, level=None):
"""
set file handler
:param level:
:return:
"""
file_name = os.path.join(LOG_PATH, '{name}.log'.format(name=self.name))
# 设置日志回滚, 保存在log目录, 一天保存一个文件, 保留15天
file_handler = TimedRotatingFileHandler(filename=file_name, when='D', interval=1, backupCount=15)
file_handler.suffix = '%Y%m%d.log'
if not level:
file_handler.setLevel(self.level)
else:
file_handler.setLevel(level)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
file_handler.setFormatter(formatter)
self.file_handler = file_handler
self.addHandler(file_handler)
示例14: __init__
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def __init__(self, filename, level='info', when='D', backCount=3,
fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
self.logger = logging.getLogger(filename)
format_str = logging.Formatter(fmt) # 设置日志格式
self.logger.setLevel(self.level_relations.get(level)) # 设置日志级别
sh = logging.StreamHandler() # 往屏幕上输出
sh.setFormatter(format_str) # 设置屏幕上显示的格式
th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount,
encoding='utf-8') # 往文件里写入#指定间隔时间自动生成文件的处理器
# 实例化TimedRotatingFileHandler
# interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
# S 秒
# M 分
# H 小时、
# D 天、
# W 每星期(interval==0时代表星期一)
# midnight 每天凌晨
th.setFormatter(format_str) # 设置文件里写入的格式
self.logger.addHandler(sh) # 把对象加到logger里
self.logger.addHandler(th)
示例15: load_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import TimedRotatingFileHandler [as 别名]
def load_logger(level, path, name):
"""
:param level:
:param path:
:param name:
"""
logger = logging.getLogger() # set up root logger
if not os.path.exists(path):
logger.warning("Folder {} not found, creating a new one ...".format(path))
os.makedirs(path)
filename = os.path.join(path, name)
handler = TimedRotatingFileHandler(filename, when='H')
handler.suffix = "%Y-%m-%d.log"
handler.extMatch = r"^\d{4}-\d{2}-\d{2}\.log$"
level = levels[level]
handler.setLevel(level) # set level for handler
formatter = '%(asctime)s - %(name)s - %(levelname)s | [%(filename)s:%(lineno)d] | %(message)s'
handler.setFormatter(logging.Formatter(formatter))
logger.addHandler(handler)
logger.setLevel(level)
logger.warning("Logger initialized, dumping log in {}".format(filename))
return logger