Commit 3181bc98 authored by Eirik Alvær's avatar Eirik Alvær
Browse files

Adds log callback function in order to support logging to different places than console.

parent 7018720b
Pipeline #40470 passed with stage
in 25 seconds
{
"name": "@nsd/log-schema-node",
"version": "3.4.0",
"version": "3.5.0",
"main": "lib/index.js",
"types": "lib/index",
"scripts": {
......
......@@ -16,13 +16,25 @@ const defaultValues: PartialLogSchema = {
};
type LevelName = LogSchema["levelName"];
type LogCallback = (loggedData: any) => void;
let logLevel: LevelName = "DEBUG";
let logCallback: LogCallback;
export function setLogLevel(levelName: LevelName) {
logLevel = levelName;
}
export function setLogCallback(cb: LogCallback) {
logCallback = cb;
}
function makeLogCallback(logData: any) {
if (logCallback) {
logCallback(logData);
}
}
function shouldLog(levelName: LevelName) {
switch (logLevel) {
case "DEBUG": {
......@@ -186,6 +198,7 @@ function log(levelName: LogSchema["levelName"], input: LogInput) {
const data = getLogData(fullLogInput);
const str = stringify(data, Number.MAX_SAFE_INTEGER);
console.log(str);
makeLogCallback(data);
if (isDev()) {
JsonSchema.validate(data, schema, { throwError: true });
}
......@@ -200,6 +213,7 @@ function log(levelName: LogSchema["levelName"], input: LogInput) {
});
const str = stringify(errData, Number.MAX_SAFE_INTEGER);
console.error(str);
makeLogCallback(errData);
}
}
......
......@@ -122,5 +122,15 @@ lab.experiment("log-schema-node", () => {
expect(res2).to.equal("");
expect(res3obj.message).to.equal("logLevel");
expect(res4obj.message).to.equal("logLevel");
})
});
lab.test("logCallback", () => {
const message = "Testing logCallback";
let logged;
Logger.setLogCallback((data) => {
logged = data;
});
Logger.error({ message });
expect(logged.message).to.equal(message);
});
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment