airframe-fluentd

airframe-fluentd is a logging library for sending metrics to Fluentd or Treasure Data

  • Internally it uses Fluency as the fluentd client.

Maven Central

Usage

build.sbt

libraryDependencies += "org.wvlet.airframe" %% "airframe-fluentd" % "(version)"

# If you need to emit logs to Treasure Data, add this dependency as well:
libraryDependencies +=  "org.komamitsu" % "fluency-treasuredata" % "2.0.0"

Sending Data to Fluentd

import wvlet.airframe.fluentd._

// Define a metric class
case class MyMetric(a:Int, b:String) extends TaggedMetric {
  // Used for defining the default tag prefix for this metric.
  // (tagPrefix).(metricTag) will be used as fluentd tag. 
  override def metricTag: String = "my_metric"
}

// Creating a logger to use the local fluentd (host="localhost", port=24224)
// [optional] tagPrefix: common tag prefix for all metrics  
val d = fluentd.withFluendLogger(tagPrefix = "data")

d.build[MetricLoggerFactory] { f =>
   // Create a metric logger for MyMetric class
   val l = f.getTypedLogger[MyMetric]

   l.emit(MyMetric(1, "hello"))   // data.my_metric {"a":1, "b":"hello"}
   l.emit(MyMetric(2, "fluentd")) // data.my_metric {"a":2, "b":"fluentd"}
}

Sending Data to Treasure Data

import wvlet.airframe.fluentd._

// Define a metric class
case class MyMetric(a:Int, b:String) extends TaggedMetric {
  // Specify the table name to store this metric
  override def metricTag: String = "my_metric"
}

// Creating a logger to send log data to Treasure Data Stream Import API:
val d = fluentd.withTDLogger(apikey = "(Your TD API key)",
  tagPrefix = "(database name to store logs)"
)

d.build[MetricLoggerFactory] { f =>
   // Create a metric logger for MyMetric class
   val l = f.getTypedLogger[MyMetric]

   // Metrics will be stored in data.my_mertric table
   l.emit(MyMetric(1, "hello"))   // data.my_metric {"a":1, "b":"hello"}
   l.emit(MyMetric(2, "fluentd")) // data.my_metric {"a":2, "b":"fluentd"}
}

Using Non-Typed Logger

val d = fluentd.withFluendLogger()

d.build[MetricLoggerFactory] { f =>
   val l = f.getLogger
   l.emit("data.my_metric", Map("a"->1, "b"->"hello"))
}

Debugging Metrics

For debugging purpose, use fluentd.withConsoleLogging design:

val d = fluentd.withConsoleLogging // Use a console logger instead of sending logs to Fluentd

d.build[MetricLoggerFactory] { f =>
   val l = f.getTypedLogger[MyMetric]
   l.emit(MyMetric(1, "hello"))   // prints data.my_metric: {"a":1, "b":"hello"}
   l.emit(MyMetric(2, "fluentd")) // prints data.my_metric: {"a":2, "b":"fluentd"}
}