Forums

Cannot log to custom log file

I am trying to write logs to a custom log file, like so:

logging.basicConfig(filename='test.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('foo')

but, despite the test.log file existing (in the same folder as my py script) it simply is not logging anything.

What gives?

How are you running the script? Is it possible that it crashes before the logging bit?

The script runs as a web app. Not sure how else to explain it sorry.

The error logs are flooded with tons of messages that simply don't make any sense to me (im new to python, and used to error logs from languages like PHP and JS)

Oh, and I tried logging when the app is running fine, so its not a case of the app crashing before logging anything

What is the most recent error that you see on the bottom of your error log?

Sorry, ignore the mention of the error logs, that was for something else.

The issue here is that logging doesn't work, and doesn't cause an error with the code i've pasted above.

I think the problem is with your working directory; this line:

logging.basicConfig(filename='test.log', encoding='utf-8', level=logging.DEBUG)

...does not mean "log to the file 'test.log' in the same directory as the file that contains this line of code" -- it means "log to the file 'test.log' in this process's current working directory". Those can be the same, but often won't be. By default the working directory for your code will be your home directory, /home/pablopython, and if you look there you'll probably see the log file that you're looking for.

If you want to log to a file in a particular directory, you can use a full filename like /home/pablopython/mysite/test.log in your call to basicConfig, or you can build a similar absolute path using Python's magic __file__ variable -- see this help page for more.

So, I can indeed see a "test.log" file in "/home/pablopython" but, it doesn't seem to be updating whenever I access the web app itself.

I changed the logging.debug message and hit the web app via my browser and then checked the log file - it had not been updated.

I also tried changing the log filename to make a new log, but that hasnt worked either.

I;ve tried moving the code to the top of the class, but still, no luck

See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Sorry Glenn, I'm not sure what you're on about? Could you expand on your reply?

So a file is being created, but my logging "logging.debug('foo')" is not working, it does not appear in the log file.

have you fixed what giles described? if not then possibly your code is eg: creating a test.log in different directories depending on where it was run from.

I have the same problem, the log file ss created but the log messages are not printing on the log file. I'm just looking for how to print log messages for my app. I haven't been able to find details on that

Found the answer I was looking for here https://www.pythonanywhere.com/forums/topic/28981/

The code above is missing registering the file handler on the app. After the logging.basicConfig call add this code

file_handler = logging.FileHandler('PATH_TO_YOUR_LOG_FILE')
app.logger.addHandler(file_handler)

Glad you figured that out!

Hello, I had the same problem but was solved adding the handler, NOW I'm receiving double logs, one in my custom file xxxxx.log and the same is printed in Error log: hstrauch.pythonanywhere.com.error.log how do I avoid to log in the error.log? I only need logs in my custom file.

That should be possible if you stop logging to stderr.

Thank you for your response, I have been trying to change my logging configuration to stop logging to stderr but I couldn't do it, can you give me an idea of how to stop logging in stderr to avoid printing in Error log: username.pythonanywhere.com.error.log. Thank you.

We need more detail to help you. What did you try?

Sure, what I have its a log configuration in a loggingPA.py file as follow:

logger = logging.getLogger(__name__)

logger.setLevel(logging.CRITICAL)

duty_handler = logging.FileHandler('pywks/zlog_checkduty.log')
duty_handler.setLevel(logging.CRITICAL)

formatter = logging.Formatter('%(asctime)s - %(message)s',datefmt='%d-%b-%y %H:%M:%S')
duty_handler.setFormatter(formatter)

logger.addHandler(duty_handler)

err_handler = logging.StreamHandler(sys.stderr)
err_handler.setLevel(logging.ERROR)
err_handler.setFormatter(formatter)

logger.addHandler(err_handler)

And in my views.py

from .loggingPA import logger
logger.critical(*************)

or

logger.error(*************)

But get printed on error.log file, not in my custom zlog_checkduty.log file. Thank you.

Try using an absolute path the custom log file (starting with /home/hstrauch) in the FileHandler.

Hello, using the absolute path did work, but now I'm receiving duplicate logs, one on my custom file and the same logs on the error.log file, I just want them on my custom file, I tried basic configuration as posible:

duty_handler = logging.FileHandler('absoluth_path/zlog_checkduty.log')
duty_handler.setLevel(logging.INFO)
duty_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s', '%d-%b-%y %H:%M:%S'))
logging.getLogger().addHandler(duty_handler)
*****
logging.info('log')

[edit by admin: formatting]

I'm not sure there's any way you could prevent logging to the website's error log; could you clarify why you need to do that? The log file doesn't use up any of your disk quota, if that's what you're concerned about.