0

I am using loguru, and when I start a new test or rerun a test, I remove the old file-handler and add a new one to separate logs per-run and reflect the new start time. However, the logger's elapsed time in the stream-handler and the file-handler both reflect the original elapsed reference.

Elapsed after several tests:

enter image description here

Is there a way to reset this reference as-needed?

loguru is so easy at first, but it's such a pain to workaround things that seem so simple, when you start to dive in.

Thanks in advance.


I tried removing and replacing both my stream-handler and file-handler. I hoped this may reset the elapsed reference, but it didn't help.

I searched google and found nothing besides how to format the elapsed time.

I tried removing the logger and re-importing it, but no help

Attempted deleting the logger: enter image description here

import sys
import time

from loguru import logger

logger.remove(0)
logger.add(sys.stderr, format='<light-black>{elapsed}</> | <cyan>{level:8}</> <light-cyan>|</> <level>{message}</>')
logger.info('test')
time.sleep(1)
logger.info('test')

del logger
from loguru import logger

logger.remove(0)
logger.add(sys.stderr, format='<light-black>{elapsed}</> | <cyan>{level:8}</> <light-cyan>|</> <level>{message}</>')
logger.info('test')
time.sleep(1)
logger.info('test')
num3ri
  • 822
  • 16
  • 20
ryguycmt
  • 1
  • 2

1 Answers1

0

I looked in the loguru github issues and found a solution. I'm surprised it didn't show up in my Google searches.

How to reset the elapsed time?

The best solution would be to add your own argument to the record.

import sys
import time
from loguru import logger
from datetime import timedelta

class LauncherTime:
    def __init__(self):
        self._start = time.time()

    def reset(self):
        self._start = time.time()

    @property
    def elapsed(self):
        return timedelta(seconds=time.time() - self._start)

launcher_time = LauncherTime()

logger.configure(
    extra={"launcher_time": launcher_time},
    handlers=[{"sink": sys.stderr, "format": "[{extra[launcher_time].elapsed}] {message}"}],
)

def f():
    launcher_time.reset()
    logger.info("Entering function")
    time.sleep(1)
    logger.info("Leaving function")

for _ in range(100):
    f()
ryguycmt
  • 1
  • 2
  • Since this doesn't answer the question whatsoever, it would be recommended to close the question. – Owenn Jul 01 '23 at 13:57
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '23 at 13:57
  • I hope this helps. I read the help center for more information. I'm providing context and quoting the provided solution in case the link dies. Is this better? – ryguycmt Jul 07 '23 at 19:03