refactor(backend): refactor logger

This commit is contained in:
syuilo 2022-09-18 23:07:41 +09:00
parent 1cb85c5c76
commit dd35f2cce6
31 changed files with 202 additions and 75 deletions

View File

@ -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,

View File

@ -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<void> {

View File

@ -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) {

View File

@ -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<void> {
@ -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<NodeInfo> {
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<DOMWindow['document']> {
logger.info(`Fetching HTML of ${instance.host} ...`);
this.#logger.info(`Fetching HTML of ${instance.host} ...`);
const url = 'https://' + instance.host;

View File

@ -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);
}
}

View File

@ -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');
}
}

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<typeof schema> {
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<Partial<KVs<typeof schema>>> {

View File

@ -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<S extends Schema>(schema: S): ToJsonSchema<Unflatt
*/
// eslint-disable-next-line import/no-default-export
export default abstract class Chart<T extends Schema> {
#logger: Logger;
public schema: T;
private name: string;
@ -231,10 +231,18 @@ export default abstract class Chart<T extends Schema> {
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<T extends Schema> {
// 初期ログデータを作成
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<T extends Schema> {
...columns,
}).then(x => repository.findOneByOrFail(x.identifiers[0])) as RawRecord<T>;
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<T extends Schema> {
public async save(): Promise<void> {
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<T extends Schema> {
.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));

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -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,7 +43,17 @@ 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() {
@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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');
}
}

View File

@ -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<string> }, 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');

View File

@ -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 {