Is there anyway to disable grequests's logging to console? My applications returns error in the requests part:
Timeout: (<requests.packages.urllib3.connectionpool.HTTPConnectionPool object at 0x10daa1a50>, 'Connection to 116.231.213.50 timed out. (connect timeout=5)')
<Greenlet at 0x10d92bf50: <bound method AsyncRequest.send of <grequests.AsyncRequest object at 0x10da97990>>(stream=False)> failed with Timeout
I found this to disable requests's logging but no luck with the grequests.
If you used the approach in your link to disable requests logging (e.g. logging.getLogger("requests").setLevel(logging.CRITICAL)) then it should work for grequests too. Have you tried it? If you have and it still doesn't behave as you would like, configure logging to show logger names (e.g. via basicConfig using %(name)s in the format string) and you should see exactly which loggers are producing the messages, and you can then silence them using the same approach as for the requests logger.
Related
When using print method I am receiving log output I haven't seen before.
I guess it's coming from Twisted module which seems to be a part of Scrapyd. I am not using this kind of logging elsewhere and I haven't started it explicitely.
Does anyone know how to supress/deactivate this kind of log messages?
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
As the question says, how can I do it? following is the code-
import logging
from sleekxmpp import ClientXMPP
logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')
def on_session(event):
xmpp.get_roster()
xmpp = ClientXMPP(jid, password)
xmpp.add_event_handler('session_start', on_session)
if xmpp.connect():
print xmpp.authenticated # Always prints `False`
xmpp.process(block=True)
I can see that printing in the logs, but I rather want to check it in the code as well. How can I do it?
In XMPP, the authentication is handled by SASL which usually starts once the client is connected.
In your case, mere connection success doesn't imply that authentication was done. You need to put event handler for SASL authentication success and failure cases. Going through the sleekxmpp events, it makes me believe that it can be done by monitoring auth_success and failed_auth events. Possible code will be :
xmpp.add_event_handler("failed_auth", on_failed_auth)
xmpp.add_event_handler("auth_success", on_auth_success)
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.
I have setup the logging module for my new python script. I have two handlers, one sending stuff to a file, and one for email alerts. The SMTPHandler is setup to mail anything at the ERROR level or above.
Everything works great, unless the SMTP connection fails. If the SMTP server does not respond or authentication fails (it requires SMTP auth), then the whole script dies.
I am fairly new to python, so I am trying to figure out how to capture the exception that the SMTPHandler is raising so that any problems sending the log message via email won't bring down my entire script. Since I am also writing errors to a log file, if the SMTP alert fails, I just want to keep going, not halt anything.
If I need a "try:" statement, would it go around the logging.handlers.SMTPHandler setup, or around the individual calls to my_logger.error()?
Exceptions which occur during logging should not stop your script, though they may cause a traceback to be printed to sys.stderr. In order to prevent this printout, do the following:
logging.raiseExceptions = 0
This is not the default (because in development you typically want to know about failures) but in production, raiseExceptions should not be set.
You should find that the SMTPHandler will attempt a re-connection the next time an ERROR (or higher) is logged.
logging does pass through SystemExit and KeyboardInterrupt exceptions, but all others should be handled so that they do not cause termination of an application which uses logging. If you find that this is not the case, please post specific details of the exceptions which are getting through and causing your script to terminate, and about your version of Python/operating system. Bear in mind that the script may appear to hang if there is a network timeout which is causing the handler to block (e.g. if a DNS lookup takes a long time, or if the SMTP connection takes a long time).
You probably need to do both. To figure this out, I suggest to install a local mail server and use that. This way, you can shut it down while your script runs and note down the error message.
To keep the code maintainable, you should extends SMTPHandler in such a way that you can handle the exceptions in a single place (instead of wrapping every logger call with try-except).