Skip to content

Instantly share code, notes, and snippets.

@nathanhoel
Last active December 14, 2016 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanhoel/0b08cbdce16af2c33cfadefb0618db77 to your computer and use it in GitHub Desktop.
Save nathanhoel/0b08cbdce16af2c33cfadefb0618db77 to your computer and use it in GitHub Desktop.
Halite node.js logger module
const loggerOn = 0;
var winston = null;
try {
winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(
winston.transports.File,
{
filename: 'match.log',
json: false,
formatter: (options) => { return options.message; }
}
);
winston.log('info', 'LOGGER INITIALIZED');
} catch (e) {
// when you submit your bot to Halite.io, don't include the winston module
// logging to file is super slow and you can get booted if you try to create files
// on their servers
}
// cause the process to wait giving time for the logs to get flushed to the file
process.on('exit', () => {
setTimeout(() => {}, 5000);
});
process.on('uncaughtException', (err) => {
// remove and readd the file transport to deal with a bug with adding formatters that won't log errors
winston.remove(winston.transports.File);
winston.add(winston.transports.File, { filename: 'match.log' });
winston.log('error', 'Fatal uncaught exception', err);
});
module.exports.log = function(message) {
if (!winston || !loggerOn) { return; }
winston.log('info', message);
}
module.exports.logSite = function(site) {
this.log(`SITE: owner: ${site.owner} | strength: ${site.strength} | production: ${site.production}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment