Skip to content

Instantly share code, notes, and snippets.

View jschwietert's full-sized avatar

Jonathan Schwietert jschwietert

View GitHub Profile
@jschwietert
jschwietert / scala_unified_logging-logger.scala
Created July 20, 2017 19:07
Scaled down Logger implementation of VictorOps unified logging.
trait Logger {
final protected def format(t: Throwable, msg: => String, variables: LogVariables): String
final def format(msg: => String, variables: LogVariable*): String
final def info(msg: => String, variables: LogVariable*): Unit = if (isInfoEnabled) logInfo(format(msg, variables))
// ...
final def error(msg: => String, variables: LogVariable*): Unit = if (isErrorEnabled) logError(format(msg, variables))
final def error(t: Throwable, msg: => String, variables: LogVariable*) = if (isErrorEnabled) logError(t, format(t, msg, variables))
@jschwietert
jschwietert / scala_unified_logging-logging.scala
Created July 20, 2017 18:50
Logging trait in VictorOps unified logging.
trait Logging {
def classLogVariables: LogVariables = Nil
implicit lazy protected[logging] val log = Logger.ClassLogger(this.getClass, classLogVariables)
}
trait ActorLogging extends Logging {
this: akka.actor.Actor =>
implicit override lazy protected[logging] val log = Logger.ActorLogger(this, classLogVariables)
}
@jschwietert
jschwietert / scala_unified_logging-implicit_logger.scala
Created July 20, 2017 18:42
Implicit logger usage with VictorOps unified logging.
def format(phoneNumber: String)(implicit log: Logger)
@jschwietert
jschwietert / scala_unified_logging-log_variables.scala
Created July 20, 2017 18:38
Log variable usage with VictorOps unified logging.
type LogVariable = (String, Any)
def info(msg: => String, variables: LogVariable*): Unit
@jschwietert
jschwietert / scala_unified_logging-before_after_improvements.scala.diff
Created July 19, 2017 18:49
Reuse, codified formatting, and maintainability improvements with VictorOps unified logging.
+ override def classLogVariables: LogVariables = Seq(“org” -> orgSlug, “incidentId” -> incidentId, “policy” -> policySlug)
+
override def preStart(): Unit = {
- log.info(s”Starting EscalationPolicyActor [org=$orgSlug] [incidentId=$incidentId] [policy=$policySlug]”)
+ log.info(“Starting EscalationPolicyActor”)
super.preStart()
}
override def postStop(): Unit = {
- log.info(s”Stopping EscalationPolicyActor [org=$orgSlug] [incidentId=$incidentId] [policy=$policySlug]”)
@jschwietert
jschwietert / scala_unified_logging-utility_log_better.log
Created July 19, 2017 18:47
Utility logging with VictorOps unified logging and context from caller.
Jun 21 17:43:49 server1.pr.victorops.com WARN victorops.controllers.api.UserController$ – Invalid phone number: ‘555-55-555’ :: [org=victorops] [user=jonathan]
@jschwietert
jschwietert / scala_unified_logging-utility_log_bad.log
Last active July 19, 2017 18:45
Utility logging without VictorOps unified logging.
Jun 21 17:43:49 server1.pr.victorops.com WARN victorops.common.util.PhoneFormat$ - Invalid phone number: ‘555-55-555’
@jschwietert
jschwietert / scala_unified_logging-log_format.scala.diff
Last active July 19, 2017 18:42
Formatting exception strings with VictorOps unified logging and class-level log variables.
- .getOrElse(Future.failed(s”Unable to locate webhook [org=$orgSlug] [incidentId=$incidentId] [policy=$policySlug]”))
// prints: Unable to locate webhook [org=victorops] [incidentId=1234] [policy=yolo]
+ .getOrElse(Future.failed(log.format(“Unable to locate webhook”)))
// prints: Unable to locate webhook :: [org=victorops] [incidentId=1234] [policy=yolo]
@jschwietert
jschwietert / scala_unified_logging-formatting_errors.scala.diff
Last active July 19, 2017 18:41
Avoiding typos with VictorOps unified logging & log variables.
- log.info(s”escalation [resultSummary: $resultSummary” )
// prints: escalation [resultSummary: ResultSummary(…)
+ log.info(“escalation”, “resultSummary” -> resultSummary)
// prints: escalation :: [resultSummary=ResultSummary(…)]

Keybase proof

I hereby claim:

  • I am jschwietert on github.
  • I am jschwietert (https://keybase.io/jschwietert) on keybase.
  • I have a public key whose fingerprint is C672 94E9 5F0E 5605 FEF6 B432 9F5A D98B FF4A 4B14

To claim this, I am signing this object: