19b19030aSopenharmony_ci/*
29b19030aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
39b19030aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49b19030aSopenharmony_ci * you may not use this file except in compliance with the License.
59b19030aSopenharmony_ci * You may obtain a copy of the License at
69b19030aSopenharmony_ci *
79b19030aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89b19030aSopenharmony_ci *
99b19030aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109b19030aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119b19030aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129b19030aSopenharmony_ci * See the License for the specific language governing permissions and
139b19030aSopenharmony_ci * limitations under the License.
149b19030aSopenharmony_ci */
159b19030aSopenharmony_ci
169b19030aSopenharmony_ciimport Logger from '@ohos.hilog';
179b19030aSopenharmony_ci
189b19030aSopenharmony_ci/**
199b19030aSopenharmony_ci * 日志打印工具
209b19030aSopenharmony_ci *
219b19030aSopenharmony_ci * @since 2022-06-06
229b19030aSopenharmony_ci */
239b19030aSopenharmony_ciexport namespace LogUtils {
249b19030aSopenharmony_ci  const DOMAIN = 0x0A00;
259b19030aSopenharmony_ci
269b19030aSopenharmony_ci  /**
279b19030aSopenharmony_ci   * 输出debug日志
289b19030aSopenharmony_ci   *
299b19030aSopenharmony_ci   * @param tag 标题
309b19030aSopenharmony_ci   * @param message 日志信息
319b19030aSopenharmony_ci   * @param args 附加信息
329b19030aSopenharmony_ci   */
339b19030aSopenharmony_ci  export function debug(tag: string, message: string, ...args: (string | number)[]): void {
349b19030aSopenharmony_ci    Logger.debug(DOMAIN, tag, filterSensitiveInfo(message), ...args);
359b19030aSopenharmony_ci  }
369b19030aSopenharmony_ci
379b19030aSopenharmony_ci  /**
389b19030aSopenharmony_ci   * 输出info日志
399b19030aSopenharmony_ci   *
409b19030aSopenharmony_ci   * @param tag 标题
419b19030aSopenharmony_ci   * @param message 日志信息
429b19030aSopenharmony_ci   * @param args 附加信息
439b19030aSopenharmony_ci   */
449b19030aSopenharmony_ci  export function log(tag: string, message: string, ...args: (string | number)[]): void {
459b19030aSopenharmony_ci    Logger.info(DOMAIN, tag, filterSensitiveInfo(message), ...args);
469b19030aSopenharmony_ci  }
479b19030aSopenharmony_ci
489b19030aSopenharmony_ci  /**
499b19030aSopenharmony_ci   * 输出info日志
509b19030aSopenharmony_ci   *
519b19030aSopenharmony_ci   * @param tag 标题
529b19030aSopenharmony_ci   * @param message 日志信息
539b19030aSopenharmony_ci   * @param args 附加信息
549b19030aSopenharmony_ci   */
559b19030aSopenharmony_ci  export function info(tag: string, message: string, ...args: (string | number)[]): void {
569b19030aSopenharmony_ci    Logger.info(DOMAIN, tag, filterSensitiveInfo(message), ...args);
579b19030aSopenharmony_ci  }
589b19030aSopenharmony_ci
599b19030aSopenharmony_ci  /**
609b19030aSopenharmony_ci   * 输出warn日志
619b19030aSopenharmony_ci   *
629b19030aSopenharmony_ci   * @param tag 标题
639b19030aSopenharmony_ci   * @param message 日志信息
649b19030aSopenharmony_ci   * @param args 附加信息
659b19030aSopenharmony_ci   */
669b19030aSopenharmony_ci  export function warn(tag: string, message: string, ...args: (string | number)[]): void {
679b19030aSopenharmony_ci    Logger.warn(DOMAIN, tag, filterSensitiveInfo(message), ...args);
689b19030aSopenharmony_ci  }
699b19030aSopenharmony_ci
709b19030aSopenharmony_ci  /**
719b19030aSopenharmony_ci   * 输出error日志
729b19030aSopenharmony_ci   *
739b19030aSopenharmony_ci   * @param tag 标题
749b19030aSopenharmony_ci   * @param message 日志信息
759b19030aSopenharmony_ci   * @param args 附加信息
769b19030aSopenharmony_ci   */
779b19030aSopenharmony_ci  export function error(tag: string, message: string, ...args: (string | number)[]): void {
789b19030aSopenharmony_ci    Logger.error(DOMAIN, tag, filterSensitiveInfo(message), ...args);
799b19030aSopenharmony_ci  }
809b19030aSopenharmony_ci
819b19030aSopenharmony_ci  function filterSensitiveInfo(message: string): string {
829b19030aSopenharmony_ci    let result: string = message;
839b19030aSopenharmony_ci    if (result) {
849b19030aSopenharmony_ci      result = filterUrl(result, true);
859b19030aSopenharmony_ci      result = filterUrl(result, false);
869b19030aSopenharmony_ci    }
879b19030aSopenharmony_ci    return result;
889b19030aSopenharmony_ci  }
899b19030aSopenharmony_ci
909b19030aSopenharmony_ci  function filterUrl(message: string, isHttps: boolean): string {
919b19030aSopenharmony_ci    let replaceStr: string = isHttps ? 'https://' : 'http://';
929b19030aSopenharmony_ci    let result: string = '';
939b19030aSopenharmony_ci    let tempResult: string = message;
949b19030aSopenharmony_ci    let startIndex: number = tempResult.indexOf(replaceStr);
959b19030aSopenharmony_ci    while (startIndex >= 0) {
969b19030aSopenharmony_ci      result += tempResult.substring(0, startIndex) + replaceStr + '****';
979b19030aSopenharmony_ci      tempResult = tempResult.substring(startIndex + replaceStr.length);
989b19030aSopenharmony_ci      let endIndex: number = tempResult.indexOf('/');
999b19030aSopenharmony_ci      tempResult = endIndex >= 0 ? tempResult.substring(endIndex) : '';
1009b19030aSopenharmony_ci      startIndex = tempResult.indexOf(replaceStr);
1019b19030aSopenharmony_ci    }
1029b19030aSopenharmony_ci    result += tempResult;
1039b19030aSopenharmony_ci    return result;
1049b19030aSopenharmony_ci  }
1059b19030aSopenharmony_ci}