首页 > 解决方案 > Snakemake:使用 Python fileConfig() 时缺少 RuntimeError 堆栈跟踪

问题描述

我有一个带有一条规则的蛇文件,它导入本地 Python 脚本,然后引发RuntimeError. 当我运行蛇文件时,没有显示堆栈跟踪RuntimeError。代码和snakemake 输出如下所示。

// test.snakefile
rule test_rule:
    run:
        from test import hello
        print(hello)
        raise RuntimeError('raising error')
// test.py
import logging
import os
from logging.config import fileConfig

log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logging_config.ini')
fileConfig(log_file_path)

hello = 'hello'

蛇形输出:

...
[Mon Jan 13 14:45:54 2020]
rule test_rule:
    jobid: 0

Job counts:
    count   jobs
    1   test_rule
    1
hello
Shutting down, this might take some time.
Exiting because a job execution failed. Look above for error message

但是,如果我注释掉其中的行fileConfig(log_file_path)test.py运行snakemake,则会按预期打印 RuntimeError 堆栈跟踪:

Error in rule test_rule:
    jobid: 0

RuleException:
RuntimeError in line 5 of /my-dir/test.snakefile:
raising error
  File "/my-dir/test.snakefile", line 5, in __rule_test_rule
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
Exiting because a job execution failed. Look above for error message

有谁知道为什么会这样?

编辑:logging_config.ini

[loggers]
keys=root

[handlers]
keys=stream_handler

[formatters]
keys=formatter

[logger_root]
level=INFO
handlers=stream_handler

[handler_stream_handler]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=formatter
args=('/tmp/experiments.log', 'midnight')

[formatter_formatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s

标签: pythonloggingsnakemake

解决方案


这个问题与如何logging定义模块test.py以及如何在Snakefile. 以下设置应该可以工作。请注意代码块内的注释以了解修改的描述。

测试.py

import logging
import os
from logging.config import fileConfig

fileConfig('logging_config.ini')
logger = logging.getLogger(__name__)   # assigns to "logger", through which logs are passed. 
logger.info('test.py debug msg')   # example log message

hello = 'yo yo yo'

蛇文件

rule test_rule:
    run:
        from test import hello, logger.   # imports both hello and logger
        print(hello)
        print (logger)

        logger.exception("Exception occurred")   # error log that would capture stack traces, if any. No stack trace in this example.
        raise SystemExit(1)     # exits with non-zero error

logging_config.ini - 与问题中发布的相同。

在这个设置中,变量hello和类logger被导入到 ruletest_rule中,并且它们的日志输出被写入到 file/tmp/experiments.log中,如logging_config.ini.


推荐阅读