diff --git a/express/eslint.config.js b/express/eslint.config.js index c067f1e3..81638ddd 100644 --- a/express/eslint.config.js +++ b/express/eslint.config.js @@ -11,8 +11,6 @@ export default [ { files: ['src/app.ts'], rules: { - 'no-console': 'off', - '@typescript-eslint/no-misused-promises': 'off', '@typescript-eslint/no-unsafe-call': 'off', // Github Actions 環境ではエラーになる }, }, diff --git a/express/package.json b/express/package.json index f1293893..6dcfe3ac 100644 --- a/express/package.json +++ b/express/package.json @@ -17,6 +17,7 @@ "dotenv": "^16.4.7", "express": "^5.0.1", "htpasswd-js": "^1.0.2", + "log4js": "^6.9.1", "matcher": "^5.0.0" }, "devDependencies": { diff --git a/express/src/app.ts b/express/src/app.ts index 1fae4729..7445bf3a 100644 --- a/express/src/app.ts +++ b/express/src/app.ts @@ -6,6 +6,7 @@ import * as dotenv from 'dotenv'; import express, { type NextFunction, type Request, type Response } from 'express'; // @ts-expect-error: ts(7016) import htpasswd from 'htpasswd-js'; +import Log4js from 'log4js'; import { isMatch } from 'matcher'; import HtmlEscape from '@w0s/html-escape'; // @ts-expect-error: ts(7016) @@ -13,11 +14,15 @@ import { handler as ssrHandler } from '@w0s.jp/astro/dist/server/entry.mjs'; import config from './config/express.js'; import { env } from './util/env.js'; -/* 設定ファイル読み込み */ dotenv.config({ path: process.env['NODE_ENV'] === 'production' ? '../.env.production' : '../.env.development', }); +/* Logger */ +Log4js.configure(env('LOGGER')); +const logger = Log4js.getLogger(); + +/* Express */ const app = express(); app.set('x-powered-by', false); @@ -153,7 +158,7 @@ app.use( .find(([fileExtension]) => fileExtension === extensionOrigin) ?.at(1); if (mimeType === undefined) { - console.warn(`MIME type is undefined: ${requestUrlOrigin}`); + logger.error(`MIME type is undefined: ${requestUrlOrigin}`); } res.setHeader('Content-Type', mimeType ?? 'application/octet-stream'); @@ -192,31 +197,25 @@ app.use( }), ); -/** - * SSR - */ +/* SSR */ app.use(async (req, res, next): Promise => { await ssrHandler(req, res, next); }); -/** - * エラー処理 - */ +/* Error pages */ app.use((req, res): void => { - console.warn(`404 Not Found: ${req.method} ${req.url}`); + logger.warn(`404 Not Found: ${req.method} ${req.url}`); res.status(404).sendFile(path.resolve(`${config.static.root}/404.html`)); }); app.use((err: Error, req: Request, res: Response, _next: NextFunction /* eslint-disable-line @typescript-eslint/no-unused-vars */): void => { - console.error(`${req.method} ${req.url}`, err.stack); + logger.fatal(`${req.method} ${req.url}`, err.stack); res.status(500).sendFile(path.resolve(`${config.static.root}/500.html`)); }); -/** - * HTTP サーバー起動 - */ +/* HTTP Server */ const port = env('PORT', 'number'); app.listen(port, () => { - console.info(`Server is running on http://localhost:${String(port)}`); + logger.info(`Server is running on http://localhost:${String(port)}`); }); diff --git a/package-lock.json b/package-lock.json index b55ba97d..497e5a3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2706,6 +2706,7 @@ "dotenv": "^16.4.7", "express": "^5.0.1", "htpasswd-js": "^1.0.2", + "log4js": "^6.9.1", "matcher": "^5.0.0" }, "devDependencies": { @@ -7737,6 +7738,7 @@ "version": "4.0.14", "resolved": "https://registry.npmjs.org/date-format/-/date-format-4.0.14.tgz", "integrity": "sha512-39BOQLs9ZjKh0/patS9nrT8wc3ioX3/eA/zgbKNopnF2wCqJEoxywwwElATYvRsXdnOxA/OQeQoFZ3rFjVajhg==", + "license": "MIT", "engines": { "node": ">=4.0" } @@ -11408,6 +11410,7 @@ "version": "6.9.1", "resolved": "https://registry.npmjs.org/log4js/-/log4js-6.9.1.tgz", "integrity": "sha512-1somDdy9sChrr9/f4UlzhdaGfDR2c/SaD2a4T7qEkG4jTS57/B3qmnjLYePwQ8cqWnUHZI0iAKxMBpCZICiZ2g==", + "license": "Apache-2.0", "dependencies": { "date-format": "^4.0.14", "debug": "^4.3.4", @@ -14648,7 +14651,8 @@ "node_modules/rfdc": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", - "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==" + "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==", + "license": "MIT" }, "node_modules/rimraf": { "version": "3.0.2", @@ -15447,6 +15451,7 @@ "version": "3.1.5", "resolved": "https://registry.npmjs.org/streamroller/-/streamroller-3.1.5.tgz", "integrity": "sha512-KFxaM7XT+irxvdqSP1LGLgNWbYN7ay5owZ3r/8t77p+EtSUAfUgtl7be3xtqtOmGUl9K9YPO2ca8133RlTjvKw==", + "license": "MIT", "dependencies": { "date-format": "^4.0.14", "debug": "^4.3.4", @@ -15460,6 +15465,7 @@ "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "license": "MIT", "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^4.0.0", @@ -15473,6 +15479,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==", + "license": "MIT", "optionalDependencies": { "graceful-fs": "^4.1.6" } @@ -15481,6 +15488,7 @@ "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "license": "MIT", "engines": { "node": ">= 4.0.0" }