我目前正在XAMPP
本地运行我的代码。
我目前的log
文件设置如下:
log.py
import logging
def setup_custom_logger(name):
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
handler = logging.FileHandler('error.log')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger = logging.getLogger('sqlalchemy.engine')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
return logger
这是我的意思的一个例子: 的 views.py
import log
logger = log.setup_custom_logger('root')
@app.route('/restaurants', methods = ['GET'])
def getRestaurants():
try:
restaurantsList = session.query(Restaurant).all()
except:
logger.debug("couldnt get restaurants")
sqltables.py
class Restaurant(Base):
__tablename__ = 'restaurant'
RESTAURANTID = Column(Integer, primary_key=True)
RESTAURANTNAME = Column(String(50), nullable=False)
RESTAURANTDESCRIPTION = Column(String(1024))
RESTAURANTCUISINE = Column(String(1024))
RESTAURANTOPENING = Column(String(512))
RESTAURANTEMAIL = Column(String(100))
RESTAURANTPHONE = Column(String(100))
RESTAURANTADDRESS = Column(String(100))
waiter = relationship(u'Waiter', secondary='restaurant_waiter')
user = relationship(u'User', secondary='restaurant_users')
def serialize(self):
return {
'id': self.RESTAURANTID,
'name': self.RESTAURANTNAME,
'description': self.RESTAURANTDESCRIPTION,
'cuisine': self.RESTAURANTCUISINE,
'opening': self.RESTAURANTOPENING,
'email': self.RESTAURANTEMAIL,
'phone': self.RESTAURANTPHONE,
'address': self.RESTAURANTADDRESS
}
在上面的例子中,我在浏览器中收到500
错误消息。
我在error.log
2015-03-13 19:11:07,924 - INFO - base - SHOW VARIABLES LIKE 'sql_mode'
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - DEBUG - result - Col ('Variable_name', 'Value')
2015-03-13 19:11:07,924 - DEBUG - result - Row ('sql_mode', 'NO_ENGINE_SUBSTITUTION')
2015-03-13 19:11:07,924 - INFO - base - SELECT DATABASE()
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - DEBUG - result - Col ('DATABASE()',)
2015-03-13 19:11:07,924 - DEBUG - result - Row ('lokachat_menumaster',)
2015-03-13 19:11:07,924 - INFO - base - show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - DEBUG - result - Col ('Collation', 'Charset', 'Id', 'Default', 'Compiled', 'Sortlen')
2015-03-13 19:11:07,924 - DEBUG - result - Row ('utf8_bin', 'utf8', 83L, '', 'Yes', 1L)
2015-03-13 19:11:07,924 - INFO - base - SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - INFO - base - SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - INFO - base - SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2015-03-13 19:11:07,924 - INFO - base - ()
2015-03-13 19:11:07,924 - DEBUG - views - couldnt get restaurants
Apache Error log
不包含任何有用的内容。
如何在日志文件中获取更多信息?
答案 0 :(得分:0)
OP使用logging
不正确。
我建议始终遵循这种模式:
logging.debug("user %r", user)
这将正确处理奇None
或3.1459
等奇数值,并正确转换不可打印的字符串。
最后,由于它是一个Web服务器,我可以建议您将顶级函数包装在try/except
块中,例如:
try:
render_user_or_something()
except:
logging.exception("oh no!")
raise
虽然这本身并不能解决您的问题,但它会告诉您代码中断的位置。