mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
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 {
|
||||
AFFiNELogger,
|
||||
CacheInterceptor,
|
||||
CloudThrottlerGuard,
|
||||
GlobalExceptionFilter,
|
||||
@@ -20,8 +21,11 @@ export async function createApp() {
|
||||
cors: true,
|
||||
rawBody: true,
|
||||
bodyParser: true,
|
||||
bufferLogs: true,
|
||||
});
|
||||
|
||||
app.useLogger(app.get(AFFiNELogger));
|
||||
|
||||
if (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({
|
||||
scope: Scope.TRANSIENT,
|
||||
})
|
||||
// DO NOT use this Logger directly
|
||||
// Use it via this way: `private readonly logger = new Logger(MyService.name)`
|
||||
@Injectable()
|
||||
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 SMTPTransport from 'nodemailer/lib/smtp-transport';
|
||||
|
||||
import { Config } from '../config';
|
||||
import { AFFiNELogger } from '../logger';
|
||||
|
||||
export const MAILER_SERVICE = Symbol('MAILER_SERVICE');
|
||||
|
||||
@@ -15,9 +14,9 @@ export const MAILER: FactoryProvider<
|
||||
Transporter<SMTPTransport.SentMessageInfo> | undefined
|
||||
> = {
|
||||
provide: MAILER_SERVICE,
|
||||
useFactory: (config: Config, logger: AFFiNELogger) => {
|
||||
useFactory: (config: Config) => {
|
||||
if (config.mailer) {
|
||||
logger.setContext('Mailer');
|
||||
const logger = new Logger('Mailer');
|
||||
const auth = config.mailer.auth;
|
||||
if (auth && auth.user && !('pass' in auth)) {
|
||||
logger.warn(
|
||||
@@ -30,5 +29,5 @@ export const MAILER: FactoryProvider<
|
||||
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 { PrismaClient } from '@prisma/client';
|
||||
|
||||
import {
|
||||
AFFiNELogger,
|
||||
CallMetric,
|
||||
Config,
|
||||
type EventPayload,
|
||||
@@ -15,16 +14,14 @@ import { PgWorkspaceDocStorageAdapter } from './adapters/workspace';
|
||||
@Injectable()
|
||||
export class DocStorageCronJob implements OnModuleInit {
|
||||
private busy = false;
|
||||
private readonly logger = new Logger(DocStorageCronJob.name);
|
||||
|
||||
constructor(
|
||||
private readonly config: Config,
|
||||
private readonly db: PrismaClient,
|
||||
private readonly workspace: PgWorkspaceDocStorageAdapter,
|
||||
private readonly logger: AFFiNELogger,
|
||||
@Optional() private readonly registry?: SchedulerRegistry
|
||||
) {
|
||||
this.logger.setContext(DocStorageCronJob.name);
|
||||
}
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
if (this.registry && this.config.doc.manager.enableUpdateAutoMerging) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { AFFiNELogger as LoggerProvide } from '../../../base/logger';
|
||||
@@ -20,7 +20,4 @@ export const loggerProvider: Provider<LoggerService> = {
|
||||
});
|
||||
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