Django Logging with FileHandler not Working

2024/10/18 12:42:16

I am using the logging setup below with a django project (also using sentry/raven). The sentry/raven bit is working fine, but the file logging isn't. An empty logfile is created, but whenever I use logging.info('foo') nothing comes up in the log file (i.e. it remains empty). Any suggestions?

LOGGING = {'version': 1,'disable_existing_loggers': True,'root': {'level': 'WARNING','handlers': ['sentry'],},'formatters': {'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'},},'handlers': {'sentry': {'level': 'ERROR','class': 'raven.contrib.django.handlers.SentryHandler',},'file': {'level': 'INFO','class': 'logging.FileHandler','filename': '/var/log/django/breeding.log',},'console': {'level': 'DEBUG','class': 'logging.StreamHandler','formatter': 'verbose'}},'loggers': {'django.db.backends': {'level': 'ERROR','handlers': ['console'],'propagate': False,},'raven': {'level': 'DEBUG','handlers': ['console'],'propagate': False,},'sentry.errors': {'level': 'DEBUG','handlers': ['console'],'propagate': False,},},
}
Answer

I ran into this same issue. It turned out to be a permissions problem. When I ran the development server for the first time after configuring logging, it created the file /var/log/django/request.log owned by my local user (stretch) with mode 644.

When I started the "production" server (nginx/uwsgi), the service would run as the www-data user and was unable to open /var/log/django/request.log for writing. Simply deleting the log file and restarting uwsgi was enough to get it going, but I'll have to come up with a more elegant long-term fix.

https://en.xdnf.cn/q/72965.html

Related Q&A

How to directly access a resource in a Py2app (or Py2exe) program?

This is mostly for Py2app, but I plan to also port to Windows so Py2exe is also applicable.For Mac: How can I access the Resources folder of my app bundle from Python code? The ideal way for me would …

Workflow for Python with Docker + IDE for non-web applications

I am currently trying to insert Docker in my Python development workflow of non-web applications.What are the current best practices in Python development using Docker and an IDE? I need the possibili…

Django Deserialization Error Problem installing Fixture

Traceback (most recent call last):File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializeryield from PythonDeserialize…

Python HTTP Exception Handling

Im running a program that downloads files from a web sit. Ive introduced one exception handling urllib.error.HTTPError, but now Im getting from time to time additional errors that Im not sure how to ca…

Weird lambda behaviour in loops [duplicate]

This question already has answers here:What do lambda function closures capture?(8 answers)Closed 10 years ago.I stumbled upon a behaviour in python that I have a hard time understanding. This is the…

Why does inspect return different line for class inheriting from superclass?

While trying to figure out if a function is called with the @decorator syntax, we realized that inspect has a different behaviour when looking at a decorated class that inherits from a superclass.The f…

Sorting a list of tuples with multiple conditions

I am currently trying to sort the following list:list_ = [(1, 0101), (1, 1010), (1, 101), (2, 01), (2, 010), (2, 10)]These are the steps I want to take in order to sort it:Sort the list by the value of…

Tensorboard error: Tensor object has no attribute value

My goal: Add arbitrary text to tensorboard.My code:text = "muh teeeext" summary = tf.summary.text("Muh taaaag", tf.convert_to_tensor(text)) writer.add_summary(summary)My error:File …

How to embed Google Speech to Text API in Python program? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

shell script remote execution using python

Is there a way that I can use Python on Windows to execute shell scripts which are located on a remote Unix machine?P.S: Sorry about the late edit. I do know of Paramiko, but I wanted to know if there…