46
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

node.jsでログをファイル出力する

Posted at

ロギングは設定が簡単なlog4jsを使用します。

log4jsインストール

npm install log4js --save

ログ設定ファイルを作る

vi logger.js

filenameの指定で「./」ここはプロジェクトルートをさすので注意

logger.js
var log4js = require('log4js');
var logger = exports = module.exports = {};
log4js.configure({
     appenders: [
         {
             "type": "file",
             "category": "request",
             "filename": "./logs/request.log",
             "pattern": "-yyyy-MM-dd"
         }
	]
});

logger.request = log4js.getLogger('request');

logsディレクトリを作る

mkdir logs

ログ出力してみる!

vi index.js
index.js
var express = require('express');
var router = express.Router();
var logger = require('./logger');


router.get('/', function(req, res) {
  logger.request.info('url:'+ decodeURI(req.url));
  res.send(200);
});

ログファイルを確認

[2014-09-22 18:34:58.943] [INFO] request - http://localhost:3000/

ok!

まとめ

  • 設定ファイルのfilenameパス設定ではまった...
  • シンプルで使いやすい
  • 次はfluentd と組み合わせてログ活用したい
46
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?