HarmonyOS NEXT
.封装Logger工具
1.第一步
/**
* 1. 封装一个Logger工具类
* 2. 工具类提供一系列的方法(info, debug, error, warn)
* 3. 导出一个实例对象, 使用处通过实例对象调用方法
*/
import { hilog } from "@kit.PerformanceAnalysisKit";
const PREFIX = 'interview_tong_logger'
class Logger {
private domain: number;
private prefix: string;
private format: string = '%{public}s, %{public}s';
constructor(domain: number, prefix: string) {
this.domain = domain
this.prefix = prefix
}
debug(...args: string[]): void {
hilog.debug(this.domain, this.prefix, this.format, args);
}
info(...args: string[]): void {
hilog.info(this.domain, this.prefix, this.format, args);
}
warn(...args: string[]): void {
hilog.warn(this.domain, this.prefix, this.format, args);
}
error(...args: string[]): void {
hilog.error(this.domain, this.prefix, this.format, args);
}
}
export const logger = new Logger(0x1234, PREFIX)
\ No newline at end of file
2.第二步

3.第三步
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { logger } from '../commons/utils';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
logger.info('Ability onCreate')
}
onDestroy(): void {
logger.info('Ability onDestroy')
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
logger.info('Ability onWindowStageCreate')
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
logger.error(JSON.stringify(err))
return;
}
logger.info('Succeeded in loading the content.')
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
logger.info('Ability onWindowStageDestroy')
}
onForeground(): void {
// Ability has brought to foreground
logger.info('Ability onForeground')
}
onBackground(): void {
// Ability has back to background
logger.info('Ability onBackground')
}
}
4.第四步测试
import { fullScreen } from '../commons/utils/FullScreen';
@Entry
@Component
struct Index {
@StorageProp('topHeight') topHeight: number = 0
@StorageProp('bottomHeight') bottomHeight: number = 0
aboutToAppear(): void {
setTimeout(() => {
fullScreen.disable()
}, 3000)
}
build() {
Column() {
Button('TOP')
Blank()
Button('Bottom')
}
.backgroundColor(Color.Pink)
.height('100%')
.width('100%')
.padding({ top: this.topHeight, bottom: this.bottomHeight })
}
}
1076

被折叠的 条评论
为什么被折叠?



