chore: introduce logger and save log into file
This commit is contained in:
7 files changed
+56
-9
No files matched your search
@@ -42,6 +42,8 @@ dependencies {
|
|||||||
implementation(libs.nashorn.core)
|
implementation(libs.nashorn.core)
|
||||||
implementation(libs.zxing.core)
|
implementation(libs.zxing.core)
|
||||||
implementation(libs.zxing.javase)
|
implementation(libs.zxing.javase)
|
||||||
|
implementation(libs.logback.classic)
|
||||||
|
implementation(libs.slf4j.api)
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.build {
|
tasks.build {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlinVersion = "2.0.10"
|
kotlinVersion = "2.0.20"
|
||||||
ronebotVersion = "1.12.9"
|
ronebotVersion = "1.12.9"
|
||||||
shadowVersion = "8.3.0"
|
shadowVersion = "8.3.0"
|
||||||
okhttpVersion = "5.0.0-alpha.14"
|
okhttpVersion = "5.0.0-alpha.14"
|
||||||
@@ -11,6 +11,8 @@ simpleJavaMailVersion = "8.12.2"
|
|||||||
jsoupVersion = "1.18.1"
|
jsoupVersion = "1.18.1"
|
||||||
nashornVersion = "15.4"
|
nashornVersion = "15.4"
|
||||||
zxingVersion = "3.5.3"
|
zxingVersion = "3.5.3"
|
||||||
|
logbackClassicVersion = "1.5.8"
|
||||||
|
sl4jVersion = "2.0.16"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
ronebot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
|
||||||
@@ -25,6 +27,8 @@ jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoupVersion" }
|
|||||||
nashorn-core = { group = "org.openjdk.nashorn", name = "nashorn-core", version.ref = "nashornVersion" }
|
nashorn-core = { group = "org.openjdk.nashorn", name = "nashorn-core", version.ref = "nashornVersion" }
|
||||||
zxing-core = { group = "com.google.zxing", name = "core", version.ref = "zxingVersion" }
|
zxing-core = { group = "com.google.zxing", name = "core", version.ref = "zxingVersion" }
|
||||||
zxing-javase = { group = "com.google.zxing", name = "javase", version.ref = "zxingVersion" }
|
zxing-javase = { group = "com.google.zxing", name = "javase", version.ref = "zxingVersion" }
|
||||||
|
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logbackClassicVersion" }
|
||||||
|
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "sl4jVersion" }
|
||||||
|
|
||||||
[bundles]
|
[bundles]
|
||||||
|
|
||||||
|
|||||||
@@ -10,10 +10,7 @@ package cn.rtast.fancybot
|
|||||||
import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
|
import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
|
||||||
import cn.rtast.fancybot.commands.parse.*
|
import cn.rtast.fancybot.commands.parse.*
|
||||||
import cn.rtast.fancybot.enums.WSType
|
import cn.rtast.fancybot.enums.WSType
|
||||||
import cn.rtast.fancybot.util.initCommandAndItem
|
import cn.rtast.fancybot.util.*
|
||||||
import cn.rtast.fancybot.util.initDatabase
|
|
||||||
import cn.rtast.fancybot.util.initFilesDir
|
|
||||||
import cn.rtast.fancybot.util.initSetuIndex
|
|
||||||
import cn.rtast.rob.ROneBotFactory
|
import cn.rtast.rob.ROneBotFactory
|
||||||
import cn.rtast.rob.entity.*
|
import cn.rtast.rob.entity.*
|
||||||
import cn.rtast.rob.entity.lagrange.FileEvent
|
import cn.rtast.rob.entity.lagrange.FileEvent
|
||||||
@@ -30,6 +27,7 @@ import java.net.URI
|
|||||||
|
|
||||||
class FancyBot : OneBotListener {
|
class FancyBot : OneBotListener {
|
||||||
|
|
||||||
|
private val logger = Logger.getLogger<FancyBot>()
|
||||||
private val coroutineScope = CoroutineScope(Dispatchers.IO)
|
private val coroutineScope = CoroutineScope(Dispatchers.IO)
|
||||||
private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""")
|
private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""")
|
||||||
|
|
||||||
@@ -43,7 +41,8 @@ class FancyBot : OneBotListener {
|
|||||||
val msg = message.rawMessage
|
val msg = message.rawMessage
|
||||||
val groupId = message.groupId
|
val groupId = message.groupId
|
||||||
val messageId = message.messageId
|
val messageId = message.messageId
|
||||||
println("$sender($senderId: $groupId >>> $messageId): $msg")
|
logger.info("$sender($senderId: $groupId >>> $messageId): $msg")
|
||||||
|
logger.trace("$sender($senderId: $groupId >>> $messageId: $json")
|
||||||
|
|
||||||
if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) {
|
if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) {
|
||||||
message.reply("你发牛魔的火车呢, 我直接就是打断")
|
message.reply("你发牛魔的火车呢, 我直接就是打断")
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
package cn.rtast.fancybot.commands.parse
|
package cn.rtast.fancybot.commands.parse
|
||||||
|
|
||||||
import cn.rtast.fancybot.annotations.CommandDescription
|
import cn.rtast.fancybot.annotations.CommandDescription
|
||||||
|
import cn.rtast.fancybot.util.Logger
|
||||||
import cn.rtast.fancybot.util.makeGif
|
import cn.rtast.fancybot.util.makeGif
|
||||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||||
import cn.rtast.fancybot.util.toByteArray
|
import cn.rtast.fancybot.util.toByteArray
|
||||||
@@ -33,6 +34,7 @@ class AsciiArtCommand : BaseCommand() {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val FONT_SIZE = 12
|
private const val FONT_SIZE = 12
|
||||||
private val font = Font("Monospaced", Font.PLAIN, FONT_SIZE)
|
private val font = Font("Monospaced", Font.PLAIN, FONT_SIZE)
|
||||||
|
private val logger = Logger.getLogger<AsciiArtCommand>()
|
||||||
val waitingList = mutableListOf<Long>()
|
val waitingList = mutableListOf<Long>()
|
||||||
|
|
||||||
private fun BufferedImage.convertToAscii(): String {
|
private fun BufferedImage.convertToAscii(): String {
|
||||||
@@ -106,7 +108,7 @@ class AsciiArtCommand : BaseCommand() {
|
|||||||
val msg = MessageChain.Builder().addImage(imageBase64, true).build()
|
val msg = MessageChain.Builder().addImage(imageBase64, true).build()
|
||||||
message.reply(msg)
|
message.reply(msg)
|
||||||
} else {
|
} else {
|
||||||
println("Making gif ascii art, frames: ${decoder.frameCount}")
|
logger.info("制作GIF Ascii art中, 总帧数: ${decoder.frameCount}")
|
||||||
val frames = (0 until decoder.frameCount).map { decoder.getFrame(it) }
|
val frames = (0 until decoder.frameCount).map { decoder.getFrame(it) }
|
||||||
val asciiFrames = mutableListOf<BufferedImage>()
|
val asciiFrames = mutableListOf<BufferedImage>()
|
||||||
val width = frames.first().width
|
val width = frames.first().width
|
||||||
@@ -114,11 +116,12 @@ class AsciiArtCommand : BaseCommand() {
|
|||||||
frames.forEach { asciiFrames.add(it.convertToAscii().saveAsciiArtToImage(width, height)) }
|
frames.forEach { asciiFrames.add(it.convertToAscii().saveAsciiArtToImage(width, height)) }
|
||||||
val gifBytes = decoder.makeGif(asciiFrames)
|
val gifBytes = decoder.makeGif(asciiFrames)
|
||||||
val gifBase64 = gifBytes.encodeToBase64()
|
val gifBase64 = gifBytes.encodeToBase64()
|
||||||
println(gifBase64)
|
logger.info("处理后的图片大小: ${gifBytes.size}字节, base64大小: ${gifBase64.length * 2}字节")
|
||||||
message.reply(MessageChain.Builder().addImage(gifBase64, true).build())
|
message.reply(MessageChain.Builder().addImage(gifBase64, true).build())
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
message.reply("处理GIF失败")
|
message.reply("处理GIF失败")
|
||||||
|
logger.info("gif处理失败")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message.reply("回复错误已取消本次操作")
|
message.reply("回复错误已取消本次操作")
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2024 RTAkland
|
||||||
|
* Author: RTAkland
|
||||||
|
* Date: 2024/10/7
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package cn.rtast.fancybot.util
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.slf4j.event.Level
|
||||||
|
|
||||||
|
object Logger {
|
||||||
|
inline fun <reified T> getLogger(): Logger {
|
||||||
|
return LoggerFactory.getLogger(T::class.simpleName).also { it.atLevel(Level.INFO) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,7 +31,8 @@ fun initCommandAndItem(rob: ROneBotFactory) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun initFilesDir() {
|
fun initFilesDir() {
|
||||||
File("./$ROOT_PATH/caches/images").also { it.mkdirs() }
|
File("$ROOT_PATH/caches/images").also { it.mkdirs() }
|
||||||
|
File("$ROOT_PATH/logs").also { it.mkdirs() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun initSetuIndex() {
|
fun initSetuIndex() {
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
|
||||||
|
<file>./data/logs/app.log</file>
|
||||||
|
<append>true</append>
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
<appender-ref ref="FILE" />
|
||||||
|
</root>
|
||||||
|
<logger name="Exposed" level="OFF" />
|
||||||
|
</configuration>
|
||||||
Reference in New Issue
Block a user