1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-06-08 15:21:53 +03:00
matrix-js-sdk/spec/unit/logger.spec.ts
Richard van der Hoff 33648a711c
Remove deprecated PrefixedLogger interface (#4705)
* Add some tests for `logger`

* Remove deprecated `PrefixedLogger` interface

`PrefixedLogger` has been deprecated for some time, so let's remove it now,
while we have a major version bump.

We can tidy up some of the other logic while we're here.

Unfortunately lots of the code still uses `logger.log` which isn't exposed by
the `Logger` interface, so we need to keep exposing that where it was before.
2025-02-11 12:19:46 +00:00

52 lines
1.8 KiB
TypeScript

/*
Copyright 2025 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* eslint-disable no-console */
import loglevel from "loglevel";
import { logger } from "../../src/logger.ts";
afterEach(() => {
jest.restoreAllMocks();
});
describe("logger", () => {
it("should log to console by default", () => {
jest.spyOn(console, "debug").mockReturnValue(undefined);
logger.debug("test1");
logger.log("test2");
expect(console.debug).toHaveBeenCalledWith("test1");
expect(console.debug).toHaveBeenCalledWith("test2");
});
it("should allow creation of child loggers which add a prefix", () => {
jest.spyOn(loglevel, "getLogger");
jest.spyOn(console, "debug").mockReturnValue(undefined);
const childLogger = logger.getChild("[prefix1]");
expect(loglevel.getLogger).toHaveBeenCalledWith("matrix-[prefix1]");
childLogger.debug("test1");
expect(console.debug).toHaveBeenCalledWith("[prefix1]", "test1");
const grandchildLogger = childLogger.getChild("[prefix2]");
expect(loglevel.getLogger).toHaveBeenCalledWith("matrix-[prefix1][prefix2]");
grandchildLogger.debug("test2");
expect(console.debug).toHaveBeenCalledWith("[prefix1][prefix2]", "test2");
});
});