Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express のログ出力を console.xxx() から Log4js へ変更 #623

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions express/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 環境ではエラーになる
},
},
Expand Down
1 change: 1 addition & 0 deletions express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
27 changes: 13 additions & 14 deletions express/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ 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)
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);
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -192,31 +197,25 @@ app.use(
}),
);

/**
* SSR
*/
/* SSR */
app.use(async (req, res, next): Promise<void> => {
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)}`);
});
10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading