1/* 2 * Copyright (c) 2021-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 */ 15import hilog from '@ohos.hilog'; 16 17const DOMAIN: number = 0x300A; 18const FILTER_KEYS = [ 19 new RegExp('hide', "gi") 20] 21 22export function filterKey(target: any, propKey: string, descriptor: PropertyDescriptor) { 23 const original = descriptor.value; 24 descriptor.value = function (...args: string[]) { 25 let filterResult = args.map((str) => { 26 let tempStr = str 27 FILTER_KEYS.forEach((filterKey) => tempStr = tempStr.replace(filterKey, "**")) 28 return tempStr 29 }); 30 const result = original.call(this, ...filterResult); 31 return result; 32 }; 33} 34 35/** 36 * Basic log class 37 */ 38export default class Log { 39 /** 40 * Outputs debug-level logs. 41 * 42 * @param tag Identifies the log tag. 43 * @param format Indicates the log format string. 44 * @param args Indicates the log parameters. 45 * @since 7 46 */ 47 static showDebug(tag: string, format: string, ...args: any[]) { 48 if (Log.isLoggable(tag, hilog.LogLevel.DEBUG)) { 49 hilog.debug(DOMAIN, tag, format, args); 50 } 51 } 52 53 /** 54 * Outputs info-level logs. 55 * 56 * @param tag Identifies the log tag. 57 * @param format Indicates the log format string. 58 * @param args Indicates the log parameters. 59 * @since 7 60 */ 61 static showInfo(tag: string, format: string, ...args: any[]) { 62 if (Log.isLoggable(tag, hilog.LogLevel.INFO)) { 63 hilog.info(DOMAIN, tag, format, args); 64 } 65 } 66 67 /** 68 * Outputs warning-level logs. 69 * 70 * @param tag Identifies the log tag. 71 * @param format Indicates the log format string. 72 * @param args Indicates the log parameters. 73 * @since 7 74 */ 75 static showWarn(tag: string, format: string, ...args: any[]) { 76 if (Log.isLoggable(tag, hilog.LogLevel.WARN)) { 77 hilog.warn(DOMAIN, tag, format, args); 78 } 79 } 80 81 /** 82 * Outputs error-level logs. 83 * 84 * @param tag Identifies the log tag. 85 * @param format Indicates the log format string. 86 * @param args Indicates the log parameters. 87 * @since 7 88 */ 89 static showError(tag: string, format: string, ...args: any[]) { 90 if (Log.isLoggable(tag, hilog.LogLevel.ERROR)) { 91 hilog.error(DOMAIN, tag, format, args); 92 } 93 } 94 95 /** 96 * Outputs fatal-level logs. 97 * 98 * @param tag Identifies the log tag. 99 * @param format Indicates the log format string. 100 * @param args Indicates the log parameters. 101 * @since 7 102 */ 103 static showFatal(tag: string, format: string, ...args: any[]) { 104 if (Log.isLoggable(tag, hilog.LogLevel.FATAL)) { 105 hilog.fatal(DOMAIN, tag, format, args); 106 } 107 } 108 109 /** 110 * Checks whether logs of the specified tag, and level can be printed. 111 * 112 * @param tag Identifies the log tag. 113 * @param level log level 114 * @since 7 115 */ 116 private static isLoggable(tag: string, level: hilog.LogLevel): boolean { 117 return hilog.isLoggable(DOMAIN, tag, level); 118 } 119} 120