1/* 2 * Copyright (c) 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 { getLogger } from 'log4js'; 17 18type ErrorCode = string; 19type ErrorDescription = string; 20 21export class LogDataFactory { 22 static newInstance( 23 code: ErrorCode, 24 description: ErrorDescription 25 ): LogData { 26 return new LogData(code, description); 27 } 28} 29 30class LogData { 31 code: string; 32 description: string; 33 34 constructor( 35 code: ErrorCode, 36 description: string 37 ) { 38 this.code = code; 39 this.description = description; 40 } 41 42 toString(): string { 43 return `ERROR Code: ${this.code} ${this.description}\n`; 44 } 45} 46 47export class IntentLogger { 48 private static instance: IntentLogger; 49 private logger: Object = getLogger('ETS'); 50 51 static getInstance(): IntentLogger { 52 if (!IntentLogger.instance) { 53 IntentLogger.instance = new IntentLogger(); 54 } 55 return IntentLogger.instance; 56 } 57 58 info(...args: string[]): void { 59 this.logger.info(...args); 60 } 61 62 debug(...args: string[]): void { 63 this.logger.debug(...args); 64 } 65 66 warn(...args: string[]): void { 67 this.logger.warn(...args); 68 } 69 70 error(...args: string[]): void { 71 this.logger.error(...args); 72 } 73 74 printError(error: LogData | string): void { 75 if (typeof error === 'string') { 76 this.logger.error(error); 77 } else { 78 this.logger.error(error.toString()); 79 } 80 } 81 82 printErrorAndExit(error: LogData | string): never { 83 const message: string = typeof error === 'string' ? error : error.toString(); 84 throw new Error(message); 85 } 86}