This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kh14821
Recipients kh14821, rhettinger, vinay.sajip
Date 2021-03-02.18:14:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <CAAFLtXpRP+2kGN6WwrO2wM=4Fts3qCFDcnWhpbkQiN21DLYMWw@mail.gmail.com>
In-reply-to <1614611607.82.0.732318865366.issue43344@roundup.psfhosted.org>
Content
Thanks Vinay, I was able to do this with:

def namer(name):
    return name.replace(".log", "") + ".log"

Then when initializing the logger:

handler.namer = namer

My full initializer script:

import os
import logging
from logging.handlers import TimedRotatingFileHandler
from config import constants

def namer(name):
    return name.replace(".log", "") + ".log"

def init(baseFilename):
    logPath = constants.LOGGING_DIR
    envSuffix = '-prod' if constants.ENV == 'prod' else '-dev'
    logFilename = os.path.join(logPath, baseFilename + envSuffix + '.log')
    print(f"Logging to {logFilename}")

    handler = TimedRotatingFileHandler(logFilename,
        when = "midnight",
        backupCount = 30,
        encoding = 'utf8')
    handler.setLevel(logging.DEBUG)
    handler.suffix = "%Y%m%d"
    handler.namer = namer

    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s
[%(module)s:%(lineno)d]')
    handler.setFormatter(formatter)

    logging.basicConfig(
        handlers = [handler],
        format = '%(asctime)s %(levelname)s %(message)s
[%(module)s:%(lineno)d]',
        level = logging.DEBUG,
        datefmt = '%Y-%m-%d %H:%M:%S')

if __name__ == '__main__':
    init('testing')
    logging.error("ohai")
    logging.debug("ohai debug")
    logging.getLogger().handlers[0].doRollover()
    logging.error("ohai next day")
    logging.debug("ohai debug next day")

On Mon, Mar 1, 2021 at 8:13 AM Vinay Sajip <report@bugs.python.org> wrote:

>
> Vinay Sajip <vinay_sajip@yahoo.co.uk> added the comment:
>
> As per the documentation at
>
>
> https://docs.python.org/3/library/logging.handlers.html#logging.handlers.BaseRotatingHandler.namer
>
> You can set the handler's "namer" attribute to a callable that returns a
> computed name for the rotated file - this can be computed as per your
> requirements. The cookbook also has an entry about this:
>
>
> https://docs.python.org/3/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43344>
> _______________________________________
>
History
Date User Action Args
2021-03-02 18:14:11kh14821setrecipients: + kh14821, rhettinger, vinay.sajip
2021-03-02 18:14:11kh14821linkissue43344 messages
2021-03-02 18:14:10kh14821create