Python logging.handlers.TimedRotatingFileHandler() Examples
The following are 30
code examples of logging.handlers.TimedRotatingFileHandler().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
logging.handlers
, or try the search function
.
Example #1
Source File: log.py From webinfo-crawler with MIT License | 8 votes |
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)
Example #2
Source File: log.py From mate-i3-applet with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #3
Source File: LogHandler.py From spider with MIT License | 6 votes |
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)
Example #4
Source File: Windows_Log.py From BiliBiliHelper with GNU General Public License v3.0 | 6 votes |
def __init__(self, filename, level='info', when='D', backCount=3, fmt='[%(asctime)s] - %(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) self.count = 0
Example #5
Source File: Logger.py From listen-now with MIT License | 6 votes |
def __init__(self, filename, level='info', when='D', backCount=10, format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'): self.logger = logging.getLogger(filename) format_str = logging.Formatter(format) # 设置日志格式 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') # 往文件里写入 # 指定间隔时间自动生成文件的处理器 # interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种: # S 秒, M 分, H 小时, D 天, W 每星期(interval==0时代表星期一), midnight 每天凌晨 th.setFormatter(format_str) # 设置文件里写入的格式 self.logger.addHandler(sh) # 把对象加到logger里 self.logger.addHandler(th)
Example #6
Source File: __init__.py From python-admin with MIT License | 6 votes |
def init_app(): formatter = logging.Formatter('%(asctime)s - %(module)s %(filename)s %(funcName)s:%(lineno)s - %(name)s -%(message)s') logging.root.setLevel(logging.INFO) console = logging.StreamHandler() console.setLevel(logging.INFO) console.setFormatter(logging.Formatter('%(message)s')) console.addFilter(logging.Filter()) logging.root.addHandler(console) file_handler_info = TimedRotatingFileHandler(filename=log_info['LOG_PATH_INFO'], backupCount=log_info['LOG_FILE_BACKUP_COUNT'], when='D', interval=1, encoding='utf-8') file_handler_info.setFormatter(formatter) file_handler_info.setLevel(logging.INFO) file_handler_info.addFilter(logging.Filter()) logging.root.addHandler(file_handler_info) return True
Example #7
Source File: Start.py From Timeline with GNU General Public License v3.0 | 6 votes |
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
Example #8
Source File: log.py From probert with GNU Affero General Public License v3.0 | 6 votes |
def setup_logger(name=__name__): LOGDIR = "logs" LOGFILE = os.path.join(LOGDIR, "debug.log") if not os.path.isdir(LOGDIR): os.makedirs(LOGDIR) log = TimedRotatingFileHandler(LOGFILE, when='D', interval=1, backupCount=7) log.setLevel('DEBUG') log.setFormatter(logging.Formatter( "%(asctime)s " "%(name)s:%(lineno)d %(message)s", datefmt='%m/%d %H:%M')) log_filter = logging.Filter(name='probert') log.addFilter(log_filter) logger = logging.getLogger('') logger.setLevel('DEBUG') logger.addHandler(log) return logger
Example #9
Source File: logmanage.py From PyOne with Mozilla Public License 2.0 | 6 votes |
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)
Example #10
Source File: cli.py From lxd-image-server with Apache License 2.0 | 6 votes |
def configure_log(log_file, verbose=False): filename = log_file if log_file == 'STDOUT': handler = logging.StreamHandler(sys.stdout) elif log_file == 'STDERR': handler = logging.StreamHandler(sys.stderr) else: handler = TimedRotatingFileHandler( filename, when="d", interval=7, backupCount=4) formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s') handler.setFormatter(formatter) logger.setLevel('DEBUG' if verbose else 'INFO') logger.addHandler(handler)
Example #11
Source File: Util.py From securedrop-workstation with GNU Affero General Public License v3.0 | 6 votes |
def configure_logging(log_file): """ All logging related settings are set up by this function. """ if not os.path.exists(LOG_DIRECTORY): os.makedirs(LOG_DIRECTORY) formatter = logging.Formatter((LOG_FORMAT)) handler = TimedRotatingFileHandler(os.path.join(LOG_DIRECTORY, log_file)) handler.setFormatter(formatter) handler.setLevel(logging.INFO) log = logging.getLogger() log.setLevel(logging.INFO) log.addHandler(handler)
Example #12
Source File: log_utils.py From FATE with Apache License 2.0 | 6 votes |
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
Example #13
Source File: log.py From snmpfwd with BSD 2-Clause "Simplified" License | 6 votes |
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: # File rotation seems to fail, postpone the next run timestamp = time.time() self.rolloverAt = self.computeRollover(timestamp) if not self.__failure: self.__failure = True error('Failed to rotate log/timestamp file ' '%s: %s' % (self.__filename, sys.exc_info()[1]))
Example #14
Source File: log.py From snmpfwd with BSD 2-Clause "Simplified" License | 6 votes |
def __init__(self, *args, **kwargs): handlers.TimedRotatingFileHandler.__init__(self, *args, **kwargs) self.__failure = False try: timestamp = os.stat(self.__filename)[stat.ST_MTIME] except IOError: return # 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.rolloverAt = self.computeRollover(timestamp)
Example #15
Source File: logHandler.py From proxy_pool with MIT License | 6 votes |
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)
Example #16
Source File: freepydius.py From freeradius with MIT License | 6 votes |
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
Example #17
Source File: log.py From snmpsim with BSD 2-Clause "Simplified" License | 6 votes |
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))
Example #18
Source File: log.py From snmpsim with BSD 2-Clause "Simplified" License | 6 votes |
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)
Example #19
Source File: logger.py From WSPIH with MIT License | 6 votes |
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)
Example #20
Source File: util.py From PyRecognizer with MIT License | 6 votes |
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
Example #21
Source File: log.py From vorta with GNU General Public License v3.0 | 6 votes |
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)
Example #22
Source File: check_music.py From snippet with MIT License | 6 votes |
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
Example #23
Source File: graphite_influxdb.py From graphite-influxdb with Apache License 2.0 | 6 votes |
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)
Example #24
Source File: multiprocesslogging.py From pushkin with MIT License | 5 votes |
def create_multiprocess_logger(logger_name, persist_logger_name, log_level, log_format, log_queue, log_file_path, when_to_rotate, keep_log_days, log_suffix=None): """ Creates queue logger and persist logger. Queue logger should be used to log into. It is Thread and Process safe. Persist logger is logger which persist data to disk. LogCollector moves data from queue log into persist log. """ queue_log_formatter = logging.Formatter(log_format) queue_log_handler = QueueHandler(log_queue, persist_logger_name) queue_log_handler.setFormatter(queue_log_formatter) queue_logger = logging.getLogger(logger_name) queue_logger.setLevel(log_level) queue_logger.handlers = [] queue_logger.addHandler(queue_log_handler) queue_logger.propagate = False persist_log_formatter = logging.Formatter('%(message)s') persist_log_handler = TimedRotatingFileHandler(log_file_path, when=when_to_rotate, interval=1, backupCount=keep_log_days) if log_suffix is not None: persist_log_handler.suffix = log_suffix persist_log_handler.setFormatter(queue_log_formatter) persist_logger = logging.getLogger(persist_logger_name) persist_logger.setLevel(log_level) persist_logger.handlers = [] persist_logger.addHandler(persist_log_handler) persist_logger.propagate = False
Example #25
Source File: logger.py From CAIL2019 with MIT License | 5 votes |
def init_logger(log_name, log_dir): """ 日志模块 1. 同时将日志打印到屏幕跟文件中 2. 默认值保留近30天日志文件 """ ensure_dir(log_dir) if log_name not in Logger.manager.loggerDict: logger = logging.getLogger(log_name) logger.setLevel(logging.DEBUG) handler = TimedRotatingFileHandler( filename=os.path.join(log_dir, "%s.log" % log_name), when="D", backupCount=30, ) datefmt = "%Y-%m-%d %H:%M:%S" format_str = "[%(asctime)s]: %(name)s %(filename)s[line:%(lineno)s] %(levelname)s %(message)s" formatter = logging.Formatter(format_str, datefmt) handler.setFormatter(formatter) handler.setLevel(logging.INFO) logger.addHandler(handler) console = logging.StreamHandler() console.setLevel(logging.INFO) console.setFormatter(formatter) logger.addHandler(console) handler = TimedRotatingFileHandler( filename=os.path.join(log_dir, "ERROR.log"), when="D", backupCount=30, ) datefmt = "%Y-%m-%d %H:%M:%S" format_str = "[%(asctime)s]: %(name)s %(filename)s[line:%(lineno)s] %(levelname)s %(message)s" formatter = logging.Formatter(format_str, datefmt) handler.setFormatter(formatter) handler.setLevel(logging.ERROR) logger.addHandler(handler) logger = logging.getLogger(log_name) return logger
Example #26
Source File: test_config.py From hdfs with MIT License | 5 votes |
def test_get_file_handler(self): with temppath() as tpath: config = Config(tpath) handler = config.get_log_handler('cmd') ok_(isinstance(handler, TimedRotatingFileHandler))
Example #27
Source File: takeover.py From rundeck-scripts with Apache License 2.0 | 5 votes |
def config_log(log_filename): logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) handler = TimedRotatingFileHandler(log_filename, when="d", interval=30, backupCount=5) handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) return logger
Example #28
Source File: all_write_handler.py From presto-admin with Apache License 2.0 | 5 votes |
def _open(self): prev_umask = os.umask(000) rotating_file_handler = handlers.TimedRotatingFileHandler._open(self) os.umask(prev_umask) return rotating_file_handler
Example #29
Source File: logger.py From graham_discord_bot with MIT License | 5 votes |
def setup_logger(log_file: str, log_level: int = logging.INFO) -> logging.Logger: root = logging.getLogger() logging.basicConfig(level=log_level) handler = WatchedFileHandler(log_file) formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z") handler.setFormatter(formatter) root.addHandler(handler) root.addHandler(TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=100))
Example #30
Source File: sr_config.py From sarracenia with GNU General Public License v2.0 | 5 votes |
def create_handler(self, log_format, level): if self.lr_interval > 0 and self.lr_backupCount > 0: os.makedirs(os.path.dirname(self.logpath), exist_ok=True) handler = handlers.TimedRotatingFileHandler(self.logpath, when=self.lr_when, interval=self.lr_interval, backupCount=self.lr_backupCount) else: handler = logging.FileHandler(self.logpath) handler.setFormatter(logging.Formatter(log_format)) handler.setLevel(level) if self.chmod_log: os.chmod(self.logpath, self.chmod_log) return handler