在BAE python 环境,后台程序log经常会看到这样未知错误,想想GAE 还是很好用。还好BAE 提供一个log 输出方式。只能自己跟踪,看哪一步出错了。
BAE python 出错处理、日志、标准输出
您在使用BAE Python开发应用时,关于出错处理、日志以及标准输出,有以下事项应当注意:
- 应用程序通过返回值将输出结果返回给WEB服务器;
- stdout和stderr被重定向到 /dev/null。这意味着您在开发过程中不能使用stdout和stderr来进行调试;
- wsgi.stderr被关闭;
- 您在开发应用的过程中应使用BAE提供的日志模块bae.api.logging来输出日志。使用该模块输出的日志,会被发送到专门的日志服务器,可通过WEB界面来查看日志;
- 不建议使用Python标准库中的logging模块,使用该模块输出的日志信息无法查看。
BAE Python环境中的日志服务示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from bae.api import logging
#打印一条critical日志
logging.critical('critical log')
#打印一条error日志
logging.error('error log')
#打印一条warning日志
logging.warning('warning log')
#打印一条info日志
logging.info('info log')
#打印一条debug日志
logging.debug('debug log')
#打印一条exception日志(包含异常traceback信息)
try:
raise Exception('raise')
except:
logging.exception('exception log')
#获取一个Logger对象(注意:日志服务中的Logger为单例模式)
test_log = logging.getLogger('test')
test_log.critical('test')
test_log.error('test')
test_log.warning('test')
test_log.info('test')
test_log.debug('test')
test_log.exception('test')