Last active
February 28, 2018 15:24
-
-
Save ThomasKruegl/7d69cc57157d5e9a5023fd13a36c7524 to your computer and use it in GitHub Desktop.
Testing logging nested metadata objects in winston, auto collected to application insights
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
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