Skip to content

Added method to extract the context object #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ In case of `LazyLogging` and `StrictLogging`, the underlying SLF4J logger is nam
these traits are mixed:

```scala
class MyClass extends LazyLogging {
class LazyLoggingExample extends LazyLogging {
logger.debug("This is very convenient ;-)")

logger.whenDebugEnabled {
Expand All @@ -87,6 +87,19 @@ class MyClass extends LazyLogging {
}
```

```scala
class AnyLoggingExample extends AnyLogging {
override protected val logger: Logger = Logger("name")

logger.info("This is Any Logging ;-)")

logger.whenInfoEnabled {
println("This would only execute when the info level is enabled.")
(1 to 10).foreach(x => println("Scala logging is great!"))
}
}
```

`LoggerTakingImplicit` provides the same methods as `Logger` class, but with additional implicit parameter `A`.
During creation of the `LoggerTakingImplicit` evidence `CanLog[A]` is required.
It may be useful when contextual parameter (e.g. _Correlation ID_) is being passed around and you would like to include it in the log messages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package com.typesafe.scalalogging
import org.slf4j.{ Logger => Underlying }

trait CanLog[A] {
def logMessage(originalMsg: String, a: A): String
def afterLog(a: A): Unit = {
val _ = a
def logMessage(originalMsg: String, context: A): String
def afterLog(context: A): Unit = {
val _ = context
}
def getContext()(implicit context: A): A = context
}

@SerialVersionUID(957385465L)
Expand Down