Skip to content

Instantly share code, notes, and snippets.

@rduplain
Last active December 14, 2015 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rduplain/5035223 to your computer and use it in GitHub Desktop.
Save rduplain/5035223 to your computer and use it in GitHub Desktop.
Simple dated file logging script for hubot.
# Description:
# Simple room logging, to log/yyyy-mm-dd.log files for all rooms.
# Log format is: isodate - room <user> text
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# None
#
# Author:
# rduplain
fs = require 'fs'
path = require 'path'
util = require 'util'
date_filename = (date = new Date) ->
# zero-fill month and day
zero = '00'
util.format(
'%s-%s-%s.log',
date.getFullYear(),
(zero + (date.getMonth() + 1).toString()).slice(-zero.length),
(zero + date.getDate().toString()).slice(-zero.length))
module.exports = (robot) ->
dir = 'log'
if not fs.existsSync(dir)
fs.mkdirSync(dir)
robot.hear /.*$/i, (msg) ->
# Do not log private messages.
if msg.message.room
now = new Date
log = util.format(
'%s - %s <%s> %s\n',
now.toISOString(),
msg.message.room,
msg.message.user.name,
msg.message.text)
fs.appendFile(path.join(dir, date_filename()), log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment