Skip to content

martijnpoppen/homey-betterstack

Repository files navigation

Homey - Betterstack

📣 Logtail is now part of Better Stack. Learn more ⇗

Better Stack dashboard

Introduction

This package provides integration between your Homey app and Betterstack logging service. By using this package, you can easily log events, errors, and other information from your Homey app to Betterstack for monitoring and analysis.

Setup

  1. Create an account and project on Betterstack.
  2. Obtain the source_token provided by Betterstack.
  3. Add the source_token as HOMEY_BETTERSTACK_TOKEN in your env.json file.

Installation

You can install the package via npm:

npm install homey-betterstack

or

yarn add homey-betterstack

Usage

Import the package

const HomeyLog = require("homey-betterstack");



Extend the app class

Edit the app class to extend HomeyLog:

//From:
class App extends Homey.App {
// To
class App extends HomeyLog {



Logging from app.js

Use this.log or this.error etc within your app.js:

this.trace("Event occurred");
this.debug("Event occurred");
this.info("Event occurred"); // log is the same as info
this.log("Event occurred"); // log is the same as info
this.warn("Warning occurred", error);
this.error("Error occurred", error);
this.fatal("Fatal error occurred", error);



Logging from devices and drivers

Use this.homey.app.log etc from within your devices and drivers:

this.homey.app.trace("Event occurred in device/driver");
this.homey.app.debug("Event occurred in device/driver");
this.homey.app.log("Event occurred in device/driver"); // log is the same as info
this.homey.app.log("Event occurred in device/driver"); // log is the same as info
this.homey.app.warn("Warning occurred in device/driver", error);
this.homey.app.error("Error occurred in device/driver", error);
this.homey.app.fatal("Fatal error occurred in device/driver", error);



Note:

Make sure to remove any log functions in your app as they will overwrite the log functionality of Homey-Betterstack

Not allowed ❌ :

    log() {
        console.log.bind(this, '[log]').apply(this, arguments);
    }

    error() {
        console.error.bind(this, '[error]').apply(this, arguments);
    }



Example

class App extends HomeyLog {
  async onInit() {
    try {
      this.log(`${Homey.manifest.id} - ${Homey.manifest.version} started...`);
    } catch (error) {
      this.homey.app.log(error);
    }
  }
}

module.exports = App;



Result

In the terminal:

[info] com.test.app - 0.0.1 started...

In Betterstack:



Configuration

Change Log Level

It is possible to alter the loglevels and change the enable/disable the output to console/betterstack. Add the following to your Homey app env.json:

    "HOMEY_BETTERSTACK_CONFIG": {
        "console_enabled": "1",
        "publish_enabled": "1",
        "console_level": "info",
        "publish_level": "info"
    }

Above values are also the default values of the app.

Log Levels

The following log levels are available:

    trace: 6,
    debug: 5,
    info: 4,
    warn: 3,
    error: 2,
    fatal: 1

E.G: When setting the level to warn everything below warn will be logged.