Python使用日志记录日志SQL错误

时间:2012-12-03 18:34:23

标签: python python-2.7

为什么EXCEPT没有捕获名称解析错误? 另外,我如何获取错误文本并将其扔进记录器设施? 谢谢。

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())

出现错误:

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 个答案:

答案 0 :(得分:1)

如果这代表了你的实际代码结构,你实际上是在调用doSomething()函数时将它传递给simpleLogger, not 传入callable然后调用它;因此,执行不会发生在try / except块中。

相关问题