DEV Community

RogueYBJ
RogueYBJ

Posted on

Harmony-Free Hengong Log Console Outputs Logs

Hello everyone, welcome! Today, we will talk about the commonly used log logging in HarmonyOS. During the development process, logging is basically indispensable. Logging can help us quickly locate and identify problems. However, sometimes during the development process, the testers who show the bugs on their phones cannot directly locate the problems even when the developers are present. It would be great if the console logging could be displayed on the phone, right? Today, we will discuss how to use the AOP aspect capabilities provided by the Aspect tool class to implement monitoring and logging, as well as how to open the console log output page by shaking the phone.

一、Monitor and print logs through the AOP aspect capabilities of the Aspect tool class

1、Add the "logAfter" monitoring method to the log printing method

util.Aspect.addAfter(console, 'log', true, this.logAfter)
Enter fullscreen mode Exit fullscreen mode

2、The logAfter monitoring method records the log entries into the logList

logAfter: (instance: console, method: string, arg: string, ...args: object[]) => void  = (instance: console, method: string, arg: string, ...args: object[]) => {
      let msg = arg
      if (args != undefined) {
        args.forEach((a) => {
          msg += JSON.stringify(a)
        })
      }
      this.logList.push(new LogModel(msg))
    }
Enter fullscreen mode Exit fullscreen mode

二、Open the console output log page by shaking the mobile phone.

1、Configure the permissions for the mobile phone sensors

2、Monitor the mobile phone sensors and open the console output log page

sensors = sensor.getSensorListSync()
// Start listening
open() {
    if (this.findSensor(sensor.SensorId.ACCELEROMETER)){
      try {
        sensor.on(sensor.SensorId.ACCELEROMETER, this.action.bind(this));
      } catch (error) {
        let e: BusinessError = error as BusinessError;
        console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
      }
    }else{
      console.error(`not find id with ${sensor.SensorId.ACCELEROMETER}`);
    }
}
// Turn off the listening function
close(){
    try {
      sensor.off(sensor.SensorId.ACCELEROMETER, this.action.bind(this));
    } catch (error) {
      let e: BusinessError = error as BusinessError;
      console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
    }
}
// Open the console log page
action(data: sensor.GyroscopeResponse){
    if ((data.x > this.num || data.y > this.num || data.z > this.num) && (this.isOn)) {
      this.isOn = false
      if (this.type === "router"){
        router.pushNamedRoute({name:"log"})
      }else {
        nav.push("log").then(() => {
          this.isOn = true
        })
      }
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: The complete code has been submitted toHongmeng Tripartite Library,Use the following command to install.

ohpm install @free/log
Enter fullscreen mode Exit fullscreen mode

The calling method: The onCreate and onDestroy methods in the EntryAbility class respectively invoke the following two methods

// Start log monitoring
Log.open()
// Disable log monitoring
Log.close()
Enter fullscreen mode Exit fullscreen mode

If you like this content, please give a little heart!

Top comments (0)