http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10778
Revision: 10778 Author: xqt Date: 2012-12-10 14:37:29 +0000 (Mon, 10 Dec 2012) Log Message: ----------- New doRollower() method for RotatingFileHandler which keeps the file extension of the log files and inserts the counter number before the extension instead of behind it.
New config variables are introduced to control the FileHandler:
logfilesize allows to specify the size of a single logfiles given in kilobytes
logfilecount specifies the number of rotation logfiles. If 0 is given no logfile will be archived but the current logfile will reused and the previous data are lost. (New!) If logfilecount is -1, there are no rotating logfiles but the files where renamed if the logfile is full. The newest file gets the highest number until some logfiles where deleted.
Modified Paths: -------------- branches/rewrite/pywikibot/bot.py branches/rewrite/pywikibot/config2.py
Modified: branches/rewrite/pywikibot/bot.py =================================================================== --- branches/rewrite/pywikibot/bot.py 2012-12-10 14:06:38 UTC (rev 10777) +++ branches/rewrite/pywikibot/bot.py 2012-12-10 14:37:29 UTC (rev 10778) @@ -3,7 +3,7 @@ User-interface related functions for building bots """ # -# (C) Pywikipedia bot team, 2008-2011 +# (C) Pywikipedia bot team, 2008-2012 # # Distributed under the terms of the MIT license. # @@ -35,16 +35,65 @@ # User interface initialization # search for user interface module in the 'userinterfaces' subdirectory uiModule = __import__("pywikibot.userinterfaces.%s_interface" - % config.userinterface, - fromlist=['UI'] ) + % config.userinterface, + fromlist=['UI'] ) ui = uiModule.UI()
# Logging module configuration
class RotatingFileHandler(logging.handlers.RotatingFileHandler): - """Strip trailing newlines before outputting text to file""" + + def doRollover(self): + """ + Overwrites the default Rollover renaming by inserting the count number + between file name root and extension. If backupCount is >= 1, the system + will successively create new files with the same pathname as the base + file, but with inserting ".1", ".2" etc. in front of the filename + suffix. For example, with a backupCount of 5 and a base file name of + "app.log", you would get "app.log", "app.1.log", "app.2.log", ... + through to "app.5.log". The file being written to is always "app.log" - + when it gets filled up, it is closed and renamed to "app.1.log", and if + files "app.2.log", "app.2.log" etc. exist, then they are renamed to + "app.2.log", "app.3.log" etc. respectively. + If backupCount is >= 1 do not rotate but create new numbered filenames. + The newest file has the highest number except some older numbered files + where deleted and the bot was restarted. In this case the ordering + starts from the lowest availlable (unused) number. + + """ + if self.stream: + self.stream.close() + self.stream = None + root, ext = os.path.splitext(self.baseFilename) + if self.backupCount > 0: + for i in range(self.backupCount - 1, 0, -1): + sfn = "%s.%d%s" % (root, i, ext) + dfn = "%s.%d%s" % (root, i + 1, ext) + if os.path.exists(sfn): + #print "%s -> %s" % (sfn, dfn) + if os.path.exists(dfn): + os.remove(dfn) + os.rename(sfn, dfn) + dfn = "%s.1%s" % (root, ext) + if os.path.exists(dfn): + os.remove(dfn) + os.rename(self.baseFilename, dfn) + #print "%s -> %s" % (self.baseFilename, dfn) + elif self.backupCount == -1: + if not hasattr(self, lastNo): + self._lastNo = 1 + while True: + fn = "%s.%d%s" % (root, self._lastNo, ext) + self._lastNo += 1 + if not os.path.exists(fn): + break + os.rename(self.baseFilename, fn) + self.mode = 'w' + self.stream = self._open() + def format(self, record): + """Strip trailing newlines before outputting text to file""" text = logging.handlers.RotatingFileHandler.format(self, record) return text.rstrip("\r\n")
@@ -152,8 +201,9 @@ logfile = config.datafilepath("logs", config.logfilename) else: logfile = config.datafilepath("logs", "%s-bot.log" % moduleName) - file_handler = RotatingFileHandler( - filename=logfile, maxBytes=2 << 20, backupCount=5) + file_handler = RotatingFileHandler(filename=logfile, + maxBytes=1024 * config.logfilesize, + backupCount=config.logfilecount)
file_handler.setLevel(DEBUG) form = LoggingFormatter( @@ -167,7 +217,7 @@ # or for all components if nothing was specified for component in config.debug_log: if component: - debuglogger = logging.getLogger("pywiki."+component) + debuglogger = logging.getLogger("pywiki." + component) else: debuglogger = logging.getLogger("pywiki") debuglogger.setLevel(DEBUG) @@ -215,7 +265,7 @@ # done filching
def logoutput(text, decoder=None, newline=True, _level=INFO, _logger="", - **kwargs): + **kwargs): """Format output and send to the logging module.
Backend function used by all the user-output convenience functions.
Modified: branches/rewrite/pywikibot/config2.py =================================================================== --- branches/rewrite/pywikibot/config2.py 2012-12-10 14:06:38 UTC (rev 10777) +++ branches/rewrite/pywikibot/config2.py 2012-12-10 14:37:29 UTC (rev 10778) @@ -256,6 +256,17 @@ log = ['interwiki'] # filename defaults to modulename-bot.log logfilename = None +# maximal size of a logfile in kilobytes. If the size reached that limit the +# logfile will be renamed (if logfilecount is not 0) and the old file is filled +# again. logfilesize must be an integer value +logfilesize = 1024 +# Number of rotating logfiles are created. The older files get the higher +# number. If logfilecount is 0, no logfile will be archived but the current +# logfile will be overwritten if the file size reached the logfilesize above. +# If logfilecount is -1 there are no rotating logfiles but the files where +# renamed if the logfile is full. The newest file gets the highest number until +# some logfiles where deleted. +logfilecount = 5 # set to 1 (or higher) to generate "informative" messages to terminal verbose_output = 0 # if True, include a lot of debugging info in logfile
pywikipedia-svn@lists.wikimedia.org