MySQLdb is really nice library for handling SQL connections. I found only one problem. It writes all warning messages directly to stdout. Is there any possibility to redirect it to standard python logger instead?
Replace warnings.showwarning() with a function that works as desired.
Related
I'm making a project and using a library from the requirements of the project. The library implements logging and automatically logs to a file, when I call its functions.
Now, I'm implementing logging by myself, and only want my own messages to be logged to the file and not the library messages.
One solution I thought of would be switching the logging file each time I call a function from the library and then removing that file, but that seems overly complicated and clutterly. What can I do in order to not log the messages from the library?
P.S.:
I'm using the logging library and I initalize it as:
logging.basicConfig(level = logging.INFO,filename = loggingFile,format = "%(message)s")
, which means, all messages from myself and from the library will get logged in loggingFile
Libraries should not directly output anything to logs - that should be done only by handlers configured by an application. A library that logs output is an anti-pattern - if a library definitely does that, I'd log a bug against that library's issue tracker.
On the other hand, it might be that the library is only outputting stuff because you have configured output via your basicConfig() call. If you need more than basic configuration, don't usebasicConfig() - use the other APIs provided.
My project uses these python libraries:
requests==2.18.4
requests-toolbelt==0.8.0
and calls this at the main.py
requests_toolbelt.adapters.appengine.monkeypatch()
App engine is logging Error message inside google cloud logs when using the requests library:
AppEnginePlatformWarning: urllib3 is using URLFetch on Google App
Engine sandbox instead of sockets. To use sockets directly instead of
URLFetch see
https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html.
AppEnginePlatformWarning: URLFetch does not support granular timeout
settings, reverting to total or default URLFetch timeout.
but this is actually just Warning and my code works as expected. But this log is problematic for me because I cannot know if there is really error or not in the logs when I see the statistics. That's why I have to log it as warning somehow.
Here is one stackoverflow answer. This answer states that if this warning is shown on GAE standard environment then the code will work without problem. So it's really warning for me. How to log it as such?
AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets
You can do this using the logging.captureWarnings function.
From the docs:
This function is used to turn the capture of warnings by logging on
and off.
If capture is True, warnings issued by the warnings module will be
redirected to the logging system. Specifically, a warning will be
formatted using warnings.formatwarning() and the resulting string
logged to a logger named 'py.warnings' with a severity of WARNING.
If capture is False, the redirection of warnings to the logging system
will stop, and warnings will be redirected to their original
destinations (i.e. those in effect before captureWarnings(True) was
called).
Executing logging.captureWarnings(True) in appengine_config.py causes these warnings to be logged as warnings for me.
See also the docs for the warnings module.
Edit:
This question includes this code fragment for suppressing the message altogether:
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
# squelch warning
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.contrib.appengine.AppEnginePlatformWarning
)
I am using paramiko module for ssh connection.I am facing below problem:
No handlers could be found for logger
I am not getting the reason of this problem.I tried to get solution from below link but not able to get reason.
No handlers could be found for logger "paramiko.transport"
I am using below code:
1.ssh = paramiko.SSHClient()
2.ssh.set_missing_host_key_policy(
3.paramiko.AutoAddPolicy())
4.ssh.connect(serverip, username=username,
5.password=password,timeout=None)
6.transport = ssh.get_transport()
7.transport.set_keepalive(30)
8.stdin, stdout, stderr =ssh.exec_command(cmd)
9.tables=stdout.readlines()
10.ssh.close()
I think i am getting this problem at line no 8.Please advice how can i solve this.
I found the solution from this website.
Basically, you just need to add a line:
paramiko.util.log_to_file("filename.log")
Then all connection will be logged to the file.
cf http://docs.python.org/2.7/howto/logging.html#what-happens-if-no-configuration-is-provided
To make a long story short: Paramiko uses the logging package and do it the RightWay - which for a library package or module is to not assume anything about the execution context and let the application take care of logging configuration. You have not configured any logger so you get this message. The obvious solution is configure the logging according to your needs.
pg module in python for interacting with postgres is not giving any error message for DML queries.
Is there any alternative to pg module which gives meaningful error messages.
>>>import pg
>>>
>>>
>>>conn = pg.connect(dbname="db", user="postgres", host="localhost")
>>>print conn.query("delete from item where item_id=0")
>>>None
>>>print conn.query("delete from item where item_id=0")
>>>None #This should have been an error message
Why are you expecting an error message? I delete does not raise an error in the server if no records were found. So why do you expect a generally applicable database driver to raise an error?
I can't think of any database driver that would issue an error in that case because there may be perfectly legitimate reasons for it. In database terms, for example, an error usually means you are going to roll back a transaction.
If you are going to wrap in your own API, my recommendation is that you either decide how you want your application to address this or at most you raise warnings instead.
Do you know the psycopg2 module? It seems a very nice module to interact with PostgreSQL via Python. There is a tutorial available, besides the modules' documentation. In the examples given in the docs, commands that fail indeed print out error messages.
I personally don't have experience in this module. But it looks very nice!
I'm struggling getting something going that should be pretty simple. I'm working on some uniittests that calls code with some very verbose debug logging. How can I tell the logging module "set everything to debug, except this one module, make it info"
disable query logging - Google Appengine SDK Python
check the log filter, which can be used as module based filter.