This repository has been archived by the owner on May 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality to tag logs (#267)
* inital commit after work with @dominickendrick * implement @rtyley's suggested improvements * slight refactoring - tagging logs is not specific to Sentry * comment note updated
- Loading branch information
Showing
14 changed files
with
131 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package monitoring | ||
|
||
import java.util.UUID | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import org.slf4j.MDC | ||
import play.api.mvc.Request | ||
|
||
// Tags to be included in a log statement using Mapped Diagnostic Context (MDC). | ||
// A MDC can be used to supplement log messages with additional contextual information, | ||
// or provide tags to a Sentry Appender. | ||
// | ||
// References: | ||
// - https://logback.qos.ch/manual/mdc.html, and | ||
// - https://github.com/getsentry/raven-java/blob/master/raven-logback/src/main/java/com/getsentry/raven/logback/SentryAppender.java | ||
case class LoggingTags(browserId: String, requestId: UUID) { | ||
|
||
def toMap: Map[String, String] = Map( | ||
LoggingTags.browserId -> browserId, | ||
LoggingTags.requestId -> requestId.toString | ||
) | ||
} | ||
|
||
object LoggingTags { | ||
|
||
val browserId = "browserId" | ||
val requestId = "requestId" | ||
|
||
val allTags = Seq(browserId, requestId) | ||
|
||
// Means that if `implicit request =>` is used in an Action, an implicit LoggingTags instance will be in scope. | ||
implicit def fromRequest(implicit request: Request[Any]): LoggingTags = { | ||
val browserId = request.cookies.get("bwid").map(_.value).getOrElse("unknown") | ||
LoggingTags(browserId, UUID.randomUUID) | ||
} | ||
} | ||
|
||
trait TagAwareLogger extends LazyLogging { | ||
|
||
private[this] def withTags(loggingExpr: => Unit)(implicit tags: LoggingTags): Unit = | ||
try { | ||
for ((tagName, tagValue) <- tags.toMap) MDC.put(tagName, tagValue) | ||
loggingExpr | ||
} finally { | ||
MDC.clear() | ||
} | ||
|
||
def info(msg: String)(implicit tags: LoggingTags): Unit = | ||
withTags(logger.info(msg)) | ||
|
||
def error(msg: String, t: Throwable)(implicit tags: LoggingTags): Unit = | ||
withTags(logger.error(msg, t)) | ||
|
||
def error(msg: String)(implicit tags: LoggingTags): Unit = | ||
withTags(logger.error(msg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.