Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
winston is failing to flush log files with DailyRotateFile
var util = require('util');
var winston = require('winston');
var BaseTrigger = require('./baseTrigger.js').BaseTrigger;
var TinyCache = require( 'tinycache' );
var cache = new TinyCache();
/*
Trigger that logs chat messages to files by groupchat "bot.groupsteamid64.date.log". You NEED to put this trigger first, or it might not log everything. Removes unnecessary line breaks (keeps single breaks, but lines with just spaces or nothing will be removed)
prefix = string - what to prefix the logfiles with. Defaults to "bot." After this comes a '.', the bot's username, another ., the 'name' (see next option) of the groupchat (or steamid64) and a ., then the date.
roomNames = {} - object containing group names by steamid64, to be used in filenames. {'steamid64':'GroupName','steamid64':'GroupName'}
html = string/bool - Set to true to enable html output. Each chat line will be <p><span class="timestamp">timestamp</span> &lt;<a href="linktoprofilebysteamid64" class="username">profile name</a>&gt; - <span class="message">message</span></p>. You'll need to figure out adding the header yourself
datePattern = string - what pattern to name the logs as. see http://goo.gl/tYYEBf for info. Defaults to 'yyyy-MM-dd', 4 digit date with padded month then date (sortable)
logUserChats = bool - log chats with users or no? Defaults to true.
rooms = array (optional) - universal trigger option. If you only want certain groups to be logged.
ignore = array (optional) - universal trigger option. If you want certain groups to *not* be logged.
*/
var LogTrigger = function() {
LogTrigger.super_.apply(this, arguments);
};
util.inherits(LogTrigger, BaseTrigger);
var type = "LogTrigger";
exports.triggerType = type;
exports.create = function(name, chatBot, options) {
var trigger = new LogTrigger(type, name, chatBot, options);
trigger.respectsMute = false;
trigger.options.suffix = trigger.options.suffix || ".log";
trigger.logs = {};
return trigger;
};
// Return true if a message was sent
LogTrigger.prototype._respondToFriendMessage = function(userId, message) {
if(this.options.logUserChats && this.options.logUserChats==false) return false;
var that = this;
if(!that.logs[userId]) {
this.logs[userId] = new winston.Logger;
console.log("Creating logger for user "+this._prefixes(userId));
this.logs[userId].add(winston.transports.DailyRotateFile, {
level: (that.options.loglevel || "warn"),
colorize: false,
timestamp: false,
filename: that._prefixes(userId),
datePattern: that.options.datePattern || "yyyy-MM-dd-m",
json:false
});
this.logs[userId].add(winston.transports.Console,{
handleExceptions: false,
colorize: true,
timestamp: true,
level: "info",
json: false
});
}
this.logs[userId].info(this._timestamp() + this._username(userId) + this._message(message.replace(/\n\s*\n/g, '\n')),function(){console.log("done");});
// console.log(this.logs);
return false;
}
// Return true if a message was sent
LogTrigger.prototype._respondToChatMessage = function(roomId, userId, message) {
var that = this;
if(!that.logs[roomId]) {
console.log("Creating logger for room "+this._prefixes(roomId));
this.logs[roomId] = new winston.Logger;
this.logs[roomId].add(winston.transports.DailyRotateFile, {
level: (that.options.loglevel || "warn"),
colorize: false,
timestamp: false,
filename: that._prefixes(roomId),
datePattern: that.options.datePattern || "yyyy-MM-dd-m",
json:false
});
}
this.logs[roomId].info(this._timestamp() + this._username(userId) + this._message(message.replace(/\n\s*\n/g, '\n')));
// console.log(this.logs);
return false;
}
String.prototype._pad = function (length, character) {
return new Array(length - this.length + 1).join(character || ' ') + this;
}
LogTrigger.prototype._timestamp = function() {
var date = new Date();
var string = String(date.getHours())._pad(2,'0') + ':'
+ String(date.getMinutes())._pad(2,'0') + ":"
+ String(date.getSeconds())._pad(2,'0');
if(this.options.html && this.options.html!=false) return '<p><span class="timestamp">'+string+'</span> ';
else return string+' ';
}
LogTrigger.prototype._username = function(steamId) {
console.log(steamId);
console.log(typeof steamId);
if(this.options.html && this.options.html!=false)
return '<a class="username" href="http://steamcommunity.com/profiles/'+steamId+'">'
+ ((this.chatBot.steamClient.users && steamId in this.chatBot.steamClient.users) ? this.chatBot.steamClient.users[steamId].playerName : steamId)
+ '</a> ';
else return whoCalled = ((this.chatBot.steamClient.users && steamId in this.chatBot.steamClient.users) ? (this.chatBot.steamClient.users[steamId].playerName + "/"+steamId) : steamId) + " ";
}
LogTrigger.prototype._message = function(message) {
if(this.options.html && this.options.html!=false)
return '<span class="message">'+message+'</span></p>';
else return message;
}
LogTrigger.prototype._prefixes = function(steamId) {
console.log(typeof steamId + " steamId: " + steamId);
var prefix = (this.options.prefix || "bot."+this.chatBot.username)+".";
if(this.options.roomNames && this.options.roomNames[steamId])
return prefix + this.options.roomNames[steamId];
else return prefix + steamId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.