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 16import Logger from '@ohos.hilog'; 17 18/** 19 * 日志打印工具 20 * 21 * @since 2022-06-06 22 */ 23export namespace LogUtils { 24 const DOMAIN = 0x0A00; 25 26 /** 27 * 输出debug日志 28 * 29 * @param tag 标题 30 * @param message 日志信息 31 * @param args 附加信息 32 */ 33 export function debug(tag: string, message: string, ...args: (string | number)[]): void { 34 Logger.debug(DOMAIN, tag, filterSensitiveInfo(message), ...args); 35 } 36 37 /** 38 * 输出info日志 39 * 40 * @param tag 标题 41 * @param message 日志信息 42 * @param args 附加信息 43 */ 44 export function log(tag: string, message: string, ...args: (string | number)[]): void { 45 Logger.info(DOMAIN, tag, filterSensitiveInfo(message), ...args); 46 } 47 48 /** 49 * 输出info日志 50 * 51 * @param tag 标题 52 * @param message 日志信息 53 * @param args 附加信息 54 */ 55 export function info(tag: string, message: string, ...args: (string | number)[]): void { 56 Logger.info(DOMAIN, tag, filterSensitiveInfo(message), ...args); 57 } 58 59 /** 60 * 输出warn日志 61 * 62 * @param tag 标题 63 * @param message 日志信息 64 * @param args 附加信息 65 */ 66 export function warn(tag: string, message: string, ...args: (string | number)[]): void { 67 Logger.warn(DOMAIN, tag, filterSensitiveInfo(message), ...args); 68 } 69 70 /** 71 * 输出error日志 72 * 73 * @param tag 标题 74 * @param message 日志信息 75 * @param args 附加信息 76 */ 77 export function error(tag: string, message: string, ...args: (string | number)[]): void { 78 Logger.error(DOMAIN, tag, filterSensitiveInfo(message), ...args); 79 } 80 81 function filterSensitiveInfo(message: string): string { 82 let result: string = message; 83 if (result) { 84 result = filterUrl(result, true); 85 result = filterUrl(result, false); 86 } 87 return result; 88 } 89 90 function filterUrl(message: string, isHttps: boolean): string { 91 let replaceStr: string = isHttps ? 'https://' : 'http://'; 92 let result: string = ''; 93 let tempResult: string = message; 94 let startIndex: number = tempResult.indexOf(replaceStr); 95 while (startIndex >= 0) { 96 result += tempResult.substring(0, startIndex) + replaceStr + '****'; 97 tempResult = tempResult.substring(startIndex + replaceStr.length); 98 let endIndex: number = tempResult.indexOf('/'); 99 tempResult = endIndex >= 0 ? tempResult.substring(endIndex) : ''; 100 startIndex = tempResult.indexOf(replaceStr); 101 } 102 result += tempResult; 103 return result; 104 } 105}