refactor(server): enhance the Logger usage (#9763)
This commit is contained in:
@@ -4,6 +4,7 @@ import cookieParser from 'cookie-parser';
|
|||||||
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
AFFiNELogger,
|
||||||
CacheInterceptor,
|
CacheInterceptor,
|
||||||
CloudThrottlerGuard,
|
CloudThrottlerGuard,
|
||||||
GlobalExceptionFilter,
|
GlobalExceptionFilter,
|
||||||
@@ -20,8 +21,11 @@ export async function createApp() {
|
|||||||
cors: true,
|
cors: true,
|
||||||
rawBody: true,
|
rawBody: true,
|
||||||
bodyParser: true,
|
bodyParser: true,
|
||||||
|
bufferLogs: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.useLogger(app.get(AFFiNELogger));
|
||||||
|
|
||||||
if (AFFiNE.server.path) {
|
if (AFFiNE.server.path) {
|
||||||
app.setGlobalPrefix(AFFiNE.server.path);
|
app.setGlobalPrefix(AFFiNE.server.path);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ConsoleLogger, Injectable, Scope } from '@nestjs/common';
|
import { ConsoleLogger, Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
@Injectable({
|
// DO NOT use this Logger directly
|
||||||
scope: Scope.TRANSIENT,
|
// Use it via this way: `private readonly logger = new Logger(MyService.name)`
|
||||||
})
|
@Injectable()
|
||||||
export class AFFiNELogger extends ConsoleLogger {}
|
export class AFFiNELogger extends ConsoleLogger {}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { FactoryProvider } from '@nestjs/common';
|
import { FactoryProvider, Logger } from '@nestjs/common';
|
||||||
import { createTransport, Transporter } from 'nodemailer';
|
import { createTransport, Transporter } from 'nodemailer';
|
||||||
import SMTPTransport from 'nodemailer/lib/smtp-transport';
|
import SMTPTransport from 'nodemailer/lib/smtp-transport';
|
||||||
|
|
||||||
import { Config } from '../config';
|
import { Config } from '../config';
|
||||||
import { AFFiNELogger } from '../logger';
|
|
||||||
|
|
||||||
export const MAILER_SERVICE = Symbol('MAILER_SERVICE');
|
export const MAILER_SERVICE = Symbol('MAILER_SERVICE');
|
||||||
|
|
||||||
@@ -15,9 +14,9 @@ export const MAILER: FactoryProvider<
|
|||||||
Transporter<SMTPTransport.SentMessageInfo> | undefined
|
Transporter<SMTPTransport.SentMessageInfo> | undefined
|
||||||
> = {
|
> = {
|
||||||
provide: MAILER_SERVICE,
|
provide: MAILER_SERVICE,
|
||||||
useFactory: (config: Config, logger: AFFiNELogger) => {
|
useFactory: (config: Config) => {
|
||||||
if (config.mailer) {
|
if (config.mailer) {
|
||||||
logger.setContext('Mailer');
|
const logger = new Logger('Mailer');
|
||||||
const auth = config.mailer.auth;
|
const auth = config.mailer.auth;
|
||||||
if (auth && auth.user && !('pass' in auth)) {
|
if (auth && auth.user && !('pass' in auth)) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
@@ -30,5 +29,5 @@ export const MAILER: FactoryProvider<
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
inject: [Config, AFFiNELogger],
|
inject: [Config],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { Injectable, OnModuleInit, Optional } from '@nestjs/common';
|
import { Injectable, Logger, OnModuleInit, Optional } from '@nestjs/common';
|
||||||
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule';
|
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AFFiNELogger,
|
|
||||||
CallMetric,
|
CallMetric,
|
||||||
Config,
|
Config,
|
||||||
type EventPayload,
|
type EventPayload,
|
||||||
@@ -15,16 +14,14 @@ import { PgWorkspaceDocStorageAdapter } from './adapters/workspace';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class DocStorageCronJob implements OnModuleInit {
|
export class DocStorageCronJob implements OnModuleInit {
|
||||||
private busy = false;
|
private busy = false;
|
||||||
|
private readonly logger = new Logger(DocStorageCronJob.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly config: Config,
|
private readonly config: Config,
|
||||||
private readonly db: PrismaClient,
|
private readonly db: PrismaClient,
|
||||||
private readonly workspace: PgWorkspaceDocStorageAdapter,
|
private readonly workspace: PgWorkspaceDocStorageAdapter,
|
||||||
private readonly logger: AFFiNELogger,
|
|
||||||
@Optional() private readonly registry?: SchedulerRegistry
|
@Optional() private readonly registry?: SchedulerRegistry
|
||||||
) {
|
) {}
|
||||||
this.logger.setContext(DocStorageCronJob.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
onModuleInit() {
|
onModuleInit() {
|
||||||
if (this.registry && this.config.doc.manager.enableUpdateAutoMerging) {
|
if (this.registry && this.config.doc.manager.enableUpdateAutoMerging) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { LoggingWinston } from '@google-cloud/logging-winston';
|
import { LoggingWinston } from '@google-cloud/logging-winston';
|
||||||
import { LoggerService, Provider, Scope } from '@nestjs/common';
|
import { LoggerService, Provider } from '@nestjs/common';
|
||||||
import { createLogger, transports } from 'winston';
|
import { createLogger, transports } from 'winston';
|
||||||
|
|
||||||
import { AFFiNELogger as LoggerProvide } from '../../../base/logger';
|
import { AFFiNELogger as LoggerProvide } from '../../../base/logger';
|
||||||
@@ -20,7 +20,4 @@ export const loggerProvider: Provider<LoggerService> = {
|
|||||||
});
|
});
|
||||||
return new AFFiNELogger(instance);
|
return new AFFiNELogger(instance);
|
||||||
},
|
},
|
||||||
// use transient to make sure the logger is created for each di context
|
|
||||||
// to make the `setContext` method works as expected
|
|
||||||
scope: Scope.TRANSIENT,
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user