From dd35f2cce6789ce8a0cdf15f0db86506efb07a8f Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 18 Sep 2022 23:07:41 +0900 Subject: [PATCH] refactor(backend): refactor logger --- packages/backend/src/core/CoreModule.ts | 10 ++++++ packages/backend/src/core/DownloadService.ts | 6 ++-- packages/backend/src/core/EmailService.ts | 6 ++-- .../src/core/FetchInstanceMetadataService.ts | 25 +++++++------- packages/backend/src/core/LoggerService.ts | 33 +++++++++++++++++++ .../src/core/chart/ChartLoggerService.ts | 14 ++++++++ .../src/core/chart/charts/active-users.ts | 4 ++- .../src/core/chart/charts/ap-request.ts | 4 ++- .../backend/src/core/chart/charts/drive.ts | 4 ++- .../src/core/chart/charts/federation.ts | 4 ++- .../backend/src/core/chart/charts/hashtag.ts | 4 ++- .../backend/src/core/chart/charts/instance.ts | 4 ++- .../backend/src/core/chart/charts/notes.ts | 4 ++- .../src/core/chart/charts/per-user-drive.ts | 4 ++- .../core/chart/charts/per-user-following.ts | 4 ++- .../src/core/chart/charts/per-user-notes.ts | 4 ++- .../core/chart/charts/per-user-reactions.ts | 4 ++- .../src/core/chart/charts/test-grouped.ts | 4 ++- .../core/chart/charts/test-intersection.ts | 4 ++- .../src/core/chart/charts/test-unique.ts | 4 ++- .../backend/src/core/chart/charts/test.ts | 4 ++- .../backend/src/core/chart/charts/users.ts | 4 ++- packages/backend/src/core/chart/core.ts | 24 +++++++++----- .../src/core/remote/RemoteLoggerService.ts | 6 ++-- .../backend/src/queue/QueueLoggerService.ts | 6 ++-- .../backend/src/server/FileServerService.ts | 25 ++++++++------ .../src/server/MediaProxyServerService.ts | 11 ++++--- packages/backend/src/server/ServerService.ts | 21 +++++++----- .../src/server/api/ApiLoggerService.ts | 6 ++-- .../src/server/api/RateLimiterService.ts | 14 +++++--- .../src/server/web/UrlPreviewService.ts | 6 ++-- 31 files changed, 202 insertions(+), 75 deletions(-) create mode 100644 packages/backend/src/core/LoggerService.ts create mode 100644 packages/backend/src/core/chart/ChartLoggerService.ts diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index 523362818..da07728d2 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -50,6 +50,7 @@ import { WebhookService } from './WebhookService.js'; import { ProxyAccountService } from './ProxyAccountService.js'; import { UtilityService } from './UtilityService.js'; import { FileInfoService } from './FileInfoService.js'; +import { ChartLoggerService } from './chart/ChartLoggerService.js'; import FederationChart from './chart/charts/federation.js'; import NotesChart from './chart/charts/notes.js'; import UsersChart from './chart/charts/users.js'; @@ -113,9 +114,11 @@ import { ApPersonService } from './remote/activitypub/models/ApPersonService.js' import { ApQuestionService } from './remote/activitypub/models/ApQuestionService.js'; import { QueueModule } from './queue/QueueModule.js'; import { QueueService } from './QueueService.js'; +import { LoggerService } from './LoggerService.js'; import type { Provider } from '@nestjs/common'; //#region 文字列ベースでのinjection用(循環参照対応のため) +const $LoggerService: Provider = { provide: 'LoggerService', useExisting: LoggerService }; const $AccountUpdateService: Provider = { provide: 'AccountUpdateService', useExisting: AccountUpdateService }; const $AiService: Provider = { provide: 'AiService', useExisting: AiService }; const $AntennaService: Provider = { provide: 'AntennaService', useExisting: AntennaService }; @@ -166,6 +169,7 @@ const $VideoProcessingService: Provider = { provide: 'VideoProcessingService', u const $WebhookService: Provider = { provide: 'WebhookService', useExisting: WebhookService }; const $UtilityService: Provider = { provide: 'UtilityService', useExisting: UtilityService }; const $FileInfoService: Provider = { provide: 'FileInfoService', useExisting: FileInfoService }; +const $ChartLoggerService: Provider = { provide: 'ChartLoggerService', useExisting: ChartLoggerService }; const $FederationChart: Provider = { provide: 'FederationChart', useExisting: FederationChart }; const $NotesChart: Provider = { provide: 'NotesChart', useExisting: NotesChart }; const $UsersChart: Provider = { provide: 'UsersChart', useExisting: UsersChart }; @@ -236,6 +240,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting QueueModule, ], providers: [ + LoggerService, AccountUpdateService, AiService, AntennaService, @@ -286,6 +291,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting WebhookService, UtilityService, FileInfoService, + ChartLoggerService, FederationChart, NotesChart, UsersChart, @@ -350,6 +356,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting QueueService, //#region 文字列ベースでのinjection用(循環参照対応のため) + $LoggerService, $AccountUpdateService, $AiService, $AntennaService, @@ -400,6 +407,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $WebhookService, $UtilityService, $FileInfoService, + $ChartLoggerService, $FederationChart, $NotesChart, $UsersChart, @@ -465,6 +473,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting ], exports: [ QueueModule, + LoggerService, AccountUpdateService, AiService, AntennaService, @@ -579,6 +588,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting QueueService, //#region 文字列ベースでのinjection用(循環参照対応のため) + $LoggerService, $AccountUpdateService, $AiService, $AntennaService, diff --git a/packages/backend/src/core/DownloadService.ts b/packages/backend/src/core/DownloadService.ts index 84d5ca2e8..9c5c45ed2 100644 --- a/packages/backend/src/core/DownloadService.ts +++ b/packages/backend/src/core/DownloadService.ts @@ -8,10 +8,11 @@ import got, * as Got from 'got'; import chalk from 'chalk'; import { DI } from '@/di-symbols.js'; import { Config } from '@/config.js'; -import Logger from '@/logger.js'; import { HttpRequestService } from '@/core/HttpRequestService.js'; import { createTemp } from '@/misc/create-temp.js'; import { StatusError } from '@/misc/status-error.js'; +import { LoggerService } from '@/core/LoggerService.js'; +import type Logger from '@/logger.js'; const pipeline = util.promisify(stream.pipeline); @@ -24,8 +25,9 @@ export class DownloadService { private config: Config, private httpRequestService: HttpRequestService, + private loggerService: LoggerService, ) { - this.#logger = new Logger('download'); + this.#logger = this.loggerService.getLogger('download'); } public async downloadUrl(url: string, path: string): Promise { diff --git a/packages/backend/src/core/EmailService.ts b/packages/backend/src/core/EmailService.ts index 7d6960b73..a593922ac 100644 --- a/packages/backend/src/core/EmailService.ts +++ b/packages/backend/src/core/EmailService.ts @@ -4,8 +4,9 @@ import { validate as validateEmail } from 'deep-email-validator'; import { MetaService } from '@/core/MetaService.js'; import { DI } from '@/di-symbols.js'; import { Config } from '@/config.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { UserProfilesRepository } from '@/models/index.js'; +import { LoggerService } from '@/core/LoggerService.js'; @Injectable() export class EmailService { @@ -19,8 +20,9 @@ export class EmailService { private userProfilesRepository: UserProfilesRepository, private metaService: MetaService, + private loggerService: LoggerService, ) { - this.#logger = new Logger('email'); + this.#logger = this.loggerService.getLogger('email'); } public async sendEmail(to: string, subject: string, html: string, text: string) { diff --git a/packages/backend/src/core/FetchInstanceMetadataService.ts b/packages/backend/src/core/FetchInstanceMetadataService.ts index 6353784c1..4414d8394 100644 --- a/packages/backend/src/core/FetchInstanceMetadataService.ts +++ b/packages/backend/src/core/FetchInstanceMetadataService.ts @@ -6,13 +6,12 @@ import tinycolor from 'tinycolor2'; import type { Instance } from '@/models/entities/Instance.js'; import { InstancesRepository } from '@/models/index.js'; import { AppLockService } from '@/core/AppLockService.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { DI } from '@/di-symbols.js'; +import { LoggerService } from '@/core/LoggerService.js'; import { HttpRequestService } from './HttpRequestService.js'; import type { DOMWindow } from 'jsdom'; -const logger = new Logger('metadata', 'cyan'); - type NodeInfo = { openRegistrations?: any; software?: { @@ -33,13 +32,17 @@ type NodeInfo = { @Injectable() export class FetchInstanceMetadataService { + #logger: Logger; + constructor( @Inject(DI.instancesRepository) private instancesRepository: InstancesRepository, private appLockService: AppLockService, private httpRequestService: HttpRequestService, + private loggerService: LoggerService, ) { + this.#logger = this.loggerService.getLogger('metadata', 'cyan'); } public async fetchInstanceMetadata(instance: Instance, force = false): Promise { @@ -54,7 +57,7 @@ export class FetchInstanceMetadataService { } } - logger.info(`Fetching metadata of ${instance.host} ...`); + this.#logger.info(`Fetching metadata of ${instance.host} ...`); try { const [info, dom, manifest] = await Promise.all([ @@ -71,7 +74,7 @@ export class FetchInstanceMetadataService { this.#getDescription(info, dom, manifest).catch(() => null), ]); - logger.succ(`Successfuly fetched metadata of ${instance.host}`); + this.#logger.succ(`Successfuly fetched metadata of ${instance.host}`); const updates = { infoUpdatedAt: new Date(), @@ -93,16 +96,16 @@ export class FetchInstanceMetadataService { await this.instancesRepository.update(instance.id, updates); - logger.succ(`Successfuly updated metadata of ${instance.host}`); + this.#logger.succ(`Successfuly updated metadata of ${instance.host}`); } catch (e) { - logger.error(`Failed to update metadata of ${instance.host}: ${e}`); + this.#logger.error(`Failed to update metadata of ${instance.host}: ${e}`); } finally { unlock(); } } async #fetchNodeinfo(instance: Instance): Promise { - logger.info(`Fetching nodeinfo of ${instance.host} ...`); + this.#logger.info(`Fetching nodeinfo of ${instance.host} ...`); try { const wellknown = await this.httpRequestService.getJson('https://' + instance.host + '/.well-known/nodeinfo') @@ -134,18 +137,18 @@ export class FetchInstanceMetadataService { throw err.statusCode ?? err.message; }); - logger.succ(`Successfuly fetched nodeinfo of ${instance.host}`); + this.#logger.succ(`Successfuly fetched nodeinfo of ${instance.host}`); return info as NodeInfo; } catch (err) { - logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${err}`); + this.#logger.error(`Failed to fetch nodeinfo of ${instance.host}: ${err}`); throw err; } } async #fetchDom(instance: Instance): Promise { - logger.info(`Fetching HTML of ${instance.host} ...`); + this.#logger.info(`Fetching HTML of ${instance.host} ...`); const url = 'https://' + instance.host; diff --git a/packages/backend/src/core/LoggerService.ts b/packages/backend/src/core/LoggerService.ts new file mode 100644 index 000000000..d844b3884 --- /dev/null +++ b/packages/backend/src/core/LoggerService.ts @@ -0,0 +1,33 @@ +import { Inject, Injectable } from '@nestjs/common'; +import * as SyslogPro from 'syslog-pro'; +import { DI } from '@/di-symbols.js'; +import { Config } from '@/config.js'; +import Logger from '@/logger.js'; + +@Injectable() +export class LoggerService { + #syslogClient; + + constructor( + @Inject(DI.config) + private config: Config, + ) { + if (this.config.syslog) { + this.#syslogClient = new SyslogPro.RFC5424({ + applacationName: 'Misskey', + timestamp: true, + encludeStructuredData: true, + color: true, + extendedColor: true, + server: { + target: config.syslog.host, + port: config.syslog.port, + }, + }); + } + } + + public getLogger(domain: string, color?: string | undefined, store?: boolean) { + return new Logger(domain, color, store, this.#syslogClient); + } +} diff --git a/packages/backend/src/core/chart/ChartLoggerService.ts b/packages/backend/src/core/chart/ChartLoggerService.ts new file mode 100644 index 000000000..544a006ac --- /dev/null +++ b/packages/backend/src/core/chart/ChartLoggerService.ts @@ -0,0 +1,14 @@ +import { Inject, Injectable } from '@nestjs/common'; +import type Logger from '@/logger.js'; +import { LoggerService } from '@/core/LoggerService.js'; + +@Injectable() +export class ChartLoggerService { + public logger: Logger; + + constructor( + private loggerService: LoggerService, + ) { + this.logger = this.loggerService.getLogger('chart', 'white', process.env.NODE_ENV !== 'test'); + } +} diff --git a/packages/backend/src/core/chart/charts/active-users.ts b/packages/backend/src/core/chart/charts/active-users.ts index a5d9f166e..40c60910e 100644 --- a/packages/backend/src/core/chart/charts/active-users.ts +++ b/packages/backend/src/core/chart/charts/active-users.ts @@ -4,6 +4,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import type { User } from '@/models/entities/User.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/active-users.js'; import type { KVs } from '../core.js'; @@ -22,8 +23,9 @@ export default class ActiveUsersChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/ap-request.ts b/packages/backend/src/core/chart/charts/ap-request.ts index c857cea98..4b91fbbf1 100644 --- a/packages/backend/src/core/chart/charts/ap-request.ts +++ b/packages/backend/src/core/chart/charts/ap-request.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/ap-request.js'; import type { KVs } from '../core.js'; @@ -17,8 +18,9 @@ export default class ApRequestChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/drive.ts b/packages/backend/src/core/chart/charts/drive.ts index dd6d00203..494dfbbe5 100644 --- a/packages/backend/src/core/chart/charts/drive.ts +++ b/packages/backend/src/core/chart/charts/drive.ts @@ -4,6 +4,7 @@ import type { DriveFile } from '@/models/entities/DriveFile.js'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/drive.js'; import type { KVs } from '../core.js'; @@ -18,8 +19,9 @@ export default class DriveChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/federation.ts b/packages/backend/src/core/chart/charts/federation.ts index 372e0f1fa..4366d4cce 100644 --- a/packages/backend/src/core/chart/charts/federation.ts +++ b/packages/backend/src/core/chart/charts/federation.ts @@ -5,6 +5,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { MetaService } from '@/core/MetaService.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/federation.js'; import type { KVs } from '../core.js'; @@ -26,8 +27,9 @@ export default class FederationChart extends Chart { private metaService: MetaService, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/hashtag.ts b/packages/backend/src/core/chart/charts/hashtag.ts index 66ac0b882..8b8c795cf 100644 --- a/packages/backend/src/core/chart/charts/hashtag.ts +++ b/packages/backend/src/core/chart/charts/hashtag.ts @@ -5,6 +5,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/hashtag.js'; import type { KVs } from '../core.js'; @@ -20,8 +21,9 @@ export default class HashtagChart extends Chart { private appLockService: AppLockService, private userEntityService: UserEntityService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/instance.ts b/packages/backend/src/core/chart/charts/instance.ts index c43ebeddc..be70bc79c 100644 --- a/packages/backend/src/core/chart/charts/instance.ts +++ b/packages/backend/src/core/chart/charts/instance.ts @@ -7,6 +7,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { UtilityService } from '@/core/UtilityService.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/instance.js'; import type { KVs } from '../core.js'; @@ -34,8 +35,9 @@ export default class InstanceChart extends Chart { private utilityService: UtilityService, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/notes.ts b/packages/backend/src/core/chart/charts/notes.ts index 1597b5727..e1bfeabf9 100644 --- a/packages/backend/src/core/chart/charts/notes.ts +++ b/packages/backend/src/core/chart/charts/notes.ts @@ -5,6 +5,7 @@ import type { Note } from '@/models/entities/Note.js'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/notes.js'; import type { KVs } from '../core.js'; @@ -22,8 +23,9 @@ export default class NotesChart extends Chart { private notesRepository: NotesRepository, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/per-user-drive.ts b/packages/backend/src/core/chart/charts/per-user-drive.ts index 181b9a38b..752203daa 100644 --- a/packages/backend/src/core/chart/charts/per-user-drive.ts +++ b/packages/backend/src/core/chart/charts/per-user-drive.ts @@ -6,6 +6,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/per-user-drive.js'; import type { KVs } from '../core.js'; @@ -24,8 +25,9 @@ export default class PerUserDriveChart extends Chart { private appLockService: AppLockService, private driveFileEntityService: DriveFileEntityService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/per-user-following.ts b/packages/backend/src/core/chart/charts/per-user-following.ts index 5195723a2..48bf3d7c6 100644 --- a/packages/backend/src/core/chart/charts/per-user-following.ts +++ b/packages/backend/src/core/chart/charts/per-user-following.ts @@ -6,6 +6,7 @@ import { DI } from '@/di-symbols.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { FollowingsRepository } from '@/models/index.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/per-user-following.js'; import type { KVs } from '../core.js'; @@ -24,8 +25,9 @@ export default class PerUserFollowingChart extends Chart { private appLockService: AppLockService, private userEntityService: UserEntityService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/per-user-notes.ts b/packages/backend/src/core/chart/charts/per-user-notes.ts index 6dbe309b7..ffe52dcd5 100644 --- a/packages/backend/src/core/chart/charts/per-user-notes.ts +++ b/packages/backend/src/core/chart/charts/per-user-notes.ts @@ -6,6 +6,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { NotesRepository } from '@/models/index.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/per-user-notes.js'; import type { KVs } from '../core.js'; @@ -23,8 +24,9 @@ export default class PerUserNotesChart extends Chart { private notesRepository: NotesRepository, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/per-user-reactions.ts b/packages/backend/src/core/chart/charts/per-user-reactions.ts index 73a58656f..416021972 100644 --- a/packages/backend/src/core/chart/charts/per-user-reactions.ts +++ b/packages/backend/src/core/chart/charts/per-user-reactions.ts @@ -6,6 +6,7 @@ import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/per-user-reactions.js'; import type { KVs } from '../core.js'; @@ -21,8 +22,9 @@ export default class PerUserReactionsChart extends Chart { private appLockService: AppLockService, private userEntityService: UserEntityService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/test-grouped.ts b/packages/backend/src/core/chart/charts/test-grouped.ts index e6cbe8979..103cf2a27 100644 --- a/packages/backend/src/core/chart/charts/test-grouped.ts +++ b/packages/backend/src/core/chart/charts/test-grouped.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/test-grouped.js'; import type { KVs } from '../core.js'; @@ -19,8 +20,9 @@ export default class TestGroupedChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema, true); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema, true); } protected async tickMajor(group: string): Promise>> { diff --git a/packages/backend/src/core/chart/charts/test-intersection.ts b/packages/backend/src/core/chart/charts/test-intersection.ts index f2f17c8de..0e3876916 100644 --- a/packages/backend/src/core/chart/charts/test-intersection.ts +++ b/packages/backend/src/core/chart/charts/test-intersection.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/test-intersection.js'; import type { KVs } from '../core.js'; @@ -17,8 +18,9 @@ export default class TestIntersectionChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/test-unique.ts b/packages/backend/src/core/chart/charts/test-unique.ts index ce0159452..44e1b8365 100644 --- a/packages/backend/src/core/chart/charts/test-unique.ts +++ b/packages/backend/src/core/chart/charts/test-unique.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/test-unique.js'; import type { KVs } from '../core.js'; @@ -17,8 +18,9 @@ export default class TestUniqueChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/test.ts b/packages/backend/src/core/chart/charts/test.ts index bd59b7aa6..626967402 100644 --- a/packages/backend/src/core/chart/charts/test.ts +++ b/packages/backend/src/core/chart/charts/test.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { AppLockService } from '@/core/AppLockService.js'; import { DI } from '@/di-symbols.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/test.js'; import type { KVs } from '../core.js'; @@ -19,8 +20,9 @@ export default class TestChart extends Chart { private db: DataSource, private appLockService: AppLockService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/charts/users.ts b/packages/backend/src/core/chart/charts/users.ts index 4fdddcc0a..b3187997c 100644 --- a/packages/backend/src/core/chart/charts/users.ts +++ b/packages/backend/src/core/chart/charts/users.ts @@ -6,6 +6,7 @@ import { DI } from '@/di-symbols.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UsersRepository } from '@/models/index.js'; import Chart from '../core.js'; +import { ChartLoggerService } from '../ChartLoggerService.js'; import { name, schema } from './entities/users.js'; import type { KVs } from '../core.js'; @@ -24,8 +25,9 @@ export default class UsersChart extends Chart { private appLockService: AppLockService, private userEntityService: UserEntityService, + private chartLoggerService: ChartLoggerService, ) { - super(db, (k) => appLockService.getChartInsertLock(k), name, schema); + super(db, (k) => appLockService.getChartInsertLock(k), chartLoggerService.logger, name, schema); } protected async tickMajor(): Promise>> { diff --git a/packages/backend/src/core/chart/core.ts b/packages/backend/src/core/chart/core.ts index 1933e80c7..93a6c3ce0 100644 --- a/packages/backend/src/core/chart/core.ts +++ b/packages/backend/src/core/chart/core.ts @@ -7,11 +7,9 @@ import * as nestedProperty from 'nested-property'; import { EntitySchema, LessThan, Between } from 'typeorm'; import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/misc/prelude/time.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import type { Repository, DataSource } from 'typeorm'; -const logger = new Logger('chart', 'white', process.env.NODE_ENV !== 'test'); - const columnPrefix = '___' as const; const uniqueTempColumnPrefix = 'unique_temp___' as const; const columnDot = '_' as const; @@ -111,6 +109,8 @@ export function getJsonSchema(schema: S): ToJsonSchema { + #logger: Logger; + public schema: T; private name: string; @@ -231,10 +231,18 @@ export default abstract class Chart { private lock: (key: string) => Promise<() => void>; - constructor(db: DataSource, lock: (key: string) => Promise<() => void>, name: string, schema: T, grouped = false) { + constructor( + db: DataSource, + lock: (key: string) => Promise<() => void>, + logger: Logger, + name: string, + schema: T, + grouped = false, + ) { this.name = name; this.schema = schema; this.lock = lock; + this.#logger = logger; const { hour, day } = Chart.schemaToEntity(name, schema, grouped); this.repositoryForHour = db.getRepository<{ id: number; group?: string | null; date: number; }>(hour); @@ -325,7 +333,7 @@ export default abstract class Chart { // 初期ログデータを作成 data = this.getNewLog(null); - logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): Initial commit created`); + this.#logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): Initial commit created`); } const date = Chart.dateToTimestamp(current); @@ -355,7 +363,7 @@ export default abstract class Chart { ...columns, }).then(x => repository.findOneByOrFail(x.identifiers[0])) as RawRecord; - logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): New commit created`); + this.#logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): New commit created`); return log; } finally { @@ -374,7 +382,7 @@ export default abstract class Chart { public async save(): Promise { if (this.buffer.length === 0) { - logger.info(`${this.name}: Write skipped`); + this.#logger.info(`${this.name}: Write skipped`); return; } @@ -473,7 +481,7 @@ export default abstract class Chart { .execute(), ]); - logger.info(`${this.name + (logHour.group ? `:${logHour.group}` : '')}: Updated`); + this.#logger.info(`${this.name + (logHour.group ? `:${logHour.group}` : '')}: Updated`); // TODO: この一連の処理が始まった後に新たにbufferに入ったものは消さないようにする this.buffer = this.buffer.filter(q => q.group != null && (q.group !== logHour.group)); diff --git a/packages/backend/src/core/remote/RemoteLoggerService.ts b/packages/backend/src/core/remote/RemoteLoggerService.ts index 7ce8fe6cf..68246466c 100644 --- a/packages/backend/src/core/remote/RemoteLoggerService.ts +++ b/packages/backend/src/core/remote/RemoteLoggerService.ts @@ -1,12 +1,14 @@ import { Inject, Injectable } from '@nestjs/common'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; +import { LoggerService } from '@/core/LoggerService.js'; @Injectable() export class RemoteLoggerService { public logger: Logger; constructor( + private loggerService: LoggerService, ) { - this.logger = new Logger('remote', 'cyan'); + this.logger = this.loggerService.getLogger('remote', 'cyan'); } } diff --git a/packages/backend/src/queue/QueueLoggerService.ts b/packages/backend/src/queue/QueueLoggerService.ts index 4cdd4edfb..a311470cc 100644 --- a/packages/backend/src/queue/QueueLoggerService.ts +++ b/packages/backend/src/queue/QueueLoggerService.ts @@ -1,12 +1,14 @@ import { Inject, Injectable } from '@nestjs/common'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; +import { LoggerService } from '@/core/LoggerService.js'; @Injectable() export class QueueLoggerService { public logger: Logger; constructor( + private loggerService: LoggerService, ) { - this.logger = new Logger('queue', 'orange'); + this.logger = this.loggerService.getLogger('queue', 'orange'); } } diff --git a/packages/backend/src/server/FileServerService.ts b/packages/backend/src/server/FileServerService.ts index 0f4246bfd..4db9205e3 100644 --- a/packages/backend/src/server/FileServerService.ts +++ b/packages/backend/src/server/FileServerService.ts @@ -13,29 +13,24 @@ import { DI } from '@/di-symbols.js'; import { createTemp } from '@/misc/create-temp.js'; import { FILE_TYPE_BROWSERSAFE } from '@/const.js'; import { StatusError } from '@/misc/status-error.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { DownloadService } from '@/core/DownloadService.js'; import { ImageProcessingService } from '@/core/ImageProcessingService.js'; import { VideoProcessingService } from '@/core/VideoProcessingService.js'; import { InternalStorageService } from '@/core/InternalStorageService.js'; import { contentDisposition } from '@/misc/content-disposition.js'; import { FileInfoService } from '@/core/FileInfoService.js'; - -const serverLogger = new Logger('server', 'gray', false); +import { LoggerService } from '@/core/LoggerService.js'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); const assets = `${_dirname}/../../server/file/assets/`; -const commonReadableHandlerGenerator = (ctx: Koa.Context) => (e: Error): void => { - serverLogger.error(e); - ctx.status = 500; - ctx.set('Cache-Control', 'max-age=300'); -}; - @Injectable() export class FileServerService { + #logger: Logger; + constructor( @Inject(DI.config) private config: Config, @@ -48,9 +43,19 @@ export class FileServerService { private imageProcessingService: ImageProcessingService, private videoProcessingService: VideoProcessingService, private internalStorageService: InternalStorageService, + private loggerService: LoggerService, ) { + this.#logger = this.loggerService.getLogger('server', 'gray', false); } + public commonReadableHandlerGenerator(ctx: Koa.Context) { + return (e: Error): void => { + this.#logger.error(e); + ctx.status = 500; + ctx.set('Cache-Control', 'max-age=300'); + }; + } + public createServer() { const app = new Koa(); app.use(cors()); @@ -134,7 +139,7 @@ export class FileServerService { ctx.set('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream'); ctx.set('Cache-Control', 'max-age=31536000, immutable'); } catch (err) { - serverLogger.error(`${err}`); + this.#logger.error(`${err}`); if (err instanceof StatusError && err.isClientError) { ctx.status = err.statusCode; diff --git a/packages/backend/src/server/MediaProxyServerService.ts b/packages/backend/src/server/MediaProxyServerService.ts index b9cee38f0..5344d3a9f 100644 --- a/packages/backend/src/server/MediaProxyServerService.ts +++ b/packages/backend/src/server/MediaProxyServerService.ts @@ -13,13 +13,14 @@ import { ImageProcessingService } from '@/core/ImageProcessingService.js'; import type { IImage } from '@/core/ImageProcessingService.js'; import { FILE_TYPE_BROWSERSAFE } from '@/const.js'; import { StatusError } from '@/misc/status-error.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { FileInfoService } from '@/core/FileInfoService.js'; - -const serverLogger = new Logger('server', 'gray', false); +import { LoggerService } from '@/core/LoggerService.js'; @Injectable() export class MediaProxyServerService { + #logger: Logger; + constructor( @Inject(DI.config) private config: Config, @@ -27,7 +28,9 @@ export class MediaProxyServerService { private fileInfoService: FileInfoService, private downloadService: DownloadService, private imageProcessingService: ImageProcessingService, + private loggerService: LoggerService, ) { + this.#logger = this.loggerService.getLogger('server', 'gray', false); } public createServer() { @@ -123,7 +126,7 @@ export class MediaProxyServerService { ctx.set('Cache-Control', 'max-age=31536000, immutable'); ctx.body = image.data; } catch (err) { - serverLogger.error(`${err}`); + this.#logger.error(`${err}`); if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) { ctx.status = err.statusCode; diff --git a/packages/backend/src/server/ServerService.ts b/packages/backend/src/server/ServerService.ts index 2ee7135c2..8349e1e9f 100644 --- a/packages/backend/src/server/ServerService.ts +++ b/packages/backend/src/server/ServerService.ts @@ -12,12 +12,13 @@ import { GlobalEventService } from '@/core/GlobalEventService.js'; import { Config } from '@/config.js'; import { UserProfilesRepository, UsersRepository } from '@/models/index.js'; import { DI } from '@/di-symbols.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { envOption } from '@/env.js'; import * as Acct from '@/misc/acct.js'; import { genIdenticon } from '@/misc/gen-identicon.js'; import { createTemp } from '@/misc/create-temp.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; +import { LoggerService } from '@/core/LoggerService.js'; import { ActivityPubServerService } from './ActivityPubServerService.js'; import { NodeinfoServerService } from './NodeinfoServerService.js'; import { ApiServerService } from './api/ApiServerService.js'; @@ -27,10 +28,10 @@ import { MediaProxyServerService } from './MediaProxyServerService.js'; import { FileServerService } from './FileServerService.js'; import { ClientServerService } from './web/ClientServerService.js'; -const serverLogger = new Logger('server', 'gray', false); - @Injectable() export class ServerService { + #logger: Logger; + constructor( @Inject(DI.config) private config: Config, @@ -51,7 +52,9 @@ export class ServerService { private mediaProxyServerService: MediaProxyServerService, private clientServerService: ClientServerService, private globalEventService: GlobalEventService, + private loggerService: LoggerService, ) { + this.#logger = this.loggerService.getLogger('server', 'gray', false); } public launch() { @@ -62,7 +65,7 @@ export class ServerService { if (!['production', 'test'].includes(process.env.NODE_ENV ?? '')) { // Logger koa.use(koaLogger(str => { - serverLogger.info(str); + this.#logger.info(str); })); // Delay @@ -151,16 +154,16 @@ export class ServerService { this.streamingApiServerService.attachStreamingApi(server); - server.on('error', e => { - switch ((e as any).code) { + server.on('error', err => { + switch ((err as any).code) { case 'EACCES': - serverLogger.error(`You do not have permission to listen on port ${this.config.port}.`); + this.#logger.error(`You do not have permission to listen on port ${this.config.port}.`); break; case 'EADDRINUSE': - serverLogger.error(`Port ${this.config.port} is already in use by another process.`); + this.#logger.error(`Port ${this.config.port} is already in use by another process.`); break; default: - serverLogger.error(e); + this.#logger.error(err); break; } diff --git a/packages/backend/src/server/api/ApiLoggerService.ts b/packages/backend/src/server/api/ApiLoggerService.ts index c8c5fec85..c4fb25036 100644 --- a/packages/backend/src/server/api/ApiLoggerService.ts +++ b/packages/backend/src/server/api/ApiLoggerService.ts @@ -1,12 +1,14 @@ import { Inject, Injectable } from '@nestjs/common'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; +import { LoggerService } from '@/core/LoggerService.js'; @Injectable() export class ApiLoggerService { public logger: Logger; constructor( + private loggerService: LoggerService, ) { - this.logger = new Logger('api'); + this.logger = this.loggerService.getLogger('api'); } } diff --git a/packages/backend/src/server/api/RateLimiterService.ts b/packages/backend/src/server/api/RateLimiterService.ts index d390a47b8..61b7b1ff4 100644 --- a/packages/backend/src/server/api/RateLimiterService.ts +++ b/packages/backend/src/server/api/RateLimiterService.ts @@ -2,17 +2,21 @@ import { Inject, Injectable } from '@nestjs/common'; import Limiter from 'ratelimiter'; import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; +import { LoggerService } from '@/core/LoggerService.js'; import type { IEndpointMeta } from './endpoints.js'; -const logger = new Logger('limiter'); - @Injectable() export class RateLimiterService { + #logger: Logger; + constructor( @Inject(DI.redis) private redisClient: Redis.Redis, + + private loggerService: LoggerService, ) { + this.#logger = this.loggerService.getLogger('limiter'); } public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable }, actor: string) { @@ -33,7 +37,7 @@ export class RateLimiterService { return reject('ERR'); } - logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`); + this.#logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`); if (info.remaining === 0) { reject('BRIEF_REQUEST_INTERVAL'); @@ -61,7 +65,7 @@ export class RateLimiterService { return reject('ERR'); } - logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`); + this.#logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`); if (info.remaining === 0) { reject('RATE_LIMIT_EXCEEDED'); diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts index 4e3b45614..726b15834 100644 --- a/packages/backend/src/server/web/UrlPreviewService.ts +++ b/packages/backend/src/server/web/UrlPreviewService.ts @@ -5,8 +5,9 @@ import { UsersRepository } from '@/models/index.js'; import { Config } from '@/config.js'; import { MetaService } from '@/core/MetaService.js'; import { HttpRequestService } from '@/core/HttpRequestService.js'; -import Logger from '@/logger.js'; +import type Logger from '@/logger.js'; import { query } from '@/misc/prelude/url.js'; +import { LoggerService } from '@/core/LoggerService.js'; import type Koa from 'koa'; @Injectable() @@ -22,8 +23,9 @@ export class UrlPreviewService { private metaService: MetaService, private httpRequestService: HttpRequestService, + private loggerService: LoggerService, ) { - this.#logger = new Logger('url-preview'); + this.#logger = this.loggerService.getLogger('url-preview'); } #wrap(url?: string): string | null {