Skip to content

Instantly share code, notes, and snippets.

@ThomasKruegl
Last active February 28, 2018 15:24
Show Gist options
  • Save ThomasKruegl/7d69cc57157d5e9a5023fd13a36c7524 to your computer and use it in GitHub Desktop.
Save ThomasKruegl/7d69cc57157d5e9a5023fd13a36c7524 to your computer and use it in GitHub Desktop.
Testing logging nested metadata objects in winston, auto collected to application insights
import * as util from "util";
const appInsights = require("applicationinsights");
appInsights.setup("instrumentationKey");
appInsights.start();
const winston = require("winston");
const testLogger1 = new winston.Logger();
const consoleTransport = new winston.transports.Console({
level: "info",
colorize: true,
json: false,
timestamp: true
});
function metaDataStringifier(level: string, msg: string, meta: any) {
if (typeof meta === "object") {
for (const field of Object.keys(meta)) {
const property = meta[field];
if (typeof property === "object") {
meta[field] = util.inspect(property, { depth: null });
}
}
}
return meta;
}
testLogger1.configure({
transports: [
consoleTransport
],
rewriters: [metaDataStringifier],
level: "silly",
exitOnError: true // exit application after logging unhandled exception
});
const aComplexObject = {
message: "i am out!",
innerObject: {
message: "i am in!",
innerObject2: {
message: "i am in2!",
innerObject3: {
message: "i am in3!",
innerObject4: { message: "i am in4!" },
}
}
}
};
testLogger1.info(aComplexObject, aComplexObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment