aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/context/AppLogger.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/context/AppLogger.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/context/AppLogger.kt63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/context/AppLogger.kt b/app/src/main/java/org/pacien/tincapp/context/AppLogger.kt
new file mode 100644
index 0000000..3c1be44
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/context/AppLogger.kt
@@ -0,0 +1,63 @@
1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.tincapp.context
20
21import ch.qos.logback.classic.Logger
22import ch.qos.logback.classic.LoggerContext
23import ch.qos.logback.classic.android.LogcatAppender
24import ch.qos.logback.classic.encoder.PatternLayoutEncoder
25import ch.qos.logback.classic.spi.ILoggingEvent
26import ch.qos.logback.core.Context
27import ch.qos.logback.core.FileAppender
28import org.slf4j.LoggerFactory
29
30/**
31 * @author pacien
32 */
33object AppLogger {
34 private const val LOGCAT_PATTERN = "[%thread] %msg%n%rEx"
35 private const val LOGFILE_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%rEx"
36
37 fun configure() {
38 (LoggerFactory.getILoggerFactory() as LoggerContext)
39 .apply { reset() }
40 .let { loggerContext ->
41 (LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger)
42 .apply {
43 addAppender(LogcatAppender()
44 .apply { context = loggerContext }
45 .apply { encoder = patternEncoder(loggerContext, LOGCAT_PATTERN) }
46 .apply { start() })
47 }
48 .apply {
49 addAppender(FileAppender<ILoggingEvent>()
50 .apply { context = loggerContext }
51 .apply { encoder = patternEncoder(loggerContext, LOGFILE_PATTERN) }
52 .apply { file = AppPaths.appLogFile().absolutePath }
53 .apply { start() })
54 }
55 }
56 }
57
58 private fun patternEncoder(ctx: Context, pat: String) =
59 PatternLayoutEncoder()
60 .apply { context = ctx }
61 .apply { pattern = pat }
62 .apply { start() }
63}