简体   繁体   English

NestJS 日志与 Google Cloud Stackdriver 详细级别不匹配

[英]NestJS logs not matching Google Cloud Stackdriver verbosity levels

I have a NestJS application that I am running on cloud run in Google Cloud (GCP).我有一个在 Google Cloud (GCP) 中运行的 NestJS 应用程序。 When viewing the logs in GCP from "Logs Explorer," I can see all log output from my NestJS app only when the default value is selected in the severity dropdown.从“日志资源管理器”查看 GCP 中的日志时,只有在严重性下拉列表中选择了default值时,我才能看到我的 NestJS 应用程序的所有日志输出。 When I change the severity level to filter certain verbosity levels, it does not apply the filter to the NestJS log output, it only shows the logs when I have the default log level selected.当我更改严重级别以过滤某些详细级别时,它不会将过滤器应用于 NestJS 日志输出,它仅在我选择了default日志级别时显示日志。

For example, in my NestJS application, when I do a log.debug("hello world") , I can see "DEBUG" in my log output in Logs Explorer using the 'default' severity, but when I change the severity dropdown from default to debug , I am expecting to see only the NestJS logs that correspond to debug but it does not show any logs.例如,在我的 NestJS 应用程序中,当我执行log.debug("hello world")时,我可以使用“默认”严重性在日志资源管理器中的日志输出中看到“DEBUG”,但是当我将严重性下拉列表从defaultdebug ,我希望只看到与debug对应的 NestJS 日志,但它不显示任何日志。 The GCP severity dropdown filter does not map to the NestJS logs correctly from what I am seeing.从我看到的内容来看,GCP 严重性下拉过滤器没有正确映射到 NestJS 日志。

Is there a way to configure NestJS logs to match Stackdriver verbosity levels within GCP log viewer?有没有办法配置 NestJS 日志以匹配 GCP 日志查看器中的 Stackdriver 详细级别? 在此处输入图像描述

You can do this by overriding the default logger with a customer logger and reformatting the logging string in such a way that the google log explorer will identify the severity and message separately.您可以通过使用客户记录器覆盖默认记录器并重新格式化记录字符串以使 google 日志浏览器将分别识别严重性和消息来做到这一点。

Below is your custom logger以下是您的自定义记录器


import { LoggerService } from "@nestjs/common";


export class CustomLogger implements LoggerService {

  log(message: any, ...optionalParams: any[]) {
    console.log(JSON.stringify({ "severity": "INFO", "message": message }));
  }

  /**
   * Write an "error" level log.
   */
  error(message: any, ...optionalParams: any[]) {
    console.log(JSON.stringify({"severity": "ERROR", "message": message }));
  }

  /**
   * Write a "warn" level log.
   */
  warn(message: any, ...optionalParams: any[]) {
    console.log(JSON.stringify({ "severity": "WARNING", "message": message }));
  }

  /**
   * Write a "debug" level log.
   */
  debug?(message: any, ...optionalParams: any[]) {
    console.log(JSON.stringify({ "severity": "DEBUG", "message": message }));
  }

  /**
   * Write a "verbose" level log.
   */
  verbose?(message: any, ...optionalParams: any[]) {
  }
}

Make sure you stringify the log message.确保对日志消息进行字符串化。 otherwise, google log explorer cannot identify the log level and the message separately.否则,google log explorer 无法分别识别日志级别和消息。

Add your custom logger to the Logger Module and make it global so that you can use it anywhere in your application.将您的自定义记录器添加到记录器模块并使其全局化,以便您可以在应用程序的任何位置使用它。


import { Module, Global } from '@nestjs/common';
import { CustomLogger } from './logger.service';
import { LoggingInterceptor } from './logging.interceptor';

@Global()
@Module({
  providers: [CustomLogger, LoggingInterceptor],
  exports: [CustomLogger],
})
export class LoggerModule {
}

Then register the logger module when boostrapping the application然后在 boostrap 应用程序时注册 logger 模块


import { CustomLogger } from './logger/logger.service';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    bufferLogs: true,
  });
  app.useLogger(app.get(CustomLogger));
  await app.listen(PORT)
}

Make a logging interceptor and inject your logging service to that and you are done.制作一个日志拦截器并将您的日志服务注入其中,您就完成了。


import { CustomLogger } from './logger.service';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
  constructor(private customLogger: CustomLogger) {}
  intercept(context: ExecutionContext, next) {
    const now = Date.now();
    const req = context.switchToHttp().getRequest();
    const method = req.method;
    const url = req.url;

    return next.handle().pipe(
      tap(() => {
        this.customLogger.log("logging string");
      }),
    );
  }
}

you can customize the logging string as you wish.您可以根据需要自定义日志记录字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Google Cloud Platform中清除Stackdriver日志? - How to clear Stackdriver logs in Google Cloud Platform? 无法将Celery任务日志推送到Google Cloud(堆栈驱动程序) - Unable to push Celery task logs to Google Cloud (stackdriver) Google Cloud Stackdriver - 如何按摘要字段对日志进行分组? - Google Cloud Stackdriver - how can I group logs by summary field? 将 python 中使用的库的日志发送到谷歌云堆栈驱动程序日志记录 - Send logs of libraries used in python to google cloud stackdriver logging 将堆栈驱动程序日志作为csv文件导出到Google Cloud Storage - Exporting stackdriver logs into Google Cloud Storage as csv files Google Cloud Container Optimized OS 主机日志到 stackdriver - Google Cloud Container Optimized OS host logs to stackdriver stackdriver 日志代理未显示从 Google 云平台上的 stackdriver 日志查看器中的自定义日志文件读取的日志 - stackdriver logging agent not showing logs read from a custom log file in stackdriver logging viewer on Google cloud platform 使用新的 Google Cloud 日志记录 jar 时,日志未显示在 Google Cloud Platform Stackdriver 中 - Logs not showing in Google Cloud Platform Stackdriver when using new Google Cloud logging jar 是否无法从Google Cloud Platform Stackdriver日志中删除审核日志? - Can't the audit logs be deleted from Google Cloud Platform Stackdriver logs? 使用 Google Cloud Run 进行 Stackdriver Trace - Stackdriver Trace with Google Cloud Run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM