• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16import hilog from '@ohos.hilog';
17import { BusinessError } from '@kit.BasicServicesKit';
18
19/**
20 * 日志级别
21 */
22const enum LogVersion {
23  Debug = 1,
24  Info = 2,
25  Warn = 3,
26  Error = 4,
27  Fatal = 5,
28}
29
30export class HiLog {
31  /**
32   * 日志级别, 注意在LogVersion声明之后
33   */
34  public static readonly LOG_LEVEL = LogVersion.Debug;
35
36  /**
37   * TAG
38   */
39  public static APP_TAG = 'DLPManager';
40
41  /**
42   * Service Domain
43   */
44  public static readonly LOG_DOMAIN = 0xf888;
45
46  /**
47   * 打印 Debug 日志
48   * @param tag 日志Tag
49   * @param message 打印信息
50   */
51  public static debug(tag: string, message: string): void {
52    if (LogVersion.Debug >= HiLog.LOG_LEVEL) {
53      hilog.debug(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, message);
54    }
55  }
56
57  /**
58   * 打印 Info 日志
59   * @param tag 日志Tag
60   * @param message 打印信息
61   */
62  public static info(tag: string, message: string): void {
63    if (LogVersion.Info >= HiLog.LOG_LEVEL) {
64      hilog.info(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, message);
65    }
66  }
67
68  /**
69   * 打印 Warn 日志
70   * @param tag 日志Tag
71   * @param message 打印信息
72   */
73  public static warn(tag: string, message: string): void {
74    if (LogVersion.Warn >= HiLog.LOG_LEVEL) {
75      hilog.warn(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, message);
76    }
77  }
78
79  /**
80   * 打印 Error 日志
81   * @param tag 日志Tag
82   * @param message 打印信息
83   */
84  public static error(tag: string, message: string): void {
85    if (LogVersion.Error >= HiLog.LOG_LEVEL) {
86      hilog.error(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, message);
87    }
88  }
89
90  /**
91   * 打印 Fatal 日志
92   * @param tag 日志Tag
93   * @param message 打印信息
94   */
95  public static fatal(tag: string, message: string): void {
96    if (LogVersion.Fatal >= HiLog.LOG_LEVEL) {
97      hilog.fatal(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, message);
98    }
99  }
100
101  /**
102   * 打印 Error 日志
103   * @param tag 日志Tag
104   * @param message 打印信息
105   */
106  public static wrapError(tag: string, error: Error, msg: string): void {
107    const busErr: BusinessError = error as BusinessError;
108    hilog.error(HiLog.LOG_DOMAIN, `[${HiLog.APP_TAG}_${tag}]`, msg + `, Code is ${busErr.code},
109    message is ${busErr.message}}, data is ${busErr.data}`);
110  }
111}