简体   繁体   English

在调试级别写日志最合理的地方在哪里?

[英]Where is the most reasonable place to write logs at a debug level?

My team and I are lost in deep thoughts for a couple of days to figure out the right place for writing a debug-level log during development.我和我的团队沉思了几天,想找出在开发过程中编写调试级日志的正确位置。

We're using winston and winston-daily-rotate-file to decouple parts of the logging process, and nest-winston , a nest module wrapper for winston logger.我们正在使用winstonwinston-daily-rotate-file来解耦部分日志记录过程,以及nest-winston ,一个用于 winston 记录器的嵌套模块包装器。

We have decided to create a flexible custom logger as a service by extending built-in Logger class.我们决定通过扩展内置的 Logger 类来创建一个灵活的自定义记录器即服务。

@Injectable()
export class LoggerService extends Logger {
  constructor(
    @Inject('winston')
    private readonly logger: winston.Logger,
  ) { super(); }

 info(message: string, context?: string): void {
   this.logger.info(message, { context });
   super.log(message, context);
  }

 debug(message: string, context?: string): void {
   // To delegate the call to the parent class, no winston used.
   super.debug(message, context);
  }

 error(message: string, trace: string, context?: string): void {
   this.logger.error(message, { context });
   super.error(message, trace, context);
  }
}

As you may have noticed from a method debug() , the storage device ( Transports ) has not configured at debug level on purpose.您可能已经从debug()方法中注意到,存储设备 ( Transports ) 没有故意在调试级别进行配置。 We want them to be printed out through the console only in development.我们希望它们仅在开发中通过控制台打印出来。

Now we could use our LoggerService from anywhere in the same context.现在我们可以在同一上下文中的任何地方使用我们的 LoggerService。 For example,例如,

@Controller('users')
export class UsersController {
  constructor(private readonly logger: LoggerService) {}

}

@Injectable()
export class UsersService {
  constructor(private readonly logger: LoggerService) {}

  // Inside a method, debug some logic.
  this.logger.debug(message, UsersService.name);
}

The approach looks fine at a glance, but it could become very messy when the code overused by elsewhere.该方法一目了然,但当代码被其他地方过度使用时,它可能会变得非常混乱。

For this reason, we thought to have a place where dealing with a debugging process in one shared spot and came up with an idea to let the interceptors handle the work.出于这个原因,我们认为有一个地方可以在一个共享的地方处理调试过程,并提出了让拦截器处理工作的想法。

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

@Injectable()
export class DebuggingInterceptor implements NestInterceptor {
  constructor(private readonly logger: LoggerService) {}

  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const ctx = `${context.getClass().name} ➜ ${context.getHandler().name}()`;

    return next
      .handle()
      .pipe(
        tap((response) => {
          if (process.env.NODE_ENV === 'development') {
            this.logger.debug(response, ctx);
          }
        }),
      );
  }
}

Before printing out the debug log, checking the environment whether it's in development or not looks a bit ugly to me.在打印调试日志之前,检查环境是否在开发中对我来说有点难看。

I'm afraid that what if the approach of using an interceptor above might be completely wrong?我担心如果上面使用拦截器的方法可能完全错误怎么办?

How can I fix this issue in a better way?我怎样才能更好地解决这个问题?

I think the approach of using an interceptor is fine.我认为使用拦截器的方法很好。 If you don't like the idea of checking the environment in the interceptor you could always check it in the LoggerService class to decide whether or not to call the super.debug() method or not, that way you can just call this.logger.debug(response, ctx) .如果你不喜欢在拦截器中检查环境的想法,你可以随时在LoggerService类中检查它来决定是否调用super.debug()方法,这样你就可以调用this.logger.debug(response, ctx)

As a side note, I'm working on my own version of the overwritten logger and working out the kinks of injecting the class name into the logger so that the context is set in the logger and used from there, but that's taking some time to work with.作为旁注,我正在开发我自己版本的覆盖记录器并解决将类名注入记录器的问题,以便在记录器中设置上下文并从那里使用,但这需要一些时间与。 Just a thought to another idea.只是一个想法到另一个想法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM