// CommonJS 测试日志写入
const fs = require('fs/promises');
const path = require('path');

async function testLogDir() {
  const logDir = '/mnt/code/186-175-template-22/log-prd';
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const dateFolder = path.join(logDir, `${year}-${month}-${day}`);
  const logFile = path.join(dateFolder, 'feie-print-task-service-test.log');

  console.log('测试打印任务服务日志写入...');
  console.log('日志目录:', logDir);
  console.log('日期文件夹:', dateFolder);
  console.log('日志文件:', logFile);

  // 创建目录
  await fs.mkdir(dateFolder, { recursive: true });

  // 模拟打印任务服务的日志
  const messages = [
    `[${now.toISOString()}] [INFO] 创建打印任务\n{"tenantId":1,"taskId":"FEIE_TEST_001","printerSn":"924744594"}\n`,
    `[${now.toISOString()}] [DEBUG] 调用飞鹅API\n{"sn":"924744594","content":"测试内容","times":1}\n`,
    `[${now.toISOString()}] [WARN] 打印机响应慢\n{"taskId":"FEIE_TEST_001","retryCount":1}\n`,
    `[${now.toISOString()}] [ERROR] 打印失败\n{"error":"网络超时","code":-1,"taskId":"FEIE_TEST_001"}\n`
  ];

  for (const message of messages) {
    await fs.appendFile(logFile, message, 'utf8');
  }

  console.log('日志写入成功！');
  console.log('请检查文件:', logFile);

  // 读取并显示文件内容
  const content = await fs.readFile(logFile, 'utf8');
  console.log('\n文件内容:');
  console.log(content);
}

testLogDir().catch(console.error);