refactor(backend): refactor logger
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
| 			} | ||||
|  | ||||
|   | ||||
| @@ -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'); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -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'); | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo