Skip to content

Commit

Permalink
Configurable timestamps (#64)
Browse files Browse the repository at this point in the history
* big error log

* configurable

* bump

* adding test

* be sure with test
  • Loading branch information
zoe-codez authored Sep 5, 2024
1 parent ffd3399 commit 4a34fad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"repository": {
"url": "git+https://github.com/Digital-Alchemy-TS/core"
},
"version": "24.8.4",
"version": "24.9.1",
"author": {
"url": "https://github.com/zoe-codez",
"name": "Zoe Codez"
Expand Down
6 changes: 3 additions & 3 deletions src/extensions/logger.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const LEVEL_MAX = 7;
// #region Service definition
export async function Logger({ lifecycle, config, internal }: TServiceParams) {
const chalk = (await import("chalk")).default;
const timestampFormat =
internal.boot.options.loggerOptions?.timestamp_format ?? "ddd HH:mm:ss.SSS";

const YELLOW_DASH = chalk.yellowBright(frontDash);
const BLUE_TICK = chalk.blue(`>`);
Expand Down Expand Up @@ -132,9 +134,7 @@ export async function Logger({ lifecycle, config, internal }: TServiceParams) {
delete data.context;
delete data.name;

const timestamp = chalk.white(
`[${dayjs().format("ddd hh:mm:ss.SSS")}]`,
);
const timestamp = chalk.white(`[${dayjs().format(timestampFormat)}]`);
let logMessage: string;
if (!is.empty(parameters)) {
const text = parameters.shift() as string;
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/wiring.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ async function Teardown(internal: InternalDefinition, logger: ILogger) {
internal.boot.completedLifecycleEvents.add("ShutdownComplete");
} catch (error) {
// ! oof
logger.error(
{ error, name: Teardown },
// eslint-disable-next-line no-console
console.error(
{ error },
"error occurred during teardown, some lifecycle events may be incomplete",
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/wiring.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ export type BootstrapOptions = {
*/
customLogger?: ILogger;

/**
* fine tine the built in logger
*/
loggerOptions?: {
/**
* > default: ddd HH:mm:ss.SSS
*/
timestamp_format?: string;
};

/**
* Show detailed boot time statistics
*/
Expand Down
28 changes: 28 additions & 0 deletions src/testing/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// magic import, do not remove / put anything above
import "..";

import dayjs from "dayjs";

import { CreateApplication, is } from "../extensions";
import {
ApplicationDefinition,
Expand Down Expand Up @@ -165,4 +167,30 @@ describe("Logger", () => {
).toBe(expected);
});
});

describe("Fine Tuning", () => {
it("allows timestamp format to be configured", async () => {
const format = "ddd HH:mm:ss";
application = CreateApplication({
configurationLoaders: [],
// @ts-expect-error For unit testing
name: "testing",
services: {
Test({ logger }: TServiceParams) {
jest.spyOn(console, "error").mockImplementation(() => {});
jest.spyOn(console, "log").mockImplementation(() => {});
const spy = jest
.spyOn(dayjs.prototype, "format")
.mockImplementation(() => "timestamp");
logger.info(`test`);
expect(spy).toHaveBeenCalledWith(format);
},
},
});
await application.bootstrap({
// ...BASIC_BOOT,
loggerOptions: { timestamp_format: format },
});
});
});
});

0 comments on commit 4a34fad

Please sign in to comment.