jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[cleanup] Deprecate tools.RotatingFileHandler

- use standard implementation of RotatingFileHandler
- use a namer method to create a filename from given baseFilename
and counter
- give up logfilecount of -1 for infinite amount of backups; set it
to 999 and deprecate their usage
- deprecate old usage in bot.py and _logging.py

Change-Id: I749a4cfc0c948f156650834a01f2e756af9b746e
---
M pywikibot/bot.py
M pywikibot/tools/_logging.py
2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index b261d09..1a9f9cb 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -130,6 +130,7 @@
UnhandledAnswer,
)
from pywikibot.exceptions import (
+ ArgumentDeprecationWarning,
EditConflictError,
Error,
LockedPageError,
@@ -167,9 +168,10 @@
deprecated,
deprecated_args,
issue_deprecation_warning,
+ redirect_func,
suppress_warnings,
)
-from pywikibot.tools._logging import LoggingFormatter, RotatingFileHandler
+from pywikibot.tools._logging import LoggingFormatter
from pywikibot.tools.formatter import color_format


@@ -291,6 +293,13 @@
_handlers_initialized = False


+def handler_namer(name: str) -> str:
+ """Modify the filename of a log file when rotating."""
+ path, qualifier = name.rsplit('.', 1)
+ root, ext = os.path.splitext(path)
+ return '{}.{}{}'.format(root, qualifier, ext)
+
+
def init_handlers(strm=None):
"""Initialize logging system for terminal-based bots.

@@ -382,10 +391,23 @@
logfile = config.datafilepath('logs', '{}-{}bot.log'
.format(module_name, pid))

- file_handler = RotatingFileHandler(filename=logfile,
- maxBytes=1024 * config.logfilesize,
- backupCount=config.logfilecount,
- encoding='utf-8')
+ # give up infinite rotating file handler with logfilecount of -1;
+ # set it to 999 and use the standard implementation
+ max_count = config.logfilecount
+ if max_count == -1:
+ max_count = 999
+ issue_deprecation_warning('config.logfilecount with value -1',
+ 'any positive number',
+ warning_class=ArgumentDeprecationWarning,
+ since='6.5.0')
+
+ file_handler = logging.handlers.RotatingFileHandler(
+ filename=logfile,
+ maxBytes=config.logfilesize << 10,
+ backupCount=max_count,
+ encoding='utf-8'
+ )
+ file_handler.namer = handler_namer

file_handler.setLevel(DEBUG)
form = LoggingFormatter(
@@ -2262,6 +2284,10 @@

set_interface(config.userinterface)

+# Deprecate RotatingFileHandler
+RotatingFileHandler = redirect_func(logging.handlers.RotatingFileHandler,
+ since='6.5.0')
+
# NOTE: (T286348)
# Do not use ModuleDeprecationWrapper with this module.
# pywikibot.bot.ui would be wrapped through the ModuleDeprecationWrapper
diff --git a/pywikibot/tools/_logging.py b/pywikibot/tools/_logging.py
index c3b79df..830b8d9 100644
--- a/pywikibot/tools/_logging.py
+++ b/pywikibot/tools/_logging.py
@@ -7,11 +7,16 @@
import logging
import os

+from pywikibot.tools import ModuleDeprecationWrapper

-# Logging module configuration
-class RotatingFileHandler(logging.handlers.RotatingFileHandler):

- """Modified RotatingFileHandler supporting unlimited amount of backups."""
+class _RotatingFileHandler(logging.handlers.RotatingFileHandler):
+
+ """DEPRECATED Modified RotatingFileHandler.
+
+ Use namer instead. See:
+ https://docs.python.org/3/howto/logging-cookbook.html#cookbook-rotator-namer
+ """

def doRollover(self):
"""Modified naming system for logging files.
@@ -93,3 +98,11 @@
record.args = (msg,)

return super().format(record).rstrip()
+
+
+wrapper = ModuleDeprecationWrapper(__name__)
+wrapper.add_deprecated_attr(
+ 'RotatingFileHandler', _RotatingFileHandler,
+ replacement_name=('logging.handlers.RotatingFileHandler '
+ 'with your own namer'),
+ since='6.5.0')

To view, visit change 703930. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I749a4cfc0c948f156650834a01f2e756af9b746e
Gerrit-Change-Number: 703930
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged