如何简化Cypress Electron控制台输出,移除时间戳与来源信息?
简化Cypress无头模式下Electron客户端应用的日志输出
当前启动命令
@SET ELECTRON_ENABLE_LOGGING=true @SET DEBUG=cypress:electron @SET CYPRESS_BASE_URL=http://127.0.0.1:15002 npx cypress run --spec "cypress/e2e/01_base.cy.js"
当前日志输出
[18816:0226/074643.800:INFO:CONSOLE(85)] "mylog message 1", source: http://127.0.0.1:15002/__cypress/tests?p=cypress\support\e2e.js (85) [18816:0226/074644.607:INFO:CONSOLE(82)] "mylog message 2", source: http://127.0.0.1:15002/editor/webChannel.js (82)
期望日志输出
mylog message 1 mylog message 2
问题背景
日志来自运行在Electron中的客户端应用,而非Cypress测试内部。已知electron-log可通过log.transports.console.format = '{text}'调整输出格式,但不清楚配置位置,同时希望通过环境变量配置格式但未找到原生支持项。
解决方案
1. 在客户端应用代码中配置格式
直接在Electron客户端的启动入口文件(如主进程main.js)中初始化electron-log时设置格式:
const log = require('electron-log'); log.transports.console.format = '{text}';
确保这段代码在客户端应用输出任何日志前执行,配置才会生效。
2. 自定义环境变量实现动态配置
electron-log无原生环境变量配置格式的支持,可自行在客户端代码中读取自定义环境变量:
const log = require('electron-log'); // 读取环境变量,默认使用'{text}'格式 const logFormat = process.env.ELECTRON_LOG_FORMAT || '{text}'; log.transports.console.format = logFormat;
启动Cypress时新增环境变量:
@SET ELECTRON_ENABLE_LOGGING=true @SET DEBUG=cypress:electron @SET CYPRESS_BASE_URL=http://127.0.0.1:15002 @SET ELECTRON_LOG_FORMAT={text} npx cypress run --spec "cypress/e2e/01_base.cy.js"
3. 管道过滤临时方案(无需修改客户端)
若无法修改客户端代码,可通过Windows cmd的管道命令过滤输出(需安装GNU sed工具):
@SET ELECTRON_ENABLE_LOGGING=true @SET DEBUG=cypress:electron @SET CYPRESS_BASE_URL=http://127.0.0.1:15002 npx cypress run --spec "cypress/e2e/01_base.cy.js" | findstr /R /C:"\"mylog message.*\"" | sed 's/.*"\(mylog message.*\)".*/\1/'
内容的提问来源于stack exchange,提问作者Johannes Munk
相关产品推荐
相关产品推荐

