1/* 2 * Copyright (C) 2024 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 */ 15export let LogLevel; 16(function (LogLevel) { 17 LogLevel.OFF = Number.MAX_VALUE; 18 LogLevel.ERROR = 4000; 19 LogLevel.WARN = 3000; 20 LogLevel.INFO = 2000; 21 LogLevel.DEBUG = 1000; 22 LogLevel.TRACE = 500; 23 LogLevel.ALL = Number.MIN_VALUE; 24 })(LogLevel || (LogLevel = {})); 25export const error = (message, ...optionalParams) => { 26 ConsoleLog.logger(LogLevel.ERROR, message, ...optionalParams); 27}; 28export const warn = (message, ...optionalParams) => { 29 ConsoleLog.logger(LogLevel.WARN, message, ...optionalParams); 30}; 31export const info = (message, ...optionalParams) => { 32 ConsoleLog.logger(LogLevel.INFO, message, ...optionalParams); 33}; 34export const debug = (message, ...optionalParams) => { 35 ConsoleLog.logger(LogLevel.DEBUG, message, ...optionalParams); 36}; 37export const trace = (message, ...optionalParams) => { 38 ConsoleLog.logger(LogLevel.TRACE, message, ...optionalParams); 39}; 40export const log = (message) => { 41 ConsoleLog.logger(LogLevel.TRACE, message); 42}; 43 44class ConsoleLog { 45 static getNowLogLevel() { 46 return this.nowLogLevel; 47 } 48 49 static setLogLevel(logLevel) { 50 ConsoleLog.nowLogLevel = logLevel; 51 } 52 53 static now() { 54 const now = new Date(); 55 const timeString = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}:${now.getMilliseconds()}`; 56 return timeString; 57 } 58 59 static logger(logLevel, message, ...optionalParams) { 60 if (logLevel >= ConsoleLog.nowLogLevel) { 61 switch (logLevel) { 62 case LogLevel.ERROR: 63 console.error(message, ...optionalParams, this.now()); 64 break; 65 case LogLevel.WARN: 66 console.warn(message, ...optionalParams, this.now()); 67 break; 68 case LogLevel.INFO: 69 console.info(message, ...optionalParams, this.now()); 70 break; 71 case LogLevel.DEBUG: 72 console.debug(message, ...optionalParams, this.now()); 73 break; 74 case LogLevel.TRACE: 75 console.trace(message, ...optionalParams, this.now()); 76 break; 77 default: 78 console.log(message, this.now()); 79 } 80 } 81 } 82} 83 84ConsoleLog.nowLogLevel = LogLevel.OFF; 85