UNPKG

2.19 kBJavaScriptView Raw
1/*
2Copyright 2018 André Jaenisch
3Copyright 2019 The Matrix.org Foundation C.I.C.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17
18/**
19 * @module logger
20 */
21
22import log from "loglevel";
23
24// This is to demonstrate, that you can use any namespace you want.
25// Namespaces allow you to turn on/off the logging for specific parts of the
26// application.
27// An idea would be to control this via an environment variable (on Node.js).
28// See https://www.npmjs.com/package/debug to see how this could be implemented
29// Part of #332 is introducing a logging library in the first place.
30const DEFAULT_NAMESPACE = "matrix";
31
32// because rageshakes in react-sdk hijack the console log, also at module load time,
33// initializing the logger here races with the initialization of rageshakes.
34// to avoid the issue, we override the methodFactory of loglevel that binds to the
35// console methods at initialization time by a factory that looks up the console methods
36// when logging so we always get the current value of console methods.
37log.methodFactory = function(methodName, logLevel, loggerName) {
38 return function(...args) {
39 const supportedByConsole = methodName === "error" ||
40 methodName === "warn" ||
41 methodName === "trace" ||
42 methodName === "info";
43 if (supportedByConsole) {
44 return console[methodName](...args);
45 } else {
46 return console.log(...args);
47 }
48 };
49};
50
51/**
52 * Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
53 * Can be tailored down to specific use cases if needed.
54 */
55export const logger = log.getLogger(DEFAULT_NAMESPACE);
56logger.setLevel(log.levels.DEBUG);
57