-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.kt
109 lines (87 loc) · 4.17 KB
/
Log.kt
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
97
98
99
100
101
102
103
104
105
106
107
108
109
package kaist.iclab.abclogger.common
import com.mongodb.event.CommandFailedEvent
import com.mongodb.event.CommandStartedEvent
import com.mongodb.event.CommandSucceededEvent
import org.apache.log4j.*
import org.apache.log4j.spi.LoggingEvent
import java.text.SimpleDateFormat
import java.util.*
object Log {
private val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss XXX")
private val layout = PatternLayout("[%c] %d{HH:mm:ss.SSS} [%t] [%-3p] %m%n")
private val generalLogger = Logger.getLogger("General").apply {
level = Level.INFO
addAppender(ConsoleAppender(layout))
}
private val errorLogger = Logger.getLogger("Error").apply {
level = Level.ERROR
addAppender(ConsoleAppender(layout))
}
private val mongoLogger = Logger.getLogger("Mongo").apply {
level = Level.DEBUG
addAppender(ConsoleAppender(layout))
}
fun enableFileAppender(basePath: String) {
generalLogger.addAppender(DailyRollingFileAppender(layout, "$basePath/logs.general", "'.'yyyy-MM-dd"))
errorLogger.addAppender(DailyRollingFileAppender(layout, "$basePath/logs.error", "'.'yyyy-MM-dd"))
mongoLogger.addAppender(DailyRollingFileAppender(layout, "$basePath/logs.mongo", "'.'yyyy-MM-dd"))
}
fun enableGMailAppender(email: String, password: String, recipients: List<String>) {
val gmailSender = GMailSender(email = email, password = password, recipients = recipients)
val gmailAppender = object : AppenderSkeleton() {
override fun requiresLayout(): Boolean = false
override fun append(event: LoggingEvent?) {
val formattedTime = (event?.getTimeStamp() ?: System.currentTimeMillis()).let {
val calendar = GregorianCalendar.getInstance()
calendar.timeInMillis = it
formatter.format(calendar.time)
}
val isImportant = event?.getLevel() == Level.ERROR
if (!isImportant) return
val message = event?.renderedMessage
val throwable = event?.throwableStrRep?.joinToString(System.lineSeparator())
if (message?.isNotEmpty() == true || throwable?.isNotEmpty() == true) {
val subject = "[ABC Logger] Error report at $formattedTime"
val content = "${message ?: ""}${System.lineSeparator()}----------${throwable ?: ""}"
info("$subject - $content")
try {
gmailSender.send(subject, content)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
override fun close() { }
}
errorLogger.addAppender(gmailAppender)
}
fun error(msg: String, throwable: Throwable? = null) {
if (throwable != null) errorLogger.error(msg, throwable) else errorLogger.error(msg)
}
fun info(msg: String, throwable: Throwable? = null) {
if (throwable != null) generalLogger.info(msg, throwable) else generalLogger.info(msg)
}
fun info(event: CommandStartedEvent?) {
if (event == null) return
val command = if ("insert" in event.commandName) {
""
} else {
event.command.toJson()
}
val base = "${event.requestId}/${event.commandName}:$command} " +
"on ${event.connectionDescription.serverAddress}.${event.databaseName} - ${event.connectionDescription.connectionId}"
mongoLogger.info(base)
}
fun info(event: CommandSucceededEvent?) {
if (event == null) return
val base = "${event.requestId}/${event.commandName} " +
"on ${event.connectionDescription.serverAddress} - ${event.connectionDescription.connectionId}"
mongoLogger.info(base)
}
fun error(event: CommandFailedEvent?) {
if (event == null) return
val base = "${event.requestId}/${event.commandName} " +
"on ${event.connectionDescription.serverAddress} - ${event.connectionDescription.connectionId}"
if (event.throwable != null) mongoLogger.error(base, event.throwable) else mongoLogger.error(base)
}
}