1、写日志
log = nnlog.Logger('python.log',level='debug',backCount=5,when='D')
- level:输出日志级别,debug:把所有的日志都打印出来,info:打印info以上的日志,
- warning:打印warning以上的日志,error:只会打印error级别的日志
- backCount:备份几个日志
- when:以什么时间周期来生成,D:按天,H:小时,M:分钟
log = nnlog.Logger('python.log') log.debug() log.warning("xxxx") log.info("xxx") log.error() log.surprise() # 小猪佩奇
2、使用nnlog日志模块实例
log = nnlog.Logger('python.log', level='debug', backCount=5, when='D') mysql_info = { 'host': '118.24.*.**', 'port': 3306, 'db': 'jxz', 'password': '123456', 'charset': 'utf8', 'autocommit': True, 'user': 'jxz' } def op_db(sql, one_tag=True): try: log.debug("开始连接数据库,数据库连接信息是%s" % mysql_info) connect = pymysql.connect(**mysql_info) except Exception as e: log.debug("连接数据失败") log.error("连接数据失败,请检查") log.error(traceback.format_exc()) # 用来写详细的错误信息 # traceback.format_exc() # 拿到出错的那一大坨字符串 # traceback.print_exc() # 只print错误信息 return "001" else: log.debug("开始建立游标") cur = connect.cursor(pymysql.cursors.DictCursor) try: log.debug("开始执行sql") log.info("sql语句是%s" % sql) cur.execute(sql) except Exception as e: log.warning("sql错误!") return "002" else: if one_tag: result = cur.fetchone() # {} else: result = cur.fetchall() # [] finally: cur.close() connect.close() return result # op_db('sql')