Commit 7018720b authored by Eirik Alvær's avatar Eirik Alvær

Makes it possible to set which logLevel will be logged for ExpressLogger.

parent 85be3ed8
Pipeline #26584 passed with stage
in 20 seconds
{
"name": "@nsd/log-schema-node",
"version": "3.3.1",
"version": "3.4.0",
"main": "lib/index.js",
"types": "lib/index",
"scripts": {
......
......@@ -15,6 +15,31 @@ const defaultValues: PartialLogSchema = {
serviceVersion: process.env.npm_package_version || "unknown"
};
type LevelName = LogSchema["levelName"];
let logLevel: LevelName = "DEBUG";
export function setLogLevel(levelName: LevelName) {
logLevel = levelName;
}
function shouldLog(levelName: LevelName) {
switch (logLevel) {
case "DEBUG": {
return true;
}
case "INFO": {
return levelName !== "DEBUG";
}
case "WARN": {
return levelName === "WARN" || levelName === "ERROR";
}
case "ERROR": {
return levelName === "ERROR";
}
}
}
export interface LogInput extends PartialLogSchema {
// Modified props
message: string;
......@@ -150,6 +175,9 @@ function getLogData(input: LogInput): LogSchema {
}
function log(levelName: LogSchema["levelName"], input: LogInput) {
if (!shouldLog(levelName)) {
return;
}
if (isDev()) {
console.log("----------------------------------------------------------------------------------");
}
......
......@@ -92,5 +92,35 @@ lab.experiment("log-schema-node", () => {
const obj: LogSchema = JSON.parse(logged);
expect(obj.message).to.equal("default-serviceStack");
expect(obj.serviceStack).to.equal("TEST");
})
});
lab.test("logLevel", () => {
Logger.setLogLevel("WARN");
const res1 = Capcon.captureStdout(() => {
Logger.debug({
message: "logLevel"
});
});
const res2 = Capcon.captureStdout(() => {
Logger.info({
message: "logLevel"
});
});
const res3 = Capcon.captureStdout(() => {
Logger.warn({
message: "logLevel"
});
});
const res3obj: LogSchema = JSON.parse(res3);
const res4 = Capcon.captureStdout(() => {
Logger.error({
message: "logLevel"
});
});
const res4obj: LogSchema = JSON.parse(res4);
expect(res1).to.equal("");
expect(res2).to.equal("");
expect(res3obj.message).to.equal("logLevel");
expect(res4obj.message).to.equal("logLevel");
})
});
\ 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