|
1 | 1 | package org.jetbrains.kotlinx.jupyter.magics
|
2 | 2 |
|
3 | 3 | import ch.qos.logback.classic.Level
|
| 4 | +import ch.qos.logback.classic.spi.ILoggingEvent |
| 5 | +import ch.qos.logback.core.Appender |
| 6 | +import ch.qos.logback.core.FileAppender |
4 | 7 | import com.github.ajalt.clikt.core.CliktCommand
|
5 | 8 | import com.github.ajalt.clikt.parameters.options.default
|
6 | 9 | import com.github.ajalt.clikt.parameters.options.flag
|
7 | 10 | import com.github.ajalt.clikt.parameters.options.option
|
8 | 11 | import com.github.ajalt.clikt.parameters.types.int
|
9 | 12 | import com.github.ajalt.clikt.parameters.types.long
|
10 | 13 | import org.jetbrains.kotlinx.jupyter.ExecutedCodeLogging
|
| 14 | +import org.jetbrains.kotlinx.jupyter.LoggingManagement.addAppender |
| 15 | +import org.jetbrains.kotlinx.jupyter.LoggingManagement.allLogAppenders |
| 16 | +import org.jetbrains.kotlinx.jupyter.LoggingManagement.removeAppender |
| 17 | +import org.jetbrains.kotlinx.jupyter.LoggingManagement.setRootLoggingLevel |
11 | 18 | import org.jetbrains.kotlinx.jupyter.OutputConfig
|
12 | 19 | import org.jetbrains.kotlinx.jupyter.ReplOptions
|
13 | 20 | import org.jetbrains.kotlinx.jupyter.exceptions.ReplException
|
14 | 21 | import org.jetbrains.kotlinx.jupyter.libraries.DefaultInfoSwitch
|
15 | 22 | import org.jetbrains.kotlinx.jupyter.libraries.LibrariesProcessor
|
16 | 23 | import org.jetbrains.kotlinx.jupyter.libraries.ResolutionInfoSwitcher
|
17 |
| -import org.jetbrains.kotlinx.jupyter.setLevelForAllLoggers |
18 | 24 |
|
19 | 25 | class FullMagicsHandler(
|
20 | 26 | private val repl: ReplOptions,
|
@@ -80,6 +86,49 @@ class FullMagicsHandler(
|
80 | 86 | "debug" -> Level.DEBUG
|
81 | 87 | else -> throw ReplException("Unknown log level: '$levelStr'")
|
82 | 88 | }
|
83 |
| - setLevelForAllLoggers(level) |
| 89 | + setRootLoggingLevel(level) |
| 90 | + } |
| 91 | + |
| 92 | + override fun handleLogHandler() { |
| 93 | + val commandArgs = arg?.split(Regex("""\s+""")).orEmpty() |
| 94 | + val command = commandArgs.firstOrNull() ?: throw ReplException("Log handler command has not been passed") |
| 95 | + when (command) { |
| 96 | + "list" -> { |
| 97 | + println("Log appenders:") |
| 98 | + allLogAppenders().forEach { |
| 99 | + println( |
| 100 | + buildString { |
| 101 | + append(it.name) |
| 102 | + append(" of type ") |
| 103 | + append(it::class.simpleName) |
| 104 | + if (it is FileAppender) { |
| 105 | + append("(${it.file})") |
| 106 | + } |
| 107 | + } |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | + "add" -> { |
| 112 | + val appenderName = commandArgs.getOrNull(1) ?: throw ReplException("Log handler add command needs appender name argument") |
| 113 | + val appenderType = commandArgs.getOrNull(2) ?: throw ReplException("Log handler add command needs appender type argument") |
| 114 | + val appenderTypeArgs = commandArgs.subList(3, commandArgs.size) |
| 115 | + |
| 116 | + val appender: Appender<ILoggingEvent> = when (appenderType) { |
| 117 | + "--file" -> { |
| 118 | + val fileName = appenderTypeArgs.getOrNull(0) ?: throw ReplException("File appender needs file name to be specified") |
| 119 | + val res = FileAppender<ILoggingEvent>() |
| 120 | + res.file = fileName |
| 121 | + res |
| 122 | + } |
| 123 | + else -> throw ReplException("Unknown appender type: $appenderType") |
| 124 | + } |
| 125 | + addAppender(appenderName, appender) |
| 126 | + } |
| 127 | + "remove" -> { |
| 128 | + val appenderName = commandArgs.getOrNull(1) ?: throw ReplException("Log handler remove command needs appender name argument") |
| 129 | + removeAppender(appenderName) |
| 130 | + } |
| 131 | + else -> throw ReplException("") |
| 132 | + } |
84 | 133 | }
|
85 | 134 | }
|
0 commit comments