-
Notifications
You must be signed in to change notification settings - Fork 349
/
log.scala
96 lines (77 loc) · 3.09 KB
/
log.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
88
89
90
91
92
93
94
95
96
// Copyright (c) 2013-2017 Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.util
import java.util.logging.Logger
import scala.concurrent.duration.{ FiniteDuration => FD }
import scala.Predef.augmentString
/** A module of types and instances for logged statements. */
object log {
/**
* Algebraic type of events that can be passed to a `LogHandler`, both parameterized by the
* argument type of the SQL input parameters (this is typically an `HList`).
* @group Events
*/
sealed abstract class LogEvent extends Product with Serializable {
/** The complete SQL string as seen by JDBC. */
def sql: String
/** The query arguments. */
def args: List[Any]
}
/** @group Events */ final case class Success (sql: String, args: List[Any], exec: FD, processing: FD ) extends LogEvent
/** @group Events */ final case class ProcessingFailure(sql: String, args: List[Any], exec: FD, processing: FD, failure: Throwable) extends LogEvent
/** @group Events */ final case class ExecFailure (sql: String, args: List[Any], exec: FD, failure: Throwable) extends LogEvent
/**
* A sink for `LogEvent`s.
* @group Handlers
*/
final case class LogHandler(unsafeRun: LogEvent => Unit)
/**
* Module of instances and constructors for `LogHandler`.
* @group Handlers
*/
object LogHandler {
/**
* A do-nothing `LogHandler`.
* @group Constructors
*/
val nop: LogHandler =
LogHandler(_ => ())
/**
* A LogHandler that writes a default format to a JDK Logger. This is provided for demonstration
* purposes and is not intended for production use.
* @group Constructors
*/
val jdkLogHandler: LogHandler = {
val jdkLogger = Logger.getLogger(getClass.getName)
LogHandler {
case Success(s, a, e1, e2) =>
jdkLogger.info(s"""Successful Statement Execution:
|
| ${s.lines.dropWhile(_.trim.isEmpty).mkString("\n ")}
|
| arguments = [${a.mkString(", ")}]
| elapsed = ${e1.toMillis} ms exec + ${e2.toMillis} ms processing (${(e1 + e2).toMillis} ms total)
""".stripMargin)
case ProcessingFailure(s, a, e1, e2, t) =>
jdkLogger.severe(s"""Failed Resultset Processing:
|
| ${s.lines.dropWhile(_.trim.isEmpty).mkString("\n ")}
|
| arguments = [${a.mkString(", ")}]
| elapsed = ${e1.toMillis} ms exec + ${e2.toMillis} ms processing (failed) (${(e1 + e2).toMillis} ms total)
| failure = ${t.getMessage}
""".stripMargin)
case ExecFailure(s, a, e1, t) =>
jdkLogger.severe(s"""Failed Statement Execution:
|
| ${s.lines.dropWhile(_.trim.isEmpty).mkString("\n ")}
|
| arguments = [${a.mkString(", ")}]
| elapsed = ${e1.toMillis} ms exec (failed)
| failure = ${t.getMessage}
""".stripMargin)
}
}
}
}