概要
ログを出しかた、log4jsを利用しての方法について記述したいと思います。
log4jsとは
javaで利用されているlog4jをjavascriptで利用できるようにしたモジュール。
導入
モジュールのインストールします。
$ npm install log4js
$ npm install -D @types/log4js
configを設定します。
{
"appenders": {
"console": { "type": "console" },
"file": { "type": "file", "filename": "logs/server.log" }
},
"categories": {
"default": { "appenders": ["console", "file"], "level": "debug" }
}
}
使用の用意します。
import log4js from 'log4js';
...
const logConfigure: log4js.Configuration = {
"appenders": {
"console": { "type": "console" },
"file": { "type": "file", "filename": "logs/server.log" }
},
"categories": {
"default": { "appenders": ["console", "file"], "level": "debug" }
}
}
const logger = log4js.configure(logConfigure).getLogger();
...
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
logger.log(`Server is running on port: ${port}`);
});
起動してログを確認する
$ npx ts-node src/index.ts
Server is running on port: 3000
[2024-04-01T16:06:33.450] [INFO] default - Server is running on port: 3000
..
..
$ cat logs/server.log
[2024-04-01T16:06:33.450] [INFO] default - Server is running on port: 3000
全体
index.ts
import cors, { CorsOptions } from 'cors';
import express from 'express';
import dotenv from 'dotenv';
import { router } from './router';
import log4js from 'log4js';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const logConfigure: log4js.Configuration = {
"appenders": {
"console": { "type": "console" },
"file": { "type": "file", "filename": "logs/server.log" }
},
"categories": {
"default": { "appenders": ["console", "file"], "level": "debug" }
}
}
const logger = log4js.configure(logConfigure).getLogger();
const corsOptions: CorsOptions = {
origin: ['http://localhost:3000'],
methods: [ 'GET', 'POST', 'PUT', 'DELET' ],
allowedHeaders: [ 'Content-Type', 'X-Requested-With', 'Authorization' ],
}
app.use(cors(corsOptions));
// ミドルウェア設定
app.use(express.json());
//
app.use('/users',router);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
logger.log(`Server is running on port: ${port}`);
});
configをファイル化して構造的にする
$ cat config/log.config.json
{
"appenders": {
"console": { "type": "console" },
"file": { "type": "file", "filename": "logs/server.log" }
},
"categories": {
"default": { "appenders": ["console", "file"], "level": "debug" }
}
}
index.tsの変更
import cors, { CorsOptions } from 'cors';
import express from 'express';
import dotenv from 'dotenv';
import { router } from './router';
import log4js from 'log4js';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const logger = log4js.configure('src/config/log.config.json').getLogger();
const corsOptions: CorsOptions = {
origin: ['http://localhost:3000'],
methods: [ 'GET', 'POST', 'PUT', 'DELET' ],
allowedHeaders: [ 'Content-Type', 'X-Requested-With', 'Authorization' ],
}
app.use(cors(corsOptions));
// ミドルウェア設定
app.use(express.json());
//
app.use('/users',router);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
logger.log(`Server is running on port: ${port}`);
});