So, another week, another externs challenge. Again, I got helped by @haxiomic’s dts2hx, who is probably sick’n’tired of getting tagged every question I post around here But hey, it’s weekend, we’re engineers, WTH (what the heaven).
I did manage to get Winston to work… somewhat. Unfortunately, there’s a configuration object that I don’t know how to instantiate and which makes my version of Winston rather useless (poor beast has a heartbeat but no arms and legs… in English now: the logging works but it doesn’t have any Transports to output to).
Winston uses Transports
to log anything. One has to provide a Transport
or array of it to the createLogger function.
typedef LoggerOptions = {
...
var transports : ts.AnyOf2<Transport, Array<Transport>>;
I’ve been trying to use a FileTransportInstance
, and since that’s a typedef…
typedef FileTransportInstance = {
var name : String;
var filename : String;
I’ve been trying to instantiate it with the object literal initializer, just like I initialize LoggerOptions (which works). But for FileTransportInstance, I get nowhere. Well, I get somewhere but it’s somewhere with lots of errors and I don’t think that’s the where I want to get to
I noticed that in TypeScript
interface FileTransportInstance extends Transport {
And Transport does have a new function, but it doesn’t get inherited in Haxe because in Haxe they’re all typedefs.
I did try to instantiate Transport, since it has a new
member
function new(?opts:winston.transport.TransportStreamOptions);
But I vaguely remember that new doesn’t work this way in externs. Sorry if I forgot the exact way it works, I’m juggling way too many things in my head this period.
Here’s my code so far (and there’s a github repo below too):
import js.Node.console;
import js.node.Path;
import js.Node.__dirname;
import js.Browser;
import winston.LoggerOptions;
import logform.Format_;
import winston.transports.Transports;
import winston.transports.FileTransportInstance;
import winston.Transport;
class Test {
static function main() {
SourceMapSupport.install();
var transportsArr:Array<Transport> = new Array<Transport>();
// crash: TypeError: winston_Transport is not a constructor
// var transportErr:Transport = new Transport();
// compile error: missing fields in object initalizer. After which another error saying
// { filename : String } should be winston.transports.FileTransportInstance
// var transportErr:FileTransportInstance = {filename: "error.log"};
// transportsArr.push(transportErr);
var loggerOptions:LoggerOptions = {
level: "info",
// commenting out transports (and the above errors) actually makes it work,
// but of course nothing is logged (and the library warns about it).
// transports: transportsArr,
format: Format_.json(),
defaultMeta: {service: 'user-service'},
};
var logger:winston.Logger = Winston.createLogger(loggerOptions);
logger.log("info", "This is an info log.");
logger.log("error", "This be an error log.");
}
}
Aaaaanyway, I added this one to my GitHub repo with Haxe’s World of Externs . The problem can be easily reproduced with a simple haxe build.hxml
in the winston-haxe directory: