挣扎着记录(Flask / Module / Debug)

时间:2016-03-23 15:34:51

标签: python python-3.x flask python-multithreading

我在多线程规则引擎之上有一个Web UI。在大多数情况下,Web UI旨在为规则引擎收集的数据以及规则服务的启动和关闭提供标准CRUD服务。

我想将烧瓶记录器(current_app.logger)附加到模块级日志记录输出,然后为每个正在运行的服务配置输出日志。

简单测试失败: rules.py测试功能:

def stupid_logging_test():
    logging.debug('Will flask log me?')

admin.py

@admin.route('/', methods=['GET', 'POST'])
def admin_root():
    #show the root admin page.
    servers = current_app.config['instance_servers']
    metrics = None
    if len(servers):
        metrics = compileMetrics(servers)
    # this includes starting/stopping rules engine and metrics
    adminform = ServerAdminForm(request.form)
    if request.method == 'POST':
        if adminform.startserver.data:
            current_app.logger.info('performing rule server start.')
            start_source_servers()
            #current_app.logger.debug(pprint.pformat(form))
            #current_app.logger.debug(str(tabledata))
        elif adminform.stopserver.data:
            current_app.logger.info('performing rule server stop.')
            stop_source_servers()
            #current_app.logger.debug(pprint.pformat(form))
            #current_app.logger.debug(str(tabledata))
        else:
            current_app.logger.info('unknown submit?')
            current_app.logger.debug('debug line test.')
            rules.stupid_logging_test()

    return render_template("admin_main.html", adminform=adminform,
            metrics=metrics, servers=servers)

记录输出(控制台):

--------------------------------------------------------------------------------
INFO in admin [/<snip>/admin.py:51]:
unknown submit?
--------------------------------------------------------------------------------
INFO:ui:unknown submit?
--------------------------------------------------------------------------------
DEBUG in admin [/<snip>/admin.py:52]:
debug line test.
--------------------------------------------------------------------------------
DEBUG:ui:debug line test.

logging output (file)

unknown submit?
debug line test.

最后这是我的日志配置(到目前为止)

ui:__init__.py

# ... other app config
log_target = RotatingFileHandler(app.config['LOGFILE'], maxBytes=1024 * 1024 * 100, backupCount=20)
if 'DEBUG' in app.config and app.config['DEBUG']:
    log_target.setLevel(logging.DEBUG)
else:
    log_target.setLevel(logging.WARN)
app.logger.addHandler(log_target)
rules_logger = getLogger('rules')
rules_logger.addHandler(log_target)
app.config['core_log'] = log_target

我觉得我的问题与getLogger方法有关。我没有给记录器命名,但我认为默认值是模块本身的名称?

1 个答案:

答案 0 :(得分:0)

在处理程序上设置loglevel是不正确的。以下更改解决了此问题: ui:__init__.py

log_target = RotatingFileHandler(app.config['LOGFILE'], maxBytes=1024 * 1024 * 100, backupCount=20)
loggers = [app.logger, getLogger('rules')]
for logger in loggers:
    if 'DEBUG' in app.config and app.config['DEBUG']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.WARN)
    logger.addHandler(log_target)
app.config['core_log'] = log_target