Skip to content

Commit

Permalink
chore: enhance logger configuration and improve test type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Dec 16, 2024
1 parent fa1149b commit ec5773c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 12 additions & 3 deletions utilities/logger/Logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { describe, it, expect } from "vitest";
import { logger } from "./Logger.js";

const enum LogLevel {
debug = 'debug',
info = 'info',
warn = 'warn',
error = 'error',
fatal = 'fatal',
trace = 'trace',
}

describe("Logger", () => {
describe("logging methods", () => {
["debug", "info", "warn", "error", "fatal", "trace"].forEach((level) => {
it(`should have ${level} method`, () => {
expect(logger[level]).toBeInstanceOf(Function);
expect(logger [level as LogLevel ]).toBeInstanceOf(Function);
});

it(`should log ${level} messages`, () => {
const log = logger.child({ name: "test" });
expect(() => log[level]("test message")).not.toThrow();
expect(() => log[level as LogLevel ]("test message")).not.toThrow();
});
});
});
Expand All @@ -30,7 +39,7 @@ describe("Logger", () => {
describe("error handling", () => {
it("should handle Error objects", () => {
const error = new Error("test error");
expect(() => logger.error(error)).not.toThrow();
expect(() => logger.error(error.message)).not.toThrow();
});
});
});
9 changes: 7 additions & 2 deletions utilities/logger/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import pino from "pino";

export const logger = pino.default({
name: 'app-name',
level: 'debug'
name: 'rusty-motors-server',
level: process.env["LOG_LEVEL"] || 'debug',
formatters: {
level(label) {
return { level: label };
}
},
}) as Logger;

export interface Logger {
Expand Down

0 comments on commit ec5773c

Please sign in to comment.