Search code examples
pythonloggingpythonanywhere

Tailing log is slow


Hosted on pythonAnywhere, working inside a jupyter notebook, I create a logger

import logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename="/home/jaa/bot/ma_clipboard.log",
    level=logging.INFO)
logger=logging.getLogger(__name__)
logger.info(f"enabled the logger {logger}")

And meanwhile in a bash shell, I tail the logfile: tail -f ma_clipboard.log.

Inside the notebook, I'm working with a python-telegram-bot, which is handling user input from outside.

My issue is that the log-tailing doesn't update very quickly when either I purposefully log something, or even when uncaught Exceptions occur. Sometimes I have to wait a few minutes. This is very annoying for debugging.

I'm not sure what the source of the delay is. The logger module? The tail command? Something else? I don't think it's unique to the python-telegram-bot or that I'm working inside a Jupyter Notebook, as I had similar issues tail'ing a django logfile last year.

How can I reduce this latency in the logfile tailing?


Solution

  • It's PythonAnywhere dev here. It takes some time for our infrastructure to write a log into the file. I'm afraid that you may not be able to do anything about that on your side.

    Edit

    After closer examination, we noticed that the problem is different and it is solvable on your side. tail -f looks for the change of attributes of a file. Your files are accessed over nfs that is caching file attributes for some time that results in delayed reaction of tail -f. The workaround is to watch -n 0.1 tail [FILE] that shows you changes to the file instantaneously. You may adjust 0.1 seconds interval to save cpu time.