Django.server logging configuration not working in Django Daphne - python

I wanted to increase logging level in order to decrease spam in logs with all 2XX responses. According to Django docs and other SO questions I have changed level of django.server logger to WARNING in settings.py. Unfortunately, this change does not work when I run my server using daphne. To verify, I ran my app using manage.py runserver and there the config was working.
I've also tried changing log level (using LOGGING = {} in settings.py) of all loggers in logging.root.manager.loggerDict, also unsuccessfully.
Anyone has some idea on how to either determine which logger logs the messages like presented below in daphne or how to force daphne to respect django.server log assuming it works as I expect?
Sample log message I'm talking about:
127.0.0.1:33724 - - [21/Apr/2021:21:45:13] "GET /api/foo/bar" 200 455

I found an answer myself. In case someone has same problem:
After diving into Daphne source code it looks like as of now it does not use standard logging library, but relies on own class that writes all access log messages to stdout or given file. I do not see a possibility of increasing the log level.
Consult https://github.com/django/daphne/blob/main/daphne/access.py for more details.

set verbosity to 0 and you are done
daphne -v 0 xxx.asgi:application

Related

Google StackDrive Logging Level in containers with uwsgi always at Error Level

I'm deploying via Kuberenetes come containers on Google Cloud, which are django project and uwsgi to run them.
I'm using the stackdrive logging tool to see the logging, the problem is that all the entries are seen as severity ERROR even thought they are not error. It seems that the log of uwsgi is written to stderr or something like that.
In the picture you can see that django uses INFO level, but that is received as ERROR by stackdrive.
this is how i set up uwsgi.
[uwsgi]
master = true
socket = :3031
chdir = .
wsgi-file = docker.wsgi
processes = 4
threads = 2
socket-timeout = 90
harakiri = 90
http = :8000
env = prometheus_multiproc_dir=multi
enable-threads = yes
lazy-apps = yes
pidfile=/tmp/project-master.pid
Kubernetes logs written to stderr are always tagged as ERROR -- this is hard-coded in the Stackdriver logging agent. Similarly, logs written to stdout are always tagged with INFO.
If you can configure your application to write non-error log messages to stdout, please do so. Another possible approach is to write the logs to a file, run the "tail -f" command on that file as a sidecar container in the same pod, and looking for your logs in Stackdriver Logs Viewer under the sidecar container instead. Finally, you might consider writing your logs directly into the Stackdriver Logging API, which gives you full control over the contents of each entry.
This answer helped me find the solution to this. Using the option logger-req=stdio uWSGI logs get the correct level in Stackdriver.
Example of uwsgi.ini:
[uwsgi]
logger-req=stdio

Python Flask writes access log to STDERR

Flask is writing access logs to STDERR stream instead of STDOUT. How to change this configuration so that access logs go to STDOUT and application errors to STDERR?
open-cricket [master] python3 flaskr.py > stdout.log 2> stderr.log &
[1] 11929
open-cricket [master] tail -f stderr.log
* Running on http://127.0.0.1:9001/
127.0.0.1 - - [11/Mar/2015 16:23:25] "GET /?search=Sachin+Tendulkar+stats HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2015 16:23:25] "GET /favicon.ico HTTP/1.1" 404 -
I'll assume you're using the flask development server.
Flask's development server is based on werkzeug, whose WSGIRequestHandler is, in turn, based in the BaseHTTPServer on the standard lib.
As you'll notice, WSGIRequestHandler overrides the logging methods, log_request, log_error and log_message, to use it's own logging.Logger - so you can simply override it as you wish, in the spirit of IJade's answer.
If you go down that route, I think it'd be cleaner to add your own FileHandler instead, and split the stdout and stderr output using a filter
Note, however, that all this is very implementation specific - there's really no proper interface to do what you want.
Although it's tangential to your actual question, I feel I really must ask - why are you worried about what goes into each log on a development server?
If you're bumping into this kind of problem, shouldn't you be running a real web server already?
Here is what you got to do. Import logging and set the level which you need to log.
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

generating errors and access logs in python

We are realizing that we need error logs and access logs for our service processes. These are long running process like services; they respond to calls made to them.
Hence, we need your help to simply achieve the following:
# for developers
from MyLogger import log
log.error 'something bad something wrong'
log.access 'something something'
I am thinking of designing this MyLogger which will simply redirect an error to stderr and access to stdout, so that I can collect errors to a specific file through configuration for both stderr and stdout.
One more point: these services are nothing but web.py instances.
I guess I'm not looking for a controlling log at various levels, like warn, debug, error, info, etc. My aim is more to have an error log and access log similar to the apache web server. So my developers should not be concerned about using warn, debug, etc. as follows:
log.warn msg
log.debug msg
This is not required.
I just want to have an error log and an access log similar to that of a web server or service.
That "battery" is already included for you in the Python standard logging module.
Python ships with the logging module.

Logging in Python?

I have used log4J and log4N on previous not-pythonic projects. I like heirachy of warnings,errors and escalations. The ability to log the error and if it is serious email the support team. Also automatic log file cycling is important as the it will be running on a small LINUX device.
Can I do this with the standard Python logging module or is there a better approach?
Yes, the logging module has log levels DEBUG, INFO, WARNING, ERROR and CRITICAL. You can setup a SMTPHandler to send mail when the logging level is, say, CRITICAL, and you can setup a RotatingFileHandler to limit the number and size of the log files.
The standard Python logging module is explicitly inspired by log4J, so you will almost certainly find it suitable. It has the same hierarchy, and you can define handlers that listen to one or more levels and do something appropriate, whether it's log to a file or to an email address via SMTP. See the Python logging tutorial.

Capture google app engine logging output

How can one view the Google App Engine logs outside the Admin console?
I'm developing, so using dev_appserver.py/the Admin Console and would like to see the logs as the records are emitted.
I'd like to monitor the logging output in a console with standard Unix tools e.g. less/grep/etc, but there doesn't seem to be an option to direct the logging from the dev_appserver.py command, and I can't open a new file in GAE (e.g. a FileHandler), so file handlers won't work, and I think using a socket/udp handler would be a bit of overkill (if it's even possible).
I'm hopeful there are other options to view the log.
Thanks for reading.
The default logger sends logging output to stderr. Use your shell's method of redirecting stderr to a file (in tcsh, (dev_appserver.py > /dev/tty) >& your_logfile.txt, your shell may vary.)
You can also use the logging module in python to change the logger to send directly to a file if you detect it's running locally (os.environ['SERVER_SOFTWARE'].startswith('Dev'))
You can download the logs using the request_logs parameter of appcfg.py
http://code.google.com/appengine/docs/python/tools/uploadinganapp.html#Downloading_Logs
Edit:
This person came up with a way to send logs over XMPP. His solution is for GAE Java, but this could be adapted to python.
http://www.professionalintellectualdevelopment.com/
http://code.google.com/p/gae-xmpp-logger/

Categories

Resources