Skip to content

Commit ee4a2f5

Browse files
author
paweliwanow
committed
Added LoggerTakingImplicit
1 parent 1e289c6 commit ee4a2f5

File tree

5 files changed

+1000
-1
lines changed

5 files changed

+1000
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,45 @@ class MyClass extends LazyLogging {
7979
}
8080
```
8181

82+
`LoggerTakingImplicit` provides the same methods as `Logger` class, but with additional implicit parameter `A`.
83+
During creation of the `LoggerTakingImplicit` evidence `CanLog[A]` is required.
84+
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:
85+
86+
```scala
87+
case class CorrelationId(value: String)
88+
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
89+
override def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} $originalMsg"
90+
}
91+
92+
93+
implicit val correlationId = CorrelationId("ID")
94+
95+
val logger = Logger.takingImplicit[CorrelationId]("test")
96+
logger.info("Test") // logs "ID Test"
97+
```
98+
99+
It's possible to use `MDC` through `CanLog` without any troubles with execution context.
100+
101+
```scala
102+
case class CorrelationId(value: String)
103+
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
104+
override def logMessage(originalMsg: String, a: CorrelationId): String = {
105+
MDC.put("correlationId", a.value)
106+
originalMsg
107+
}
108+
109+
override def afterLog(a: A): Unit = {
110+
MDC.remove("correlationId")
111+
}
112+
}
113+
114+
115+
implicit val correlationId = CorrelationId("ID")
116+
117+
val logger = Logger.takingImplicit[CorrelationId]("test")
118+
Future(logger.info("Test"))
119+
```
120+
82121
### What's new?
83122

84123
##### 3.5.0

src/main/scala/com/typesafe/scalalogging/Logger.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ object Logger {
3131
def apply(underlying: Underlying): Logger =
3232
new Logger(underlying)
3333

34+
/**
35+
* Create a [[LoggerTakingImplicit]] wrapping the given underlying `org.slf4j.Logger`.
36+
*/
37+
def takingImplicit[A](underlying: Underlying)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
38+
new LoggerTakingImplicit[A](underlying)
39+
3440
/**
3541
* Create a [[Logger]] for the given name.
3642
* Example:
@@ -41,12 +47,28 @@ object Logger {
4147
def apply(name: String): Logger =
4248
new Logger(LoggerFactory.getLogger(name))
4349

50+
/**
51+
* Create a [[LoggerTakingImplicit]] for the given name.
52+
* Example:
53+
* {{{
54+
* val logger = Logger.takingImplicit[CorrelationId]("application")
55+
* }}}
56+
*/
57+
def takingImplicit[A](name: String)(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
58+
new LoggerTakingImplicit[A](LoggerFactory.getLogger(name))
59+
4460
/**
4561
* Create a [[Logger]] wrapping the created underlying `org.slf4j.Logger`.
4662
*/
47-
def apply(clazz: Class[_]): Logger =
63+
def apply[A](clazz: Class[_]): Logger =
4864
new Logger(LoggerFactory.getLogger(clazz.getName))
4965

66+
/**
67+
* Create a [[LoggerTakingImplicit]] wrapping the created underlying `org.slf4j.Logger`.
68+
*/
69+
def takingImplicit[A](clazz: Class[_])(implicit ev: CanLog[A]): LoggerTakingImplicit[A] =
70+
new LoggerTakingImplicit[A](LoggerFactory.getLogger(clazz.getName))
71+
5072
/**
5173
* Create a [[Logger]] for the runtime class wrapped by the implicit class
5274
* tag parameter.
@@ -57,6 +79,17 @@ object Logger {
5779
*/
5880
def apply[T](implicit ct: ClassTag[T]): Logger =
5981
new Logger(LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))
82+
83+
/**
84+
* Create a [[LoggerTakingImplicit]] for the runtime class wrapped by the implicit class
85+
* tag parameter.
86+
* Example:
87+
* {{{
88+
* val logger = Logger.takingImplicit[MyClass]
89+
* }}}
90+
*/
91+
def takingImplicit[T, A](implicit ct: ClassTag[T], ev: CanLog[A]): LoggerTakingImplicit[A] =
92+
new LoggerTakingImplicit[A](LoggerFactory.getLogger(ct.runtimeClass.getName.stripSuffix("$")))
6093
}
6194

6295
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.typesafe.scalalogging
2+
3+
import org.slf4j.{ Marker, Logger => Underlying }
4+
5+
trait CanLog[A] { this: Serializable =>
6+
def logMessage(originalMsg: String, a: A): String
7+
def afterLog(a: A): Unit = ()
8+
}
9+
10+
@SerialVersionUID(957385465L)
11+
final class LoggerTakingImplicit[A] private[scalalogging] (val underlying: Underlying)
12+
(implicit val canLogEv: CanLog[A]) extends Serializable {
13+
14+
// Error
15+
16+
def error(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessage[A]
17+
18+
def error(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageCause[A]
19+
20+
def error(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageArgs[A]
21+
22+
def error(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageMarker[A]
23+
24+
def error(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageCauseMarker[A]
25+
26+
def error(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.errorMessageArgsMarker[A]
27+
28+
// Warn
29+
30+
def warn(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessage[A]
31+
32+
def warn(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageCause[A]
33+
34+
def warn(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageArgs[A]
35+
36+
def warn(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageMarker[A]
37+
38+
def warn(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageCauseMarker[A]
39+
40+
def warn(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.warnMessageArgsMarker[A]
41+
42+
// Info
43+
44+
def info(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessage[A]
45+
46+
def info(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageCause[A]
47+
48+
def info(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageArgs[A]
49+
50+
def info(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageMarker[A]
51+
52+
def info(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageCauseMarker[A]
53+
54+
def info(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.infoMessageArgsMarker[A]
55+
56+
// Debug
57+
58+
def debug(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessage[A]
59+
60+
def debug(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageCause[A]
61+
62+
def debug(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageArgs[A]
63+
64+
def debug(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageMarker[A]
65+
66+
def debug(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageCauseMarker[A]
67+
68+
def debug(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.debugMessageArgsMarker[A]
69+
70+
// Trace
71+
72+
def trace(message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessage[A]
73+
74+
def trace(message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageCause[A]
75+
76+
def trace(message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageArgs[A]
77+
78+
def trace(marker: Marker, message: String)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageMarker[A]
79+
80+
def trace(marker: Marker, message: String, cause: Throwable)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageCauseMarker[A]
81+
82+
def trace(marker: Marker, message: String, args: Any*)(implicit a: A): Unit = macro LoggerTakingImplicitMacro.traceMessageArgsMarker[A]
83+
84+
}

0 commit comments

Comments
 (0)