1. <i id='JteLw'><tr id='JteLw'><dt id='JteLw'><q id='JteLw'><span id='JteLw'><b id='JteLw'><form id='JteLw'><ins id='JteLw'></ins><ul id='JteLw'></ul><sub id='JteLw'></sub></form><legend id='JteLw'></legend><bdo id='JteLw'><pre id='JteLw'><center id='JteLw'></center></pre></bdo></b><th id='JteLw'></th></span></q></dt></tr></i><div id='JteLw'><tfoot id='JteLw'></tfoot><dl id='JteLw'><fieldset id='JteLw'></fieldset></dl></div>

        <small id='JteLw'></small><noframes id='JteLw'>

      2. <tfoot id='JteLw'></tfoot>
        • <bdo id='JteLw'></bdo><ul id='JteLw'></ul>
        <legend id='JteLw'><style id='JteLw'><dir id='JteLw'><q id='JteLw'></q></dir></style></legend>

        每次启动应用程序时轮换日志文件(Python)

        时间:2023-07-03

      3. <legend id='dpul5'><style id='dpul5'><dir id='dpul5'><q id='dpul5'></q></dir></style></legend>

      4. <tfoot id='dpul5'></tfoot>
        <i id='dpul5'><tr id='dpul5'><dt id='dpul5'><q id='dpul5'><span id='dpul5'><b id='dpul5'><form id='dpul5'><ins id='dpul5'></ins><ul id='dpul5'></ul><sub id='dpul5'></sub></form><legend id='dpul5'></legend><bdo id='dpul5'><pre id='dpul5'><center id='dpul5'></center></pre></bdo></b><th id='dpul5'></th></span></q></dt></tr></i><div id='dpul5'><tfoot id='dpul5'></tfoot><dl id='dpul5'><fieldset id='dpul5'></fieldset></dl></div>

            <bdo id='dpul5'></bdo><ul id='dpul5'></ul>

          • <small id='dpul5'></small><noframes id='dpul5'>

                    <tbody id='dpul5'></tbody>
                • 本文介绍了每次启动应用程序时轮换日志文件(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Python 中使用日志记录模块,我希望它在每次启动我的应用程序时创建一个新的日志文件.应该轮换较旧的日志文件(例如:logfile.txt -> logfile1.txt 等).

                  I'm using the logging module in Python and I would like it to create a new logfile each time my application is started. The older logfiles shoud be rotated (eg: logfile.txt -> logfile1.txt, etc).

                  我已经找到了:

                  http://docs.python.org/library/logging.html

                  BaseRotatingHandler 是基类对于将日志文件轮换到的处理程序某一点.这并不意味着直接实例化.相反,使用RotatingFileHandler 或TimedRotatingFileHandler.

                  BaseRotatingHandler is the base class for handlers that rotate log files at a certain point. It is not meant to be instantiated directly. Instead, use RotatingFileHandler or TimedRotatingFileHandler.

                  RotatingFileHandler 以预定大小进行翻转,而 TimedRotatingFileHandler 根据时间和间隔的乘积进行翻转.两者都不是我想要的,我希望在我的应用程序启动时立即进行轮换.

                  The RotatingFileHandler does a rollover at a predetermined size and the TimedRotatingFileHandler does a rollover based on the product of when and interval. Both are not what I want, I want the rotation to happen immediately when my application starts.

                  推荐答案

                  我可能用RotatingFileHandler不用maxBytes就够了,然后调用doRollover() 在应用程序启动时.

                  I might be enough to use RotatingFileHandler without maxBytes, then call doRollover() on application start.

                  是的,似乎工作正常.下面的代码将在每个应用程序运行时创建一个新的日志文件,并为日志开始和关闭时间添加时间戳.运行它将打印可用日志文件的列表.您可以检查它们以检查正确的行为.改编自 Python 文档示例:

                  Yup, seems to work fine. The code below will create a new log file on each application run, with added timestamps for log start and close times. Running it will print the list of available log files. You can inspect them to check correct behavior. Adapted from the Python docs example:

                  import os
                  import glob
                  import logging
                  import logging.handlers
                  import time
                  
                  LOG_FILENAME = 'logging_rotatingfile_example.out'
                  
                  # Set up a specific logger with our desired output level
                  my_logger = logging.getLogger('MyLogger')
                  my_logger.setLevel(logging.DEBUG)
                  
                  # Check if log exists and should therefore be rolled
                  needRoll = os.path.isfile(LOG_FILENAME)
                  
                  # Add the log message handler to the logger
                  handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)
                  
                  my_logger.addHandler(handler)
                  
                  # This is a stale log, so roll it
                  if needRoll:    
                      # Add timestamp
                      my_logger.debug('
                  ---------
                  Log closed on %s.
                  ---------
                  ' % time.asctime())
                  
                      # Roll over on application start
                      my_logger.handlers[0].doRollover()
                  
                  # Add timestamp
                  my_logger.debug('
                  ---------
                  Log started on %s.
                  ---------
                  ' % time.asctime())
                  
                  # Log some messages
                  for i in xrange(20):
                      my_logger.debug('i = %d' % i)
                  
                  # See what files are created
                  logfiles = glob.glob('%s*' % LOG_FILENAME)
                  
                  print '
                  '.join(logfiles)
                  

                  这篇关于每次启动应用程序时轮换日志文件(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 PIL 调整大小并将旋转 EXIF 信息应用于文件? 下一篇:如何设置 pygame.transform.rotate() 的轴心点(旋转中心)?

                  相关文章

                  • <bdo id='Df6xL'></bdo><ul id='Df6xL'></ul>

                      <legend id='Df6xL'><style id='Df6xL'><dir id='Df6xL'><q id='Df6xL'></q></dir></style></legend>

                      <small id='Df6xL'></small><noframes id='Df6xL'>

                    1. <i id='Df6xL'><tr id='Df6xL'><dt id='Df6xL'><q id='Df6xL'><span id='Df6xL'><b id='Df6xL'><form id='Df6xL'><ins id='Df6xL'></ins><ul id='Df6xL'></ul><sub id='Df6xL'></sub></form><legend id='Df6xL'></legend><bdo id='Df6xL'><pre id='Df6xL'><center id='Df6xL'></center></pre></bdo></b><th id='Df6xL'></th></span></q></dt></tr></i><div id='Df6xL'><tfoot id='Df6xL'></tfoot><dl id='Df6xL'><fieldset id='Df6xL'></fieldset></dl></div>
                      <tfoot id='Df6xL'></tfoot>