• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export enum LogLevel {
17  OFF = Number.MAX_VALUE,
18  ERROR = 4000,
19  WARN = 3000,
20  INFO = 2000,
21  DEBUG = 1000,
22  TRACE = 500,
23  ALL = Number.MIN_VALUE,
24}
25
26export const error = (message?: any, ...optionalParams: any[]) => {
27  SpLog.logger(LogLevel.ERROR, message, ...optionalParams);
28};
29export const warn = (message?: any, ...optionalParams: any[]) => {
30  SpLog.logger(LogLevel.WARN, message, ...optionalParams);
31};
32export const info = (message?: any, ...optionalParams: any[]) => {
33  SpLog.logger(LogLevel.INFO, message, ...optionalParams);
34};
35export const debug = (message?: any, ...optionalParams: any[]) => {
36  SpLog.logger(LogLevel.DEBUG, message, ...optionalParams);
37};
38export const trace = (message?: any, ...optionalParams: any[]) => {
39  SpLog.logger(LogLevel.TRACE, message, ...optionalParams);
40};
41export const log = (message?: any) => {
42  SpLog.logger(LogLevel.TRACE, message);
43};
44
45class SpLog {
46  private static nowLogLevel: LogLevel = LogLevel.OFF;
47
48  public static getNowLogLevel(): LogLevel {
49    return this.nowLogLevel;
50  }
51
52  public static setLogLevel(logLevel: LogLevel) {
53    SpLog.nowLogLevel = logLevel;
54  }
55
56  public static logger(logLevel: LogLevel, message?: any, ...optionalParams: any[]) {
57    if (logLevel >= SpLog.nowLogLevel) {
58      switch (logLevel) {
59        case LogLevel.ERROR:
60          console.error(message, ...optionalParams);
61          break;
62        case LogLevel.WARN:
63          console.warn(message, ...optionalParams);
64          break;
65        case LogLevel.INFO:
66          console.info(message, ...optionalParams);
67          break;
68        case LogLevel.DEBUG:
69          console.debug(message, ...optionalParams);
70          break;
71        case LogLevel.TRACE:
72          console.trace(message, ...optionalParams);
73          break;
74        default:
75          console.log(message);
76      }
77    }
78  }
79}
80