Skip to content

Commit

Permalink
FIN-1544: Standard scalafmt settings (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek authored Nov 1, 2023
1 parent 5cf4086 commit cdaef37
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 46 deletions.
9 changes: 9 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version = 3.5.9
runner.dialect=scala213
maxColumn = 120
continuationIndent.callSite = 2
continuationIndent.defnSite = 2
continuationIndent.ctorSite = 2
continuationIndent.extendSite = 2
align.preset = none
project.excludePaths = [ "glob:**/generated/**" ]
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pipeline {
script {
try {
sh '''
sbt clean compile flowLintLib test doc
sbt clean compile flowLintLib test scalafmtSbtCheck scalafmtCheck doc
'''
} finally {
junit allowEmptyResults: true, testResults: '**/target/test-reports/*.xml'
Expand Down
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ lazy val allScalacOptions = Seq(
"-unchecked",
"-Xcheckinit",
"-Xlint:adapted-args",
"-Ypatmat-exhaust-depth", "100", // Fixes: Exhaustivity analysis reached max recursion depth, not all missing cases are reported.
"-Ypatmat-exhaust-depth",
"100", // Fixes: Exhaustivity analysis reached max recursion depth, not all missing cases are reported.
"-Wconf:src=generated/.*:silent",
"-Wconf:src=target/.*:silent", // silence the unused imports errors generated by the Play Routes
"-release:17",
"-release:17"
)

libraryDependencies ++= Seq(
Expand All @@ -31,7 +32,7 @@ libraryDependencies ++= Seq(
"net.logstash.logback" % "logstash-logback-encoder" % "6.3", // structured logging to sumo
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"com.typesafe.play" %% "play-json" % "2.9.4",
"com.typesafe.play" %% "play-json-joda" % "2.9.4",
"com.typesafe.play" %% "play-json-joda" % "2.9.4"
)

resolvers += "Artifactory" at "https://flow.jfrog.io/flow/libs-release/"
Expand All @@ -56,3 +57,4 @@ publishTo := {
}

scalacOptions ++= allScalacOptions
scalafmtOnCompile := true
7 changes: 4 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ resolvers += "Flow Plugins" at "https://flow.jfrog.io/flow/plugins-release/"
addSbtPlugin("io.flow" % "sbt-flow-linter" % "0.0.41")

// Resolve scala-xml version dependency mismatch, see https://github.com/sbt/sbt/issues/7007
ThisBuild / libraryDependencySchemes ++= Seq(
"org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always
)
ThisBuild / libraryDependencySchemes ++= Seq(
"org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always
)

addSbtPlugin("com.github.sbt" % "sbt-git" % "2.0.1")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
16 changes: 8 additions & 8 deletions src/main/scala/io/flow/log/LogUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class LogUtil @Inject() (logger: RollbarLogger) {

import RollbarLogger.Keys

/**
* @param frequency Log once per frequency. For instance, 100 means that the duration will be logged once every 100
* calls on average.
/** @param frequency
* Log once per frequency. For instance, 100 means that the duration will be logged once every 100 calls on
* average.
*/
def duration[T](
info: String,
Expand All @@ -22,7 +22,7 @@ class LogUtil @Inject() (logger: RollbarLogger) {
orderNumber: Option[String] = None,
requestId: Option[String] = None,
data: Option[Map[String, String]] = None,
frequency: Long = 1L,
frequency: Long = 1L
)(f: => T): T = {
if (shouldLog(frequency)) {
val start = System.currentTimeMillis()
Expand All @@ -47,9 +47,9 @@ class LogUtil @Inject() (logger: RollbarLogger) {
f
}

/**
* @param frequency Log once per frequency. For instance, 100 means that the duration will be logged once every 100
* calls on average.
/** @param frequency
* Log once per frequency. For instance, 100 means that the duration will be logged once every 100 calls on
* average.
*/
def durationF[T](
info: String,
Expand All @@ -60,7 +60,7 @@ class LogUtil @Inject() (logger: RollbarLogger) {
orderNumber: Option[String] = None,
requestId: Option[String] = None,
data: Option[Map[String, String]] = None,
frequency: Long = 1L,
frequency: Long = 1L
)(f: => Future[T])(implicit ec: ExecutionContext): Future[T] = {
if (shouldLog(frequency)) {
val start = System.currentTimeMillis()
Expand Down
17 changes: 10 additions & 7 deletions src/main/scala/io/flow/log/Rollbar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class RollbarModule extends AbstractModule with ScalaModule {
}
}

/**
* We use a provider because we don't want to keep initializing a new Rollbar notifier
* every time an instance of the logger is instantiated.
/** We use a provider because we don't want to keep initializing a new Rollbar notifier every time an instance of the
* logger is instantiated.
*
* Returns an optional Rollbar instance so a key isn't required in test and development mode.
*/
Expand All @@ -52,7 +51,7 @@ class RollbarLoggerProvider @Inject() (
// Necessary evil to allow us to copy instances of RollbarLogger, letting us have
// nice methods like `withKeyValue`
@Singleton
class RollbarFactory @Inject()(
class RollbarFactory @Inject() (
rollbarProvider: Provider[Option[Rollbar]]
) extends RollbarLogger.Factory {
@AssistedInject
Expand Down Expand Up @@ -132,9 +131,13 @@ object RollbarProvider {

resultOpt match {
case JsSuccess(res, _) => res
case JsError(errors) => new Result.Builder().code(-1).body(
JsError.toJson(errors).toString
).build
case JsError(errors) =>
new Result.Builder()
.code(-1)
.body(
JsError.toJson(errors).toString
)
.build
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions src/main/scala/io/flow/log/RollbarLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import scala.util.Random

object RollbarLogger {

/**
* Simple [[RollbarLogger]] that does not log to Rollbar.
* Useful for tests requiring a [[RollbarLogger]].
/** Simple [[RollbarLogger]] that does not log to Rollbar. Useful for tests requiring a [[RollbarLogger]].
*/
val SimpleLogger: RollbarLogger =
RollbarLogger(rollbar = None, attributes = Map.empty, legacyMessage = None, shouldSendToRollbar = false)

trait Factory {
@AssistedInject
def rollbar(attributes: Map[String, JsValue], legacyMessage: Option[String], shouldSendToRollbar: Boolean, frequency: Long): RollbarLogger
def rollbar(
attributes: Map[String, JsValue],
legacyMessage: Option[String],
shouldSendToRollbar: Boolean,
frequency: Long
): RollbarLogger
}

object Keys {
Expand Down Expand Up @@ -52,14 +55,13 @@ case class RollbarLogger @AssistedInject() (

private val logger = LoggerFactory.getLogger("application")

/**
* Log once per frequency.
* For instance, 100 means that the message will be logged once every 100 calls on average.
/** Log once per frequency. For instance, 100 means that the message will be logged once every 100 calls on average.
*/
def withFrequency(frequency: Long): RollbarLogger = this.copy(frequency = frequency)

def withKeyValue[T: Writes](keyValue: (String, T)): RollbarLogger = withKeyValue(keyValue._1, keyValue._2)
def withKeyValue[T: Writes](key: String, value: T): RollbarLogger = this.copy(attributes = attributes + (key -> Json.toJson(value)))
def withKeyValue[T: Writes](key: String, value: T): RollbarLogger =
this.copy(attributes = attributes + (key -> Json.toJson(value)))
def fingerprint(value: String): RollbarLogger = withKeyValue(Keys.Fingerprint, value)
def organization(value: String): RollbarLogger = withKeyValue(Keys.Organization, value)
def channelId(value: String): RollbarLogger = withKeyValue(Keys.ChannelId, value)
Expand All @@ -69,28 +71,25 @@ case class RollbarLogger @AssistedInject() (
def itemNumber(value: String): RollbarLogger = withKeyValue(Keys.ItemNumber, value)
def experienceKey(value: String): RollbarLogger = withKeyValue(Keys.ExperienceKey, value)

/**
* Use for warnings or errors that:
* - are very high volume
* - should be recorded for audit purposes but no action needs to be taken
/** Use for warnings or errors that:
* - are very high volume
* - should be recorded for audit purposes but no action needs to be taken
*
* Structured errors will still be sent to Sumo.
*/
def withSendToRollbar(sendToRollbar:Boolean): RollbarLogger = this.copy(shouldSendToRollbar = sendToRollbar)
def withSendToRollbar(sendToRollbar: Boolean): RollbarLogger = this.copy(shouldSendToRollbar = sendToRollbar)

def withKeyValues[T: Writes](keyValue: (String, Seq[T])): RollbarLogger = withKeyValues(keyValue._1, keyValue._2)

/**
* Accepts a list of values and writes them as individual attributes.
* for example:
* withKeyValues("error", Seq("foo", "bar"))
/** Accepts a list of values and writes them as individual attributes. for example: withKeyValues("error", Seq("foo",
* "bar"))
*
* results in the attributes:
* - error_1: foo
* - error_2: bar
* - error_1: foo
* - error_2: bar
*/
def withKeyValues[T: Writes](key: String, values: Seq[T])(
implicit maxValues: Int = MaxValuesToWrite
def withKeyValues[T: Writes](key: String, values: Seq[T])(implicit
maxValues: Int = MaxValuesToWrite
): RollbarLogger = {
val logger = values.take(maxValues).zipWithIndex.foldLeft(this) { case (l, pair) =>
val value = pair._1
Expand All @@ -105,7 +104,8 @@ case class RollbarLogger @AssistedInject() (
}
}

def withKeyValues[T: Writes](key: String, values: NonEmptyChain[T]): RollbarLogger = withKeyValues(key, values.toNonEmptyList.toList)
def withKeyValues[T: Writes](key: String, values: NonEmptyChain[T]): RollbarLogger =
withKeyValues(key, values.toNonEmptyList.toList)

def debug(message: => String): Unit = debug(message, null)
def info(message: => String): Unit = info(message, null)
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/io/flow/log/LogUtilSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LogUtilSpec extends AnyWordSpec with Matchers {
callCount.incrementAndGet()
}
logUtil.duration(info = "test", fingerprint = "test", organizationId = "test")(f)
callCount.get must be (1)
callCount.get must be(1)
}
}

Expand All @@ -36,7 +36,7 @@ class LogUtilSpec extends AnyWordSpec with Matchers {
logUtil.durationF(info = "test", fingerprint = "test", organizationId = "test")(f),
10.millis
)
callCount.get must be (1)
callCount.get must be(1)
}
}
}
Expand Down

0 comments on commit cdaef37

Please sign in to comment.