Last active
December 14, 2016 20:01
-
-
Save nathanhoel/0b08cbdce16af2c33cfadefb0618db77 to your computer and use it in GitHub Desktop.
Halite node.js logger module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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