/
Logging.scala
87 lines (71 loc) · 3.25 KB
/
Logging.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.thoughtworks.deeplearning.plugins
import java.util.logging.{Level, LogRecord, Logger}
import com.dongxiguo.fastring.Fastring
import com.dongxiguo.fastring.Fastring.Implicits._
import com.thoughtworks.feature.Caller
object Logging {
/** A [[java.util.logging.LogRecord LogRecord]] that contains current source contextual information. */
class ContextualLogRecord(
level: Level,
message: String = null,
parameters: Array[AnyRef] = null,
thrown: Throwable = null)(implicit fullName: sourcecode.FullName, name: sourcecode.Name, caller: Caller[_])
extends LogRecord(level, message) {
setParameters(parameters)
setThrown(thrown)
setLoggerName(fullName.value)
setSourceClassName(caller.value.getClass.getName)
setSourceMethodName(name.value)
}
trait LazyMessage extends LogRecord {
protected def makeDefaultMessage: Fastring
private lazy val defaultMessage: String = makeDefaultMessage.toString
override def getMessage: String = super.getMessage match {
case null => defaultMessage
case message => message
}
}
final class ThrownInLayer(val layer: Layers#Layer, getThrown: Throwable)(implicit fullName: sourcecode.FullName,
name: sourcecode.Name,
caller: Caller[_])
extends ContextualLogRecord(Level.SEVERE, thrown = getThrown)
with LazyMessage {
override protected def makeDefaultMessage: Fastring = fast"An exception is thrown in layer $layer"
}
final class ThrownInWeight(val weight: Weights#Weight, getThrown: Throwable)(implicit fullName: sourcecode.FullName,
name: sourcecode.Name,
caller: Caller[_])
extends ContextualLogRecord(Level.SEVERE, thrown = getThrown)
with LazyMessage {
override protected def makeDefaultMessage: Fastring = fast"An exception is thrown in weight $weight"
}
}
/** A plugin that logs uncaught exceptions raised from [[Layer]] and [[Weight]].
*
* @author 杨博 (Yang Bo)
*/
trait Logging extends Layers with Weights {
import Logging._
@transient lazy val logger: Logger = Logger.getLogger(getClass.getName)
trait LayerApi extends super.LayerApi { this: Layer =>
implicit protected def fullName: sourcecode.FullName
implicit protected def name: sourcecode.Name
implicit protected def caller: Caller[_]
override protected def handleException(thrown: Throwable): Unit = {
super.handleException(thrown)
logger.log(new ThrownInLayer(this, thrown))
}
}
override type Layer <: LayerApi
trait WeightApi extends super.WeightApi { this: Weight =>
implicit protected def fullName: sourcecode.FullName
implicit protected def name: sourcecode.Name
implicit protected def caller: Caller[_]
override protected def handleException(thrown: Throwable): Unit = {
super.handleException(thrown)
logger.log(new ThrownInWeight(this, thrown))
}
}
override type Weight <: WeightApi
override type Implicits <: super[Layers].ImplicitsApi with super[Weights].ImplicitsApi
}