Skip to content

Commit

Permalink
Feat(functions): added colored logs to tracing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Jan 7, 2024
1 parent 97d3d20 commit 89d8b06
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test": "bash ../../scripts/test/test.sh"
},
"dependencies": {
"@sliit-foss/module-logger": "1.1.5",
"@sliit-foss/module-logger": "1.2.3",
"chalk": "4.1.2",
"express-http-context": "1.2.4"
},
"repository": {
Expand Down
12 changes: 10 additions & 2 deletions packages/functions/src/traced.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { performance } from "perf_hooks";
import { default as chalk } from "chalk";
import { moduleLogger } from "@sliit-foss/module-logger";
import { fnName as _fnName } from "./utils";

Expand All @@ -15,11 +16,18 @@ export const _traced = (fn, loggable = {}, fnName) => {
}
const completionLog = () => {
!disableTracing &&
logger.info(`${fnName} execution completed - execution_time : ${performance.now() - startTime}ms`, loggable);
logger.info(
`${fnName} execution completed - ${chalk.bold(chalk.magentaBright("execution_time"))} : ${chalk.bold(
`${performance.now() - startTime}ms`
)}`,
loggable
);
};
const failureLog = (err) => {
if (!disableTracing && !err.isLogged) {
logger.error(`${fnName} execution failed - error: ${err.message} - stack: ${err.stack}`);
logger.error(
`${fnName} execution failed - ${chalk.bold("error")}: ${err.message} - ${chalk.bold("stack")}: ${err.stack}`
);
err.isLogged = true;
}
throw err;
Expand Down
8 changes: 6 additions & 2 deletions packages/functions/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import chalk from "chalk";

export const fnName = (fn) => {
let name = fn.name;
while (1) {
const replaced = name?.replace("bound", "");
if (name.startsWith("bound") && replaced?.startsWith(" ")) name = replaced?.trim();
else break;
}
if (!name) return "Unnamed function";
if (!name) return coloredFnName("Unnamed function");
if (name.startsWith(" ")) return name.slice(1);
return name;
return coloredFnName(name);
};

export const coloredFnName = (fn) => chalk.bold(chalk.magentaBright(fn));
3 changes: 2 additions & 1 deletion packages/functions/test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { mockLogger } = require("./__mocks__");

const { asyncHandler, tracedAsyncHandler } = require("../src");
const { coloredFnName } = require("../src/utils");

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -31,7 +32,7 @@ describe("asyncHandler", () => {
await tracedAsyncHandler(function testTracedFunction() {
return "test";
})(mockReq, mockRes, mockNext);
expect(mockLogger.info).toBeCalledWith("testTracedFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("testTracedFunction")} execution initiated`, {});
expect(mockNext).toBeCalled();
});
});
15 changes: 8 additions & 7 deletions packages/functions/test/traced.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { mockLogger } = require("./__mocks__");

const { traced, trace, cleanTrace, cleanTraced } = require("../src");
const { coloredFnName } = require("../src/utils");

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -14,25 +15,25 @@ describe("traced", () => {
return _mockResult;
})();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("testFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("testFunction")} execution initiated`, {});
});
test("test async function", async () => {
const res = await traced(async function testFunction() {
return _mockResult;
})();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("testFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("testFunction")} execution initiated`, {});
});
test("test arrow function", () => {
const testArrowFunction = () => _mockResult;
const res = traced(testArrowFunction)();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("testArrowFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("testArrowFunction")} execution initiated`, {});
});
test("test unnamed function", () => {
const res = traced(() => _mockResult)();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("Unnamed function execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("Unnamed function")} execution initiated`, {});
});
test("test disabled tracing", () => {
process.env.DISABLE_FUNCTION_TRACING = "true";
Expand Down Expand Up @@ -61,7 +62,7 @@ describe("trace", () => {
return _mockResult;
});
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("foo execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("foo")} execution initiated`, {});
});
});

Expand All @@ -71,7 +72,7 @@ describe("clean-trace", () => {
return _mockResult;
});
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("namedFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("namedFunction")} execution initiated`, {});
});
test("test unnamed function", () => {
const res = cleanTrace(() => _mockResult);
Expand All @@ -86,7 +87,7 @@ describe("clean-traced", () => {
return _mockResult;
})();
expect(res).toStrictEqual(_mockResult);
expect(mockLogger.info).toBeCalledWith("namedFunction execution initiated", {});
expect(mockLogger.info).toBeCalledWith(`${coloredFnName("namedFunction")} execution initiated`, {});
});
test("test unnamed function", () => {
const res = cleanTraced(() => _mockResult)();
Expand Down
7 changes: 5 additions & 2 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 89d8b06

Please sign in to comment.