Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

期望设置所有的日志输出格式为JSON,但是不生效。 #129

Open
MlAvanti opened this issue Aug 27, 2024 · 2 comments
Open

Comments

@MlAvanti
Copy link

1、项目说明: midway v3 + koa
1、期望效果:将所有应用日志均输出为JSON格式。
2、实际效果:仅midway-core日志生效。
midway-core.json.log:
{"timestamp":"2024-08-27 11:18:48.159","level":"info","pid":42469,"message":"[midway:redis] client connect success"}
midway-cron.json.log:
2024-08-27 11:18:00.048 ERROR 41827 [DailyAnalyzeJob] 出错了

3、config.default.ts代码实例:

midwayLogger: {
      default: {
        level: 'warn',
        dir: path.join(appInfo.baseDir, './../logs/'),
        transports: {
          console: false,
          error: {
            fileLogName: 'common-error.json.log',
          },
        },
        format: (info: LoggerInfo) => {
          const { timestamp, level, pid, message } = info;
          return JSON.stringify({
            timestamp,
            level,
            pid,
            message,
          });
        },
      },
      clients: {
        coreLogger: {
          fileLogName: 'midway-core.json.log',
        },
        appLogger: {
          fileLogName: 'midway-web.json.log',
        },
        agentLogger: {
          fileLogName: 'midway-agent.json.log',
        },
        cronLogger: {
          fileLogName: 'midway-cron.json.log',
        },
      },
    }

4、日志输出方式:

import { ILogger } from '@midwayjs/logger';

@Inject()
  logger: ILogger;

this.logger.error(err)

5、补充说明:通过手动设置修改Koa上下文日志格式,可以在请求时让conmon-error日志中输出JSON格式,但是在job定时任务里面依然无法输出JSON格式。

koa: {
       ...
      contextLoggerFormat: (info: LoggerInfo) => {
        const { timestamp, level, pid, message } = info;
        return JSON.stringify({
          timestamp,
          level,
          pid,
          message,
        });
      },
    },
@czy88840616
Copy link
Member

czy88840616 commented Aug 27, 2024

需要关闭 file,开启 json ?

midwayLogger: {
default: {
  level: 'warn',
  dir: path.join(appInfo.baseDir, './../logs/'),
  transports: {
    console: false,
    file: false,
    json:{
          // ...
     },
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants
@czy88840616 @MlAvanti and others