Skip to content

Commit

Permalink
fix(terminal&logger): 修复日志和远程终端相关错误,并进行优化
Browse files Browse the repository at this point in the history
  • Loading branch information
nullaqua committed Sep 30, 2024
1 parent 27c8938 commit 2f82b27
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 61 deletions.
70 changes: 21 additions & 49 deletions src/main/kotlin/subit/logger/ForumLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ import subit.console.Console
import subit.console.SimpleAnsiColor
import subit.console.SimpleAnsiColor.Companion.CYAN
import subit.console.SimpleAnsiColor.Companion.PURPLE
import subit.logger.ForumLogger.nativeOut
import subit.logger.ForumLogger.safe
import subit.workDir
import java.io.*
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.*
import java.util.logging.Formatter
import java.util.logging.Handler
import java.util.logging.Level
import java.util.logging.LogRecord
import java.util.logging.Logger
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.jvm.optionals.getOrDefault
Expand Down Expand Up @@ -168,6 +164,12 @@ object ToConsoleHandler: Handler()
override fun format(record: LogRecord): String
{
val message = formatMessage(record)
val messages = mutableListOf(message)
if (record.thrown != null)
{
val str = record.thrown.stackTraceToString()
str.split("\n").forEach { messages.add(it) }
}
val level = record.level
val ansiStyle = if (level.intValue() >= Level.SEVERE.intValue()) SimpleAnsiColor.RED.bright()
else if (level.intValue() >= Level.WARNING.intValue()) SimpleAnsiColor.YELLOW.bright()
Expand All @@ -191,55 +193,25 @@ object ToConsoleHandler: Handler()
level.name,
RESET,
)
return "$head $message $RESET"
return messages.joinToString("\n") { "$head $it$RESET" }
}
}
}

override fun publish(record: LogRecord) = safe()
{
if (!loggerConfig.check(record)) return
val messages = mutableListOf(formatter.format(record))

if (record.thrown != null)
{
val str = record.thrown.stackTraceToString()
str.split("\n").forEach { messages.add(it) }
}
val message = formatter.format(record)
/**
* 当[Console.println]调用的时候, 向终端打印日志, 会调用jline库,
* 使得[org.jline.utils.StyleResolver]会在此时打印等级为FINEST的日志.
*
* 当该日志打印时会调用[Console.println], 再次引起日志打印, 造成无限递归.
* 因此在这里特别处理
*/
if (record.loggerName.startsWith("org.jline"))
{
/**
* 如果等级不足INFO就不打印了
*/
if (record.level.intValue() >= Level.INFO.intValue())
{
/**
* 如果等级大于等于INFO, 也不能直接调用[Console.println], 所以使用[nativeOut]直接打印到终端
*/
val head = if (loggerConfig.showLoggerName) String.format(
"[%s][%s][%s]",
ForumLogger.loggerDateFormat.format(record.millis),
record.loggerName,
record.level.name
)
else String.format(
"[%s][%s]",
ForumLogger.loggerDateFormat.format(record.millis),
record.level.name
)
messages.forEach { message -> nativeOut.println("$head $message") }
}
return
}
if (record.loggerName.startsWith("org.jline")) return

messages.forEach(Console::println)
Console.println(message)
}

override fun flush() = Unit
Expand Down Expand Up @@ -279,6 +251,12 @@ object ToFileHandler: Handler()
override fun format(record: LogRecord): String
{
val message = formatMessage(record)
val messages = mutableListOf(message)
if (record.thrown != null)
{
val str = record.thrown.stackTraceToString()
str.split("\n").forEach { messages.add(it) }
}
val level = record.level
val head = if (loggerConfig.showLoggerName) String.format(
"[%s][%s][%s]",
Expand All @@ -291,7 +269,7 @@ object ToFileHandler: Handler()
ForumLogger.loggerDateFormat.format(record.millis),
level.name
)
return "$head $message"
return messages.joinToString("\n") { "$head $it" }
}
}
}
Expand Down Expand Up @@ -365,15 +343,9 @@ object ToFileHandler: Handler()
override fun publish(record: LogRecord) = safe()
{
if (!loggerConfig.check(record)) return
val messages = mutableListOf(formatter.format(record))

if (record.thrown != null)
{
val str = record.thrown.stackTraceToString()
str.split("\n").forEach { messages.add(it) }
}
val messagesWithOutColor = messages.map { colorMatcher.replace(it, "") }
append(messagesWithOutColor)
val message = formatter.format(record)
val messagesWithOutColor = colorMatcher.replace(message, "")
append(messagesWithOutColor.split("\n"))
}

override fun flush() = Unit
Expand Down
13 changes: 2 additions & 11 deletions src/main/kotlin/subit/router/Terminal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@ private val init: Unit = run {
override fun publish(record: LogRecord)
{
if (!loggerConfig.check(record)) return

if (record.loggerName.startsWith("io.ktor.websocket")) return

val messages = mutableListOf(formatter.format(record))

if (record.thrown != null)
{
val str = record.thrown.stackTraceToString()
str.split("\n").forEach { messages.add(it) }
}
loggerFlow.tryEmit(Packet(MESSAGE, messages.joinToString("\n")))
loggerFlow.tryEmit(Packet(MESSAGE, formatter.format(record)))
}
override fun flush() = Unit
override fun close() = Unit
Expand All @@ -73,7 +64,7 @@ fun Route.terminal() = route("/terminal", {
if (loginUser == null || loginUser.permission != PermissionLevel.ROOT)
return@webSocket close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "Permission denied"))

val job = launch { sharedFlow.collect { sendSerialized(it) } }
val job = launch { sharedFlow.collect(::sendSerialized) }

class WebSocketCommandSender(user: UserFull): CommandSet.CommandSender
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/terminal.html
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ <h1>创意写作后端 终端管理</h1>
}

socket.addEventListener('message', (event) => {
const messageDiv = document.createElement('pre');
let data = JSON.parse(event.data);

if (data.type === 'TAB')
Expand All @@ -653,6 +652,7 @@ <h1>创意写作后端 终端管理</h1>
return;
}
data.data.split('\n').forEach((line) => {
const messageDiv = document.createElement('pre');
messageDiv.innerHTML = ansi_up.ansi_to_html(line);
document.getElementById('messages').appendChild(messageDiv);
});
Expand Down

0 comments on commit 2f82b27

Please sign in to comment.