使用 log4js 控制台附加器开玩笑附加 "console.log" 和一个虚拟堆栈
jest with log4js console appender appends "console.log" and a dummy stack
当我 运行 我的 jest
测试使用 log4js
的控制台附加程序时,每个输出行都散布着 console:
和一个小的堆栈跟踪(输出和下面的代码),这使得跟踪日志流程变得更加困难。
问题:如何去掉这些添加的文字?
当前解决方案: 将类型替换为 type: 'stdout'
解决了这个问题。 [Edit] 但是,当 运行 不是通过 jest 编译代码时,日志输出不会出现在 vscode的“调试控制台”
备注:
- 当 运行仅使用节点代码时不会发生这种情况 - 仅在玩笑中。
- 将
enableCallStack: false
添加到类别 - 没有任何改变
- 向 appender 添加
layout
会影响该行 ([2021-12-28T13:12:20.334] [INFO] default - myinfo
),但周围的行仍然存在。
版本: Node.js v14.18.1,jest@26.6.3,log4js@6.3.0
输出
PS > node --experimental-vm-modules .\node_modules\jest\bin\jest
(node:21824) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./test.spec.js
√ testname (13 ms)
console.log <<<<<< want to get rid of this
[2021-12-28T13:12:20.334] [INFO] default - myinfo <<<<<< want to keep only this
at node_modules/log4js/lib/appenders/console.js:6:5 <<<<<< want to get rid of this
at Array.forEach (<anonymous>) <<<<<< want to get rid of this
at Array.forEach (<anonymous>) <<<<<< want to get rid of this
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.366 s
代码
import log4js from 'log4js';
log4js.configure({
appenders: { console: { type: 'console' } },
categories: { default: { appenders: [ 'console' ], level: 'trace' } }
});
let logger = log4js.getLogger();
test('testname', () => {
logger.info("myinfo");
});
// my actual code
function myActualCode
正如@jonrsharpe 指出的那样,这是设计使然。供参考:这是在node_modules\@jest\console\build\getConsoleOutput.js
中实现的
我的解决方案:,如果是,切换到标准输出:
log4js.configure(
{
appenders: {
myAppender: {
type: (process.env.JEST_WORKER_ID) ? 'stdout' : 'console',
layout: ...
当我 运行 我的 jest
测试使用 log4js
的控制台附加程序时,每个输出行都散布着 console:
和一个小的堆栈跟踪(输出和下面的代码),这使得跟踪日志流程变得更加困难。
问题:如何去掉这些添加的文字?
当前解决方案: 将类型替换为 type: 'stdout'
解决了这个问题。 [Edit] 但是,当 运行 不是通过 jest 编译代码时,日志输出不会出现在 vscode的“调试控制台”
备注:
- 当 运行仅使用节点代码时不会发生这种情况 - 仅在玩笑中。
- 将
enableCallStack: false
添加到类别 - 没有任何改变 - 向 appender 添加
layout
会影响该行 ([2021-12-28T13:12:20.334] [INFO] default - myinfo
),但周围的行仍然存在。
版本: Node.js v14.18.1,jest@26.6.3,log4js@6.3.0
输出
PS > node --experimental-vm-modules .\node_modules\jest\bin\jest
(node:21824) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./test.spec.js
√ testname (13 ms)
console.log <<<<<< want to get rid of this
[2021-12-28T13:12:20.334] [INFO] default - myinfo <<<<<< want to keep only this
at node_modules/log4js/lib/appenders/console.js:6:5 <<<<<< want to get rid of this
at Array.forEach (<anonymous>) <<<<<< want to get rid of this
at Array.forEach (<anonymous>) <<<<<< want to get rid of this
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.366 s
代码
import log4js from 'log4js';
log4js.configure({
appenders: { console: { type: 'console' } },
categories: { default: { appenders: [ 'console' ], level: 'trace' } }
});
let logger = log4js.getLogger();
test('testname', () => {
logger.info("myinfo");
});
// my actual code
function myActualCode
正如@jonrsharpe 指出的那样,这是设计使然。供参考:这是在node_modules\@jest\console\build\getConsoleOutput.js
我的解决方案:
log4js.configure(
{
appenders: {
myAppender: {
type: (process.env.JEST_WORKER_ID) ? 'stdout' : 'console',
layout: ...