1. 创建sharedLibrary共享hsp包commonLib:功能工具类。

import hilog from '@ohos.hilog';

class Logger {
  private DOMAIN: number = 0x0000;
  private APP_NAME: string = 'Myapplication';

  public info(tag: string, ...args: string[]) {
    hilog.info(this.DOMAIN, `[${this.APP_NAME}.${tag}]`, '%{public}s', args.join(''));
  }
  public error(tag: string, ...args: string[]) {
    hilog.error(this.DOMAIN, `[${this.APP_NAME}.${tag}]`, '%{public}s', args.join(''));
  }
}

export const logger = new Logger();
  • commonLib导出logger类:
export { logger } from "./utils/Logger";
  • 其他模块引入commonLib:
 "devDependencies": {
    "@ohos/commonLib": "file:./commonLib"
  }
  • 其他模块使用logger:
import { logger } from '@ohos/commonLib';
logger.info(TAG, 'Ability onCreate');
12-13 13:47