1 | /*
|
2 | Copyright 2018 André Jaenisch
|
3 | Copyright 2019 The Matrix.org Foundation C.I.C.
|
4 |
|
5 | Licensed under the Apache License, Version 2.0 (the "License");
|
6 | you may not use this file except in compliance with the License.
|
7 | You may obtain a copy of the License at
|
8 |
|
9 | http://www.apache.org/licenses/LICENSE-2.0
|
10 |
|
11 | Unless required by applicable law or agreed to in writing, software
|
12 | distributed under the License is distributed on an "AS IS" BASIS,
|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | See the License for the specific language governing permissions and
|
15 | limitations under the License.
|
16 | */
|
17 |
|
18 | /**
|
19 | * @module logger
|
20 | */
|
21 |
|
22 | import 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.
|
30 | const 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.
|
37 | log.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 | */
|
55 | export const logger = log.getLogger(DEFAULT_NAMESPACE);
|
56 | logger.setLevel(log.levels.DEBUG);
|
57 |
|