Created
July 24, 2018 07:46
-
-
Save ciscoheat/42d3c662e07b68dff6dd5ab47d1c7f0e to your computer and use it in GitHub Desktop.
Haxe logging with nodejs, js-kit and papertrail
This file contains 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
#if nodejs | |
import haxe.Timer; | |
import js.Node; | |
import js.node.Path; | |
import js.npm.Winston; | |
import js.npm.winston.transports.Console; | |
import js.npm.winston.transports.File; | |
#if !no_papertrail | |
import js.npm.winston.transports.Papertrail; | |
#end | |
using StringTools; | |
typedef LogOptions = { | |
name : String, | |
?production : Bool, | |
?logFile : String, | |
#if !no_papertrail | |
?papertrail : Bool | |
#end | |
} | |
class Log | |
{ | |
static public var current : Log; | |
public static function inspect(o : Dynamic, ?options : { | |
?showHidden : Bool, | |
?depth: Null<Int>, | |
?colors: Bool, | |
?customInspect: Bool | |
}) : String { | |
var opt : Dynamic = options == null ? {} : options; | |
if (!Reflect.hasField(opt, "colors")) opt.colors = true; | |
if (!Reflect.hasField(opt, "depth")) opt.depth = 4; | |
return js.Lib.require('util').inspect(o, opt); | |
} | |
public static function dump(o : Dynamic, ?opt : { | |
?showHidden : Bool, | |
?depth: Null<Int>, | |
?colors: Bool, | |
?customInspect: Bool | |
}) : Void { | |
Node.console.log(inspect(o, opt)); | |
} | |
var logger : Winston; | |
#if !no_papertrail | |
var papertrail : Papertrail; | |
#end | |
public function new(?options : LogOptions) { | |
if(options == null) options = {name: ''}; | |
var opt : LogOptions = { | |
name: options.name, | |
production: Reflect.hasField(options, 'production') | |
? options.production | |
: false, | |
logFile: Reflect.hasField(options, 'logFile') | |
? options.logFile | |
: (Node.__dirname + '/' + (options.name.length == 0 ? '' : normalize(options.name) + '-') + 'log.txt'), | |
#if !no_papertrail | |
papertrail: Reflect.hasField(options, 'papertrail') | |
? options.papertrail | |
: true | |
#end | |
}; | |
// { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } | |
var levels = { | |
debug: 7, | |
info: 6, | |
warning: 4, | |
error: 3, | |
alert: 1 | |
}; | |
var transports : Array<Dynamic> = [ | |
new Console({ | |
level: !opt.production ? 'debug' : 'error', | |
timestamp: true, | |
colorize: Node.process.stdout.isTTY, | |
handleExceptions: opt.production | |
}) | |
]; | |
#if !no_papertrail | |
if(opt.papertrail) transports.push( | |
new Papertrail({ | |
level: !opt.production ? 'debug' : 'info', | |
host: PAPERTRAIL_LOG_DESTINATION, | |
program: opt.name.length == 0 ? 'test' : opt.name, | |
port: PAPERTRAIL_PORT, | |
colorize: true, | |
handleExceptions: false // package will crash otherwise when an exception occurs | |
}) | |
); | |
#end | |
if(opt.logFile != '') transports.push( | |
new File({ | |
level: !opt.production ? 'debug' : 'info', | |
filename: opt.logFile, | |
json: false, | |
prettyPrint : true, | |
handleExceptions: opt.production | |
}) | |
); | |
this.logger = new Winston({ | |
levels: levels, | |
transports: transports | |
}); | |
} | |
public function debug(msg : Dynamic, ?metadata : {}) { | |
log('debug', msg, metadata); | |
} | |
public function info(msg : Dynamic, ?metadata : {}) { | |
log('info', msg, metadata); | |
} | |
public function warning(msg : Dynamic, ?metadata : {}) { | |
log('warning', msg, metadata); | |
} | |
public function error(msg : Dynamic, ?metadata : {}) { | |
log('error', msg, metadata); | |
} | |
public function alert(msg : Dynamic, ?metadata : {}) { | |
log('alert', msg, metadata); | |
} | |
public function close(?done : Void -> Void) { | |
Timer.delay(function() { | |
logger.close(); | |
if(done != null) Timer.delay(done, 100); | |
}, 1000); | |
} | |
////////////////////////////////////////////////// | |
function log(level : String, msg : Dynamic, metadata : {}) { | |
if(metadata == null) logger.log(level, msg); | |
else logger.log(level, msg, metadata); | |
} | |
static function normalize(s : String) { | |
while(s.length > 0 && !~/^[A-Za-z\d]/.match(s)) s = s.substr(1); | |
return ~/[^\w-_\.]/.replace(s.replace(" ", "-").toLowerCase(), ''); | |
} | |
} | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment