Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning off logging in Paramiko

Tags:

I am using the ssh client provided by Paramiko to create a function call remoteSSH (the file name is remoteConnect.py):

import paramiko import logging logger = paramiko.util.logging.getLogger() logger.setLevel(logging.WARN)  def remoteSSH(username,userpasswd):     .... 

Now I am calling the remoteSSH function in another Python module called getData() (getdata.py):

from remoteConnect import * import logging logger2=logging.getLogger() logger2.setLevel(logging.INFO) 

However, a call to logger2.info('ccc') also turns on all INFO level logging in the file that is importing the Paramiko module (i.e. remoteConnect.py)

How do I turn off logging in remoteConnect.py so that Paramiko does not spit out all the INFO level messages?

like image 904
user963986 Avatar asked Nov 15 '11 23:11

user963986


2 Answers

Paramiko names its logggers. It seems to function as the logging modules in other languages (JDK logging comes to mind) do.

I've found that

logging.getLogger("paramiko").setLevel(logging.WARNING) helps.

(You can put this inside the module that's importing paramiko - just make sure the 'logging' module is enabled as well).

It took me a while to figure out how to do this (in fact, it wasn't until I actually started dealing with Java logging that this answer came to mind)

like image 126
Mark Nunberg Avatar answered Sep 20 '22 17:09

Mark Nunberg


You're setting the root logger's level to WARN (should be WARNING) in remoteConnect.py, and to INFO in getdata.py. I would advise that you avoid setting levels on the root logger in random modules in your application: instead, do this in all your modules where you want to use logging:

import logging  logger = logging.getLogger(__name__) 

and use logger.debug(...), etc. in that module. Then, in one specific place in your application (typically in your logic called from if __name__ == '__main__':, set the levels and handlers that you want, either programmatically via basicConfig or a set of API calls to add handlers, formatters etc., or through the use of a declarative configuration (e.g. using fileConfig or dictConfig APIs - documented here).

like image 38
Vinay Sajip Avatar answered Sep 20 '22 17:09

Vinay Sajip