インストール
$ npm i -S log4js @types/log4js
使い方
コンソールとファイルに、ログを同時出力。
import { configure, getLogger } from "log4js";
configure({
appenders: {
console: {
type: "console"
},
logfile: { type: "file", filename: "log/app.log" }
},
categories: { default: { appenders: ["console", "logfile"], level: "error" } }
});
const logger = getLogger();
logger.level = "debug";
logger.debug("Some debug messages");
logger.error("aha!");
出力結果
[2019-10-20T10:58:43.835] [DEBUG] default - Some debug messages
[2019-10-20T10:58:43.839] [ERROR] default - aha!
設定を変えると、ログレベルごとに出力先を変えたり、ログの出力のテンプレート(レイアウト)を変更できたりする。
log4jsのドキュメント
https://log4js-node.github.io/log4js-node/
日付の形式について
https://www.npmjs.com/package/date-format