Skip to content

Commit

Permalink
Feat(module-logger): added support for colored logs
Browse files Browse the repository at this point in the history
Closes #90
  • Loading branch information
Akalanka47000 committed Jan 7, 2024
1 parent 0ee3ff3 commit 318dac3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/module-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test": "bash ../../scripts/test/test.sh"
},
"dependencies": {
"chalk": "4.1.2",
"express-http-context": "1.2.4",
"winston": "3.8.2",
"winston-daily-rotate-file": "4.7.1"
Expand Down
21 changes: 19 additions & 2 deletions packages/module-logger/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import crypto from "crypto";
import chalk from "chalk";
import context from "express-http-context";
import * as winston from "winston";
import "winston-daily-rotate-file";
Expand All @@ -18,6 +19,8 @@ let _defaultConfig = {
globalAttributes: {}
};

const _defaultKeys = ["level", "correlationId", "timestamp", "message"];

const _createLogger = () => {
let transports = [];

Expand Down Expand Up @@ -56,11 +59,25 @@ const _createLogger = () => {
correlationId = crypto.randomBytes(16).toString("hex");
context.set("correlationId", correlationId);
}
infoObj["correlationId"] = correlationId;
infoObj["correlationId"] = chalk.blue(correlationId);
return { ...infoObj, ...(_defaultConfig.globalAttributes ?? {}) };
})(),
winston.format.timestamp(),
winston.format.json()
winston.format.json(),
winston.format.colorize({ all: true }),
winston.format.printf((info) => {
return (
`${[..._defaultKeys, ...Object.keys(info)].reduce((acc, key, i) => {
if ((_defaultKeys.includes(key) && i <= 3) || (i > 3 && !_defaultKeys.includes(key))) {
if (i > 0) acc += ", ";
return (acc += _defaultKeys.includes(key)
? `"${chalk.gray(key)}": "${info[key]}"`
: `"${chalk.gray(chalk.underline(key))}": "${chalk.underline(info[key])}"`);
}
return acc;
}, "{ ") } }`
);
})
),
transports
});
Expand Down
5 changes: 4 additions & 1 deletion packages/module-logger/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ describe("module-logger", () => {
});
it("should-log-to-console", () => {
const mLogger = moduleLogger("console-logger");
expect(() => mLogger.info("hello-world")).not.toThrow(Error);
expect(() => mLogger.info("this is an info message")).not.toThrow(Error);
expect(() => mLogger.error("this is an error message")).not.toThrow(Error);
expect(() => mLogger.warn("this is a warning message")).not.toThrow(Error);
expect(() => mLogger.info("this is a message with a custom object", { custom: "object" })).not.toThrow(Error);
});
it("should-log-to-file", () => {
configure({
Expand Down
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 318dac3

Please sign in to comment.