• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 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 type { Logger } from 'log4js';
17import { configure, getLogger } from 'log4js';
18
19export enum LOG_LEVEL {
20    ERROR = 'ERROR',
21    WARN = 'WARN',
22    INFO = 'INFO',
23    DEBUG = 'DEBUG',
24    TRACE = 'TRACE',
25}
26
27export enum LOG_MODULE_TYPE {
28    DEFAULT = 'default',
29    ARKANALYZER = 'ArkAnalyzer',
30    HOMECHECK = 'HomeCheck',
31    TOOL = 'Tool',
32}
33
34export default class ConsoleLogger {
35    public static configure(
36        logFilePath: string,
37        arkanalyzer_level: LOG_LEVEL = LOG_LEVEL.ERROR,
38        tool_level: LOG_LEVEL = LOG_LEVEL.INFO,
39        use_console: boolean = false
40    ): void {
41        let appendersTypes: string[] = [];
42        if (logFilePath) {
43            appendersTypes.push('file');
44        }
45        if (!appendersTypes.length || use_console) {
46            appendersTypes.push('console');
47        }
48        configure({
49            appenders: {
50                file: {
51                    type: 'fileSync',
52                    filename: `${logFilePath}`,
53                    maxLogSize: 5 * 1024 * 1024,
54                    backups: 5,
55                    compress: true,
56                    encoding: 'utf-8',
57                    layout: {
58                        type: 'pattern',
59                        pattern: '[%d] [%p] [%z] [%X{module}] - [%X{tag}] %m',
60                    },
61                },
62                console: {
63                    type: 'console',
64                    layout: {
65                        type: 'pattern',
66                        pattern: '[%d] [%p] [%z] [%X{module}] - [%X{tag}] %m',
67                    },
68                },
69            },
70            categories: {
71                default: {
72                    appenders: ['console'],
73                    level: 'info',
74                    enableCallStack: false,
75                },
76                ArkAnalyzer: {
77                    appenders: appendersTypes,
78                    level: arkanalyzer_level,
79                    enableCallStack: true,
80                },
81                Tool: {
82                    appenders: appendersTypes,
83                    level: tool_level,
84                    enableCallStack: true,
85                },
86            },
87        });
88    }
89
90    public static getLogger(log_type: LOG_MODULE_TYPE, tag: string = '-'): Logger {
91        let logger;
92        if (log_type === LOG_MODULE_TYPE.DEFAULT || log_type === LOG_MODULE_TYPE.ARKANALYZER) {
93            logger = getLogger(log_type);
94        } else {
95            logger = getLogger(LOG_MODULE_TYPE.TOOL);
96        }
97        logger.addContext('module', log_type);
98        logger.addContext('tag', tag);
99        return logger;
100    }
101}
102