2
0

test-log-cjs.cjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // CommonJS 测试日志写入
  2. const fs = require('fs/promises');
  3. const path = require('path');
  4. async function testLogDir() {
  5. const logDir = '/mnt/code/186-175-template-22/log-prd';
  6. const now = new Date();
  7. const year = now.getFullYear();
  8. const month = String(now.getMonth() + 1).padStart(2, '0');
  9. const day = String(now.getDate()).padStart(2, '0');
  10. const dateFolder = path.join(logDir, `${year}-${month}-${day}`);
  11. const logFile = path.join(dateFolder, 'feie-print-task-service-test.log');
  12. console.log('测试打印任务服务日志写入...');
  13. console.log('日志目录:', logDir);
  14. console.log('日期文件夹:', dateFolder);
  15. console.log('日志文件:', logFile);
  16. // 创建目录
  17. await fs.mkdir(dateFolder, { recursive: true });
  18. // 模拟打印任务服务的日志
  19. const messages = [
  20. `[${now.toISOString()}] [INFO] 创建打印任务\n{"tenantId":1,"taskId":"FEIE_TEST_001","printerSn":"924744594"}\n`,
  21. `[${now.toISOString()}] [DEBUG] 调用飞鹅API\n{"sn":"924744594","content":"测试内容","times":1}\n`,
  22. `[${now.toISOString()}] [WARN] 打印机响应慢\n{"taskId":"FEIE_TEST_001","retryCount":1}\n`,
  23. `[${now.toISOString()}] [ERROR] 打印失败\n{"error":"网络超时","code":-1,"taskId":"FEIE_TEST_001"}\n`
  24. ];
  25. for (const message of messages) {
  26. await fs.appendFile(logFile, message, 'utf8');
  27. }
  28. console.log('日志写入成功!');
  29. console.log('请检查文件:', logFile);
  30. // 读取并显示文件内容
  31. const content = await fs.readFile(logFile, 'utf8');
  32. console.log('\n文件内容:');
  33. console.log(content);
  34. }
  35. testLogDir().catch(console.error);