Show HN: LogLayer v7 now with StatsD support

loglayer.dev

1 points by theogravity 9 hours ago

I'm the author of LogLayer, a TypeScript abstraction layer for logging libraries with the ability to send logs to cloud providers like DataDog.

It's made for outputting structured logs, and I've designed the logging APIs to reflect that:

  log.withMetadata({ userId: 123 })
     .withError(new Error())
     .error("Something's wrong!")
Depending on the logging library you're using with LogLayer, it might look like:

  {
    "msg": "Something's wrong!",
    "metadata": {
      "userId": 123
    },
    "err":{
      "type": "Error",
      "message": "test",
    }
  }
The idea is if you decide the logging library isn't fulfilling your needs, you can easily swap out the library where LogLayer is initialized without having to change the code you've already written around logs.

This weekend, I launched v7 of LogLayer, which adds support for mixins. I wanted to keep LogLayer's API focused solely on logging, but also have the ability to extend it if needed.

The use-case that came up is my workplace wants better metrics. We do have auto-instrumentation for our systems, but there are times where we want more pin-pointed metrics. I've noticed in some cases, metrics and logging goes hand-in-hand, and thought that it'd be really nice if I could access the same logging client and be able to send metrics to StatsD as I write my logs.

In node.js land, I believe "hot-shots" is the StatsD client most would use, and created a mixin with it, which adds the hot-shots methods to the LogLayer API. So now you have the ability to send metrics + logs at the same time if you wanted this functionality:

  log.statsIncrement("error.count")
     .withMetadata({ userId: 123 })
     .withError(new Error())
     .error("Something's wrong!")
  
This will now send an increment call to StatsD in addition to shipping out your log to your logging library / cloud provider.

I'm hoping with this DX, thinking about metrics won't be an afterthought because you now have easy access to send it while writing logs.

(You can also send metrics without sending logs with the mixin.)