Skip to content

Commit

Permalink
FDN-2469: Add default logger for use outside play framework
Browse files Browse the repository at this point in the history
  • Loading branch information
gheine committed Jan 17, 2025
1 parent 6ee333b commit 2ec6d4f
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/main/scala/io/flow/log/RollbarLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package io.flow.log
import cats.data.NonEmptyChain
import com.google.inject.assistedinject.{Assisted, AssistedInject}
import com.rollbar.notifier.Rollbar
import io.flow.util.FlowEnvironment
import net.logstash.logback.marker.Markers.appendEntries
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsValue, Json, Writes}

import scala.jdk.CollectionConverters._
import scala.util.Random
import scala.util.control.NonFatal
import scala.util.{Random, Try}

object RollbarLogger {

Expand All @@ -17,6 +19,31 @@ object RollbarLogger {
val SimpleLogger: RollbarLogger =
RollbarLogger(rollbar = None, attributes = Map.empty, legacyMessage = None, shouldSendToRollbar = false)

private def rollbarToken(): String = {
sys.env
.get("ROLLBAR_TOKEN")
.getOrElse {
sys.error("missing env variable: ROLLBAR_TOKEN")
}
}

lazy val Default: RollbarLogger = {
if (FlowEnvironment.Current == FlowEnvironment.Production) {
Try {
val rollbar = RollbarProvider.rollbar(rollbarToken())
RollbarLogger.SimpleLogger.copy(
rollbar = Some(rollbar),
shouldSendToRollbar = true
)
}.recover { case NonFatal(e) =>
Console.err.println(s"WARN Failed to load Rollbar logger, using simple logger: $e")
RollbarLogger.SimpleLogger
}.get
} else {
RollbarLogger.SimpleLogger
}
}

trait Factory {
@AssistedInject
def rollbar(
Expand Down

0 comments on commit 2ec6d4f

Please sign in to comment.