Chinaunix首页 | 论坛 | 博客
  • 博客访问: 652860
  • 博文数量: 149
  • 博客积分: 3901
  • 博客等级: 中校
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-16 14:33
文章分类

全部博文(149)

文章存档

2014年(2)

2013年(10)

2012年(32)

2011年(21)

2010年(84)

分类: Python/Ruby

2010-06-28 11:02:23





import sys,
traceback

try :

    .....

except :
    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    _logger.debug('[error] row %s '% ( ... ))
    # 日志输出
    _logger.error(exceptionType, exceptionValue, exceptionTraceback,limit=10)
    traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)




日志类 :

#!/usr/bin/python
#encoding: utf-8
import sys,traceback

class loggingUtils :
    def __init__(self,logfile) :
        import logging
        self.logger = logging.getLogger(logfile)
        self.hdlr = logging.FileHandler(logfile)
        formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
        self.hdlr.setFormatter(formatter)
        self.logger.addHandler(self.hdlr)
        self.logger.setLevel(logging.DEBUG)

    def debug(self,msg):
        self.logger.debug( msg )
        self.hdlr.flush()
        #logger.removeHandler( hdlr )网络上的实现基本为说明这条语句的使用和作用


    def error(self,exceptionType, exceptionValue, exceptionTraceback,limit=None):
        if limit is None:
            if hasattr(sys, 'tracebacklimit'):
                limit = sys.tracebacklimit
        n = 0
        eline = '\n'
        while exceptionTraceback is not None and (limit is None or n < limit):
            f = exceptionTraceback.tb_frame
            lineno = exceptionTraceback.tb_lineno
            co = f.f_code
            filename = co.co_filename
            name = co.co_name
            eline += ' File "%s", line %d, in %s \n ' % (filename,lineno,name)
            exceptionTraceback = exceptionTraceback.tb_next
            n = n+1

        eline+= "\n".join(traceback.format_exception_only(exceptionType, exceptionValue))
        self.logger.error(eline)
        self.hdlr.flush()




一个 python 连 mysql 的异常处理

#encoding: utf-8
import ConfigParser
import MySQLdb
import time,os,sys,re
import traceback


'''
API 使用 :
from utils import *
db=DB()

for row in db.select('
select * from pu_feed_user ;') :
    print row

print db.ddl('
insert into pu_feed_user (fuid,uid,foid,valid) values( 2,12,13,14 ) ')

db.close()

'
''
class DB():
    def __init__(self):
        config = ConfigParser.RawConfigParser()
        config.read('/........./...conf')
        # 使用 conn = getConn()
        self.conn = MySQLdb.connect(
                        host=config.get('db', 'host'), port=config.getint('db', 'port') ,
                        user=config.get('db', 'user'), passwd=config.get('db', 'passwd'),
                        db=config.get('db', 'database'), charset=config.get('db', 'charset'))

    def select(self,sql):
        cursor = self.conn.cursor ()
        try :
            try :
                cursor.execute (sql)
                return cursor.fetchall ()
            except :
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)
        finally :
            try:
                cursor.close()
            except :
                cursor = None

    def ddl(self,sql):
        cursor = self.conn.cursor ()
        try :
            try :
                cursor.execute (sql)
                return cursor.rowcount
            except :
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,limit=2)
        finally :
            try:
                self.conn.commit ()
                cursor.close()
            except :
                cursor = None

    def close(self):
        self.conn.close()


阅读(1424) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~