Forums

Logging debug messages on scheduled tasks

Hi everybody!

I'm writing some small scripts to be executed as scheduled tasks, and I'm looking for a way to debug the,

Where I can see messages thrown using logging.debug or logging.info in the script? Accessing the view log page I just see messages printed with logging.warning and logging.error.

Thanks!

You'll need to set the logging level on your logger. The default on the root logger is WARNING.

Oh, thanks a lot.

Long-running process (launched from a scheduled task) is not logging info to a log file.

As successful elsewhere, I’ve tried setting the logger as:

logging.basicConfig(filename='/home/username/home/project/logname.log', level=logging.INFO,
                    stream=sys.stderr,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')  # level=logging.DEBUG

I’ve also tried without stream=sys.stderr and with level=logging.WARNING and level=logging.ERROR

I assume a different path may be needed, but not sure what that would be.

Things are printing in my account's error/access/server logs, but not directly from my logger.

Not sure if it will be necessary, but I’ve tried to pip install daiquiri [1] (after launching a console in my virtual environment), but it says access denied

[1] — https://www.pythonanywhere.com/forums/topic/11752/

Are you sure your code is getting to the place where you're logging? You can use prints which will come out in the main scheduled task log file to check that.

If pip is giving a permission denied error, either your virtualenv is not activated or daiquiri always tries to install something outside of the virtualenv. If it's the second one, then that's probably a bug you can raise with the daiquiri devs.

Yes: Prints have been showing in the scheduled task log file. I'll report back if the daiquiri method fails.

Trying the daiquiri method: To verify the virtual environment was activated, I did source bin/activate which showed (myvirtualenv) in the console, and did pip install daiquiri again, but with the same result (also tried with pip2, pip2.7).

Unless the virtual environment was not activated correctly, I'll await any further instruction on how I might fix the communication for Python's native logging with a log file.

Given further analysis, to save time, it would be preferable to use standard Python logging. Let me know if there is any intuition or salient resources on what may be wrong with the standard logging. I'll otherwise continue to sift through the virtualenv/daiquiri morass.

PythonAnywhere is not doing anything to interfere with logging in scheduled tasks. This code does exactly what you'd expect:

import logging
logging.basicConfig(
    filename="/home/username/task.log",
    level=logging.DEBUG
)
logging.info("made it")

Some other code in your scheduled task must be interfering with the logging.

Problem solved. While it wasn’t my code interfering with logging, your response is appreciated; It led me to try initiating the logging settings from the task that launches the long-running process, instead of ‘the long-running process launched by the scheduler’ (I think I could have made that more clear). For some reason the log file couldn't be created from the launched process, despite having access to its directory (perhaps a permissions issue)