- Introduced a new `LOG_LEVEL` configuration option in the `.env.example` file to allow users to set the logging level (DEBUG, INFO, WARNING, ERROR). - Updated the `Settings` class to include the `log_level` attribute, normalizing its value to ensure valid logging levels are used. - Modified the logging setup in `run.py` to utilize the configured log level, enhancing flexibility in log management. - Enhanced the Mini App to include the logging level in the JavaScript configuration, allowing for consistent logging behavior across the application. - Added a new `logger.js` module for frontend logging, implementing level-based filtering and console delegation. - Included unit tests for the new logger functionality to ensure proper behavior and level handling.
95 lines
2.7 KiB
JavaScript
95 lines
2.7 KiB
JavaScript
/**
|
|
* Unit tests for logger: level filtering and console delegation.
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { logger } from "./logger.js";
|
|
|
|
describe("logger", () => {
|
|
const origWindow = globalThis.window;
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(console, "debug").mockImplementation(() => {});
|
|
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
if (globalThis.window) {
|
|
delete globalThis.window.__DT_LOG_LEVEL;
|
|
}
|
|
});
|
|
|
|
function setLevel(level) {
|
|
if (!globalThis.window) globalThis.window = {};
|
|
globalThis.window.__DT_LOG_LEVEL = level;
|
|
}
|
|
|
|
it("at level info does not call console.debug", () => {
|
|
setLevel("info");
|
|
logger.debug("test");
|
|
expect(console.debug).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("at level info calls console.info for logger.info", () => {
|
|
setLevel("info");
|
|
logger.info("hello");
|
|
expect(console.info).toHaveBeenCalledWith("[DutyTeller][info]", "hello");
|
|
});
|
|
|
|
it("at level info calls console.warn and console.error", () => {
|
|
setLevel("info");
|
|
logger.warn("w");
|
|
logger.error("e");
|
|
expect(console.warn).toHaveBeenCalledWith("[DutyTeller][warn]", "w");
|
|
expect(console.error).toHaveBeenCalledWith("[DutyTeller][error]", "e");
|
|
});
|
|
|
|
it("at level debug calls console.debug", () => {
|
|
setLevel("debug");
|
|
logger.debug("dbg");
|
|
expect(console.debug).toHaveBeenCalledWith("[DutyTeller][debug]", "dbg");
|
|
});
|
|
|
|
it("at level error does not call console.debug or console.info", () => {
|
|
setLevel("error");
|
|
logger.debug("d");
|
|
logger.info("i");
|
|
expect(console.debug).not.toHaveBeenCalled();
|
|
expect(console.info).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("at level error calls console.error", () => {
|
|
setLevel("error");
|
|
logger.error("err");
|
|
expect(console.error).toHaveBeenCalledWith("[DutyTeller][error]", "err");
|
|
});
|
|
|
|
it("passes extra args to console", () => {
|
|
setLevel("info");
|
|
logger.info("msg", { foo: 1 }, "bar");
|
|
expect(console.info).toHaveBeenCalledWith(
|
|
"[DutyTeller][info]",
|
|
"msg",
|
|
{ foo: 1 },
|
|
"bar"
|
|
);
|
|
});
|
|
|
|
it("defaults to info when __DT_LOG_LEVEL is missing", () => {
|
|
if (globalThis.window) delete globalThis.window.__DT_LOG_LEVEL;
|
|
logger.debug("no");
|
|
expect(console.debug).not.toHaveBeenCalled();
|
|
logger.info("yes");
|
|
expect(console.info).toHaveBeenCalledWith("[DutyTeller][info]", "yes");
|
|
});
|
|
|
|
it("defaults to info when __DT_LOG_LEVEL is invalid", () => {
|
|
setLevel("invalid");
|
|
logger.debug("no");
|
|
expect(console.debug).not.toHaveBeenCalled();
|
|
});
|
|
});
|