Help us understand the problem. What is going on with this article?

Hubotのログをlog4jsでホームディレクトリ配下に出力してみる

More than 3 years have passed since last update.

log4jsを使うために、package.jsonに下記を追加する
"log4js": "^0.6.37"

ログを出力するための記述を行う。
Linuxなどで、ホームディレクトリを指定するため、#{process.env.HOME}を使用し、ホームディレクトリを取得する。
※Linuxなどで記載するときのように~/logsと記述していて、すこしハマった。。。

nekobot.coffee
#ここから
  log4js = require('log4js')
  log4js.configure {
      "appenders": [
        {
          "category": "access",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_access.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        },
        {
          "category": "system",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_system.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        },
        {
          "category": "error",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_error.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        }
      ],
      "levels": {
        "access": "INFO",
        "system": "INFO",
        "error": "INFO"
      }
  }

  systemLogger = log4js.getLogger('system')
  accessLogger = log4js.getLogger('access')
  errorLogger = log4js.getLogger('error')
#ここまで
module.exports = (robot) ->
  robot.hear /(shiitake|([C椎]|[しシ]\s*([いイぃィー]))\s*([たタ]\s*[けケ]|茸)|Shiitake\s*mushrooms|เห็ดหอม)/i, (msg) ->
      room = msg.envelope.room
      if room == "Shell"
        text = msg.match[1]
        msg.send  "#{text}一人前追加でーす。はいよろこんでー"

続いて、実際に出力する際の記述を追加

nekobot.coffee
  log4js = require('log4js')
  log4js.configure {
      "appenders": [
        {
          "category": "access",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_access.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        },
        {
          "category": "system",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_system.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        },
        {
          "category": "error",
          "type": "dateFile",
          "filename": "#{process.env.HOME}/logs/nekobot_error.log",
          "pattern": "-yyyy-MM-dd",
          "backups": 7
        }
      ],
      "levels": {
        "access": "INFO",
        "system": "INFO",
        "error": "INFO"
      }
  }

  systemLogger = log4js.getLogger('system')
  accessLogger = log4js.getLogger('access')
  errorLogger = log4js.getLogger('error')

  module.exports = (robot) ->
    robot.hear /(shiitake|([C椎]|[しシ]\s*([いイぃィー]))\s*([たタ]\s*[けケ]|茸)|Shiitake\s*mushrooms|เห็ดหอม)/i, (msg) ->
      accessLogger.info 'nekobot connected'
      room = msg.envelope.room
      accessLogger.info room
      if room == "Shell"
        text = msg.match[1]
        msg.send  "#{text}一人前追加でーす。はいよろこんでー"

実際に試してみる

$ bin/hubot
nekobot> [Sun Aug 28 2016 02:39:15 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379
nekobot> 

Hubotを起動するとホームディレクトリ配下のlogsに下記3ファイルが作成される

$ pwd
/Users/kijitoraneko/logs
$
$ ls -ltr
total 0
-rw-r--r--  1 kijitoraneko  staff  0  8 28 02:39 nekobot_system.log
-rw-r--r--  1 kijitoraneko  staff  0  8 28 02:39 nekobot_error.log
-rw-r--r--  1 kijitoraneko  staff  0  8 28 02:39 nekobot_access.log
$

続けて

nekobot> しいたけ
nekobot> しいたけ一人前追加でーす。はいよろこんでー

と、ボットに反応させると

$ ls -ltr
total 8
-rw-r--r--  1 kijitoraneko  staff    0  8 28 02:39 nekobot_system.log
-rw-r--r--  1 kijitoraneko  staff    0  8 28 02:39 nekobot_error.log
-rw-r--r--  1 kijitoraneko  staff  108  8 28 02:41 nekobot_access.log
$ vim nekobot_access.log
[2016-08-28 02:41:35.589] [INFO] access - nekobot connected
[2016-08-28 02:41:35.590] [INFO] access - Shell

ちゃんと書き込まれているようだ。

なお、ログレベル等についてはコチラを参考にしました。
log4jsの覚え書き - log4.jsを使ってみた -

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away