1/* 2 * Copyright (c) 2023 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 DEBUG, 18 INFO, 19 WARN, 20 ERR, 21} 22 23export class LogLevelUtil { 24 static get(level: string): LogLevel { 25 for (let v = LogLevel.DEBUG; v <= LogLevel.ERR; v++) { 26 if (level === LogLevel[v]) { 27 return v; 28 } 29 } 30 return LogLevel.ERR; 31 } 32} 33 34export class LogUtil { 35 static logLevel: LogLevel = process.env.NODE_ENV === 'development' ? LogLevel.DEBUG : LogLevel.ERR; 36 37 static e(tag: string, message?: any): void { 38 if (LogUtil.logLevel <= LogLevel.ERR) { 39 console.error(`${tag}: ${message}`); 40 } 41 } 42 43 static w(tag: string, message?: any): void { 44 if (LogUtil.logLevel <= LogLevel.WARN) { 45 console.warn(`${tag}: ${message}`); 46 } 47 } 48 49 static i(tag: string, message?: any): void { 50 if (LogUtil.logLevel <= LogLevel.INFO) { 51 console.info(`${tag}: ${message}`); 52 } 53 } 54 55 static d(tag: string, message?: any): void { 56 if (LogUtil.logLevel <= LogLevel.DEBUG) { 57 console.debug(`${tag}: ${message}`); 58 } 59 } 60} 61