[Pro] Homey-Betterstack

Hello there!

I’ve developed an NPM package for Homey logging, enabling you to connect to an external monitoring source. Specifically designed for Betterstack Logs, this package facilitates automatic transmission of your Homey logs to Betterstack.

Setting it up is relatively straightforward. Detailed instructions can be found in the post below. (same as readme)

Link:

If you find any issues feel free to make a pull request or issue → Issues · martijnpoppen/homey-betterstack · GitHub

2 Likes

Homey - Betterstack

:mega: 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 :x: :

    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.

3 Likes

Placeholder 1

Placeholder 2

Cool :smiley:

Are there any settings that deal with production vs development? I assume that most developers don’t want logging to be enabled in production versions of their app (also because of possible GDPR issues).

Thanks!
Will look into that, currently development settings are the same as production.

I realised that another thing to check is to see how it handles when Homey has no internet connection, since you don’t want it to slow down (or crash) the app.

1 Like