0

Why Name resolution error is not being caught by EXCEPT ? Also, how can i pick up error text and throw it into the logger facility as well ? Thank you.

logging.basicConfig(filename = "{}.log".format(sys.argv[0]), level=logging.DEBUG)

def simpleLogger(something):
     try :
         something
         logging.info("Iteration fine")
     except :

         logging.warning("Something bad happened!")

 def doSometh():
     conn = psycopg2.connect(host = "1x18.249.21" , user = "tima",  dbname = "tima")
     curr = conn.cursor()
     sql = "select * from app_catalog;"

     curr.execute(sql)

     print curr.fetchall()

 def main():
    simpleLogger(doSometh())


 if __name__ == '__main__':
     sys.exit(main())

Here is error produced :

conn = psycopg2.connect(host = "172.x18.249.21" , user = "tima",  dbname = "tima")
File "/apps/appeng/python-2.7.3/lib/python2.7/site-packages/psycopg2/__init__.py", line   179, in connect   connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not translate host name "172.x18.249.21" to address: Name or service not known
1
  • can you copy and paste your exact errors? Commented Dec 3, 2012 at 18:43

1 Answer 1

1

If this is representative of your actual code structure, you're actually calling the doSomething() function at the point of passing it in to simpleLogger, not passing in the callable and then calling it; thus the execution doesn't take place within the try/except block.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. As i understood, if i want to react on result of doSomething(), i need decorator to wrap up this function and log if doSomething() output is suspicious, etc... Am i correct?
Well, to be frank I normally just use log logic within the function itself. Not glamorous or neato, but it's very simple and clear. In your case, with this structure, if you're decorating any callable you just need to accept a reference to that callable and then call it within the body of the decorator. The problem in your example is simply that you're calling the function at the point where you're trying to pass it in. Remove the parenthesis from doSometh() in the __main__ block and see what happens.
Thanks a lot syrion. Will dig further about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.