From 79206dd13b2f984027090da292e534d01f2b74ed Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 15 Oct 2023 21:14:54 +0800 Subject: [PATCH] new logger level and formatter - add MornyLogLevels with new log level NOTICE(notice) and ATTION(attention) - make mechanic and morny (but not coeur) related INFO to NOTICE, warn to ATTION - add MornyFormatterConsole to support the formatted time display, and some other formatter changed - now startup key output will hide key except the starting and ending 4 chars - change the function definition of consume in EventEnv - removed src/test/scala/live/LiveMain - and added gitignore for src/test/scala/live --- gradle.properties | 4 ++-- settings.gradle | 2 +- .../scala/cc/sukazyo/cono/morny/Log.scala | 16 +++++++------ .../cc/sukazyo/cono/morny/MornyCoeur.scala | 5 ++-- .../cc/sukazyo/cono/morny/ServerMain.scala | 7 ++++++ .../sukazyo/cono/morny/bot/api/EventEnv.scala | 14 ++++------- .../morny/bot/command/MornyCommands.scala | 4 ++-- .../morny/bot/command/MornyManagers.scala | 8 +++---- .../morny/bot/event/OnUniMeowTrigger.scala | 3 +-- .../cono/morny/daemon/MedicationTimer.scala | 8 +++---- .../cono/morny/daemon/MornyDaemons.scala | 8 +++---- .../internal/logging/IMornyLogLevelImpl.scala | 12 ++++++++++ .../logging/MornyFormatterConsole.scala | 24 +++++++++++++++++++ .../internal/logging/MornyLogLevels.scala | 13 ++++++++++ .../internal/logging/MornyLoggerBase.scala | 22 +++++++++++++++++ .../cono/morny/util/StringEnsure.scala | 3 +++ src/test/scala/.gitignore | 1 + src/test/scala/live/LiveMain.scala | 14 ----------- 18 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 src/main/scala/cc/sukazyo/cono/morny/internal/logging/IMornyLogLevelImpl.scala create mode 100644 src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyFormatterConsole.scala create mode 100644 src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLogLevels.scala create mode 100644 src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLoggerBase.scala create mode 100644 src/test/scala/.gitignore delete mode 100644 src/test/scala/live/LiveMain.scala diff --git a/gradle.properties b/gradle.properties index 9f87499..d78eb7a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s -VERSION = 1.1.1.xiongan-dev1 +VERSION = 1.1.1.xiongan-dev2 USE_DELTA = false VERSION_DELTA = @@ -17,7 +17,7 @@ CODENAME = nanchang lib_spotbugs_v = 4.7.3 lib_scalamodule_xml_v = 2.2.0 -lib_messiva_v = 0.1.1 +lib_messiva_v = 0.2.0 lib_resourcetools_v = 0.2.2 lib_javatelegramapi_v = 6.2.0 diff --git a/settings.gradle b/settings.gradle index 92f8302..b7b457e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ -rootProject.name = 'Coeur Morny Cono' +rootProject.name = "Coeur Morny Cono" diff --git a/src/main/scala/cc/sukazyo/cono/morny/Log.scala b/src/main/scala/cc/sukazyo/cono/morny/Log.scala index 5ef60b3..c8dd452 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/Log.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/Log.scala @@ -1,25 +1,27 @@ package cc.sukazyo.cono.morny +import cc.sukazyo.cono.morny.internal.logging.{MornyFormatterConsole, MornyLoggerBase} import cc.sukazyo.messiva.appender.ConsoleAppender import cc.sukazyo.messiva.formatter.SimpleFormatter -import cc.sukazyo.messiva.log.LogLevel +import cc.sukazyo.messiva.log.LogLevels import cc.sukazyo.messiva.logger.Logger import java.io.{PrintWriter, StringWriter} object Log { - val logger: Logger = Logger( + val logger: MornyLoggerBase = MornyLoggerBase( ConsoleAppender( - SimpleFormatter() + MornyFormatterConsole() ) - ).minLevel(LogLevel.INFO) + ) + logger minLevel LogLevels.INFO - def debug: Boolean = logger.levelSetting.minLevel.level <= LogLevel.DEBUG.level + def debug: Boolean = logger.levelSetting.minLevel.level <= LogLevels.DEBUG.level def debug(is: Boolean): Unit = - if is then logger.minLevel(LogLevel.ALL) - else logger.minLevel(LogLevel.INFO) + if is then logger.minLevel(LogLevels.ALL) + else logger.minLevel(LogLevels.INFO) def exceptionLog (e: Throwable): String = val stackTrace = StringWriter() diff --git a/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala b/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala index 905489f..e9337cf 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala @@ -27,7 +27,8 @@ class MornyCoeur (using val config: MornyConfig) { logger info "Coeur starting..." - logger info s"args key:\n ${config.telegramBotKey}" + import cc.sukazyo.cono.morny.util.StringEnsure.deSensitive + logger info s"args key:\n ${config.telegramBotKey deSensitive 4}" if config.telegramBotUsername ne null then logger info s"login as:\n ${config.telegramBotUsername}" @@ -92,7 +93,7 @@ class MornyCoeur (using val config: MornyConfig) { def saveDataAll(): Unit = { // nothing to do - logger info "done all save action." + logger notice "done all save action." } private def exitCleanup (): Unit = { diff --git a/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala b/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala index 86a0c0a..e4f7870 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala @@ -5,11 +5,15 @@ import cc.sukazyo.cono.morny.MornyConfig.CheckFailure import cc.sukazyo.cono.morny.util.CommonFormat import java.time.ZoneOffset +import java.util.TimeZone import scala.collection.mutable.ArrayBuffer import scala.language.postfixOps object ServerMain { + val tz: TimeZone = TimeZone getDefault + val tz_offset: ZoneOffset = ZoneOffset ofTotalSeconds (tz.getRawOffset/1000) + private val THREAD_MORNY_INIT: String = "morny-init" def main (args: Array[String]): Unit = { @@ -135,6 +139,9 @@ object ServerMain { |- Morny ${MornySystem.CODENAME toUpperCase} |- <${MornySystem.getJarMD5}> [${BuildConfig.CODE_TIMESTAMP}]""".stripMargin + // due to [[MornyFormatterConsole]] will use a localized time, it will output to the log + logger info s"logging time will use time-zone ${tz.getID} ($tz_offset)" + /// /// Check Coeur arguments /// finally start Coeur Program diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala index ec34522..6cc7dea 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala @@ -3,8 +3,6 @@ package cc.sukazyo.cono.morny.bot.api import com.pengrad.telegrambot.model.Update import scala.collection.mutable -import scala.reflect.ClassTag -import scala.util.boundary class EventEnv ( @@ -24,14 +22,10 @@ class EventEnv ( def provide (i: Any): Unit = variables += (i.getClass -> i) - def use [T] (t: Class[T]): ConsumeProvider[T] = ConsumeProvider(t) - - class ConsumeProvider[T] (t: Class[T]) { - def consume (consumer: T => Unit): ConsumeResult = { - variables get t match - case Some(i) => consumer(i.asInstanceOf[T]); ConsumeResult(true) - case None => ConsumeResult(false) - } + def consume [T] (t: Class[T]) (consumer: T => Unit): ConsumeResult = { + variables get t match + case Some(i) => consumer(i.asInstanceOf[T]); ConsumeResult(true) + case None => ConsumeResult(false) } class ConsumeResult (success: Boolean) { diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommands.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommands.scala index 0121b0c..e924d7e 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommands.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommands.scala @@ -91,14 +91,14 @@ class MornyCommands (using coeur: MornyCoeur) { val listing = commands_toTelegramList automaticTGListRemove() coeur.account exec SetMyCommands(listing:_*) - logger info + logger notice s"""automatic updated telegram command list : |${commandsTelegramList_toString(listing)}""".stripMargin } def automaticTGListRemove (): Unit = { coeur.account exec DeleteMyCommands() - logger info "cleaned up command list" + logger notice "cleaned up command list" } private def commandsTelegramList_toString (list: Array[BotCommand]): String = diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala index 5a143c0..aea4127 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyManagers.scala @@ -31,7 +31,7 @@ class MornyManagers (using coeur: MornyCoeur) { event.message.chat.id, TelegramStickers ID_EXIT ).replyToMessageId(event.message.messageId) - logger info s"Morny exited by user ${user toLogTag}" + logger attention s"Morny exited by user ${user toLogTag}" coeur.exit(0, user) } else { @@ -40,7 +40,7 @@ class MornyManagers (using coeur: MornyCoeur) { event.message.chat.id, TelegramStickers ID_403 ).replyToMessageId(event.message.messageId) - logger info s"403 exit caught from user ${user toLogTag}" + logger attention s"403 exit caught from user ${user toLogTag}" coeur.daemons.reporter.unauthenticatedAction("/exit", user) } @@ -62,7 +62,7 @@ class MornyManagers (using coeur: MornyCoeur) { if (coeur.trusted isTrusted user.id) { - logger info s"call save from command by ${user toLogTag}" + logger attention s"call save from command by ${user toLogTag}" coeur.saveDataAll() coeur.account exec SendSticker( event.message.chat.id, @@ -75,7 +75,7 @@ class MornyManagers (using coeur: MornyCoeur) { event.message.chat.id, TelegramStickers ID_403 ).replyToMessageId(event.message.messageId) - logger info s"403 save caught from user ${user toLogTag}" + logger attention s"403 save caught from user ${user toLogTag}" coeur.daemons.reporter.unauthenticatedAction("/save", user) } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala index 6667a15..ccf140f 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala @@ -5,13 +5,12 @@ import cc.sukazyo.cono.morny.bot.command.MornyCommands import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.MornyCoeur -import com.pengrad.telegrambot.model.Update class OnUniMeowTrigger (using commands: MornyCommands) (using coeur: MornyCoeur) extends EventListener { override def onMessage (using event: EventEnv): Unit = { - event use classOf[InputCommand] consume { input => + event.consume (classOf[InputCommand]) { input => logger trace s"got input command {$input} from event-context" for ((name, command_instance) <- commands.commands_uni) { diff --git a/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala b/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala index 6e0ed09..4840418 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/daemon/MedicationTimer.scala @@ -30,11 +30,11 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread { override def run (): Unit = { if ((notify_toChat == -1) || (notify_atHour isEmpty)) { - logger info "Medication Timer disabled : related param is not complete set" + logger notice "Medication Timer disabled : related param is not complete set" return } - logger info "Medication Timer started." + logger notice "Medication Timer started." while (!this.isInterrupted) { try { val next_time = calcNextRoutineTimestamp(System.currentTimeMillis, use_timeZone, notify_atHour) @@ -47,7 +47,7 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread { } catch case _: InterruptedException => interrupt() - logger info "MedicationTimer was interrupted, will be exit now" + logger notice "MedicationTimer was interrupted, will be exit now" case ill: IllegalArgumentException => logger warn "MedicationTimer will not work due to: " + ill.getMessage interrupt() @@ -58,7 +58,7 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread { .stripMargin coeur.daemons.reporter.exception(e) } - logger info "Medication Timer stopped." + logger notice "Medication Timer stopped." } diff --git a/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala b/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala index b748480..44c0edc 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/daemon/MornyDaemons.scala @@ -11,18 +11,18 @@ class MornyDaemons (using val coeur: MornyCoeur) { def start (): Unit = { - logger info "ALL Morny Daemons starting..." + logger notice "ALL Morny Daemons starting..." // TrackerDataManager.init(); medicationTimer.start() - logger info "Morny Daemons started." + logger notice "Morny Daemons started." } def stop (): Unit = { - logger.info("stopping All Morny Daemons...") + logger notice "stopping All Morny Daemons..." // TrackerDataManager.DAEMON.interrupt(); medicationTimer.interrupt() @@ -31,7 +31,7 @@ class MornyDaemons (using val coeur: MornyCoeur) { catch case e: InterruptedException => e.printStackTrace(System.out) - logger.info("stopped ALL Morny Daemons.") + logger notice "stopped ALL Morny Daemons." } } diff --git a/src/main/scala/cc/sukazyo/cono/morny/internal/logging/IMornyLogLevelImpl.scala b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/IMornyLogLevelImpl.scala new file mode 100644 index 0000000..80d2847 --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/IMornyLogLevelImpl.scala @@ -0,0 +1,12 @@ +package cc.sukazyo.cono.morny.internal.logging + +import cc.sukazyo.messiva.log.Message + +trait IMornyLogLevelImpl { + + def notice (message: String): Unit + def notice (message: Message): Unit + def attention (message: String): Unit + def attention (message: Message): Unit + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyFormatterConsole.scala b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyFormatterConsole.scala new file mode 100644 index 0000000..2d9514a --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyFormatterConsole.scala @@ -0,0 +1,24 @@ +package cc.sukazyo.cono.morny.internal.logging + +import cc.sukazyo.cono.morny.util.CommonFormat.formatDate +import cc.sukazyo.cono.morny.ServerMain +import cc.sukazyo.messiva.formatter.ILogFormatter +import cc.sukazyo.messiva.log.Log + +import java.time.{ZoneId, ZoneOffset} +import java.util.TimeZone + +class MornyFormatterConsole extends ILogFormatter { + + override def format (log: Log): String = + val message = StringBuilder() + val dt = formatDate(log.timestamp, ServerMain.tz_offset) + val prompt_heading = s"[$dt][${log.thread.getName}]" + val prompt_newline = "'" * prompt_heading.length + val prompt_levelTag = s"${log.level.tag}::: " + message ++= prompt_heading ++= prompt_levelTag ++= log.message.message(0) + for (line <- log.message.message drop 1) + message += '\n' ++= prompt_newline ++= prompt_levelTag ++= line + message toString + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLogLevels.scala b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLogLevels.scala new file mode 100644 index 0000000..acaf7b8 --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLogLevels.scala @@ -0,0 +1,13 @@ +package cc.sukazyo.cono.morny.internal.logging + +import cc.sukazyo.messiva.log.ILogLevel + +enum MornyLogLevels ( + override val level: Float, + override val tag: String +) extends ILogLevel { + + case NOTICE extends MornyLogLevels(0.2f, "NOTICE") + case ATTENTION extends MornyLogLevels(0.3f, "ATTION") + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLoggerBase.scala b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLoggerBase.scala new file mode 100644 index 0000000..5d4b143 --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/internal/logging/MornyLoggerBase.scala @@ -0,0 +1,22 @@ +package cc.sukazyo.cono.morny.internal.logging + +import cc.sukazyo.messiva.appender.IAppender +import cc.sukazyo.messiva.log.{Log, Message} +import cc.sukazyo.messiva.logger.Logger + +class MornyLoggerBase extends Logger with IMornyLogLevelImpl { + + def this (appends: IAppender*) = + this() + this.appends.addAll(java.util.List.of(appends:_*)) + + override def notice (message: String): Unit = + pushToAllAppender(Log(1, new Message(message), MornyLogLevels.NOTICE)) + override def notice (message: Message): Unit = + pushToAllAppender(Log(1, message, MornyLogLevels.NOTICE)) + override def attention (message: String): Unit = + pushToAllAppender(Log(1, new Message(message), MornyLogLevels.ATTENTION)) + override def attention (message: Message): Unit = + pushToAllAppender(Log(1, message, MornyLogLevels.ATTENTION)) + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/util/StringEnsure.scala b/src/main/scala/cc/sukazyo/cono/morny/util/StringEnsure.scala index c56dd16..3fd795e 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/util/StringEnsure.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/util/StringEnsure.scala @@ -12,6 +12,9 @@ object StringEnsure { } else str } + def deSensitive (keepStart: Int = 2, keepEnd: Int = 4, sensitive_cover: Char = '*'): String = + (str take keepStart) + (sensitive_cover.toString*(str.length-keepStart-keepEnd)) + (str takeRight keepEnd) + } } diff --git a/src/test/scala/.gitignore b/src/test/scala/.gitignore new file mode 100644 index 0000000..e23fe64 --- /dev/null +++ b/src/test/scala/.gitignore @@ -0,0 +1 @@ +live diff --git a/src/test/scala/live/LiveMain.scala b/src/test/scala/live/LiveMain.scala deleted file mode 100644 index f877dc3..0000000 --- a/src/test/scala/live/LiveMain.scala +++ /dev/null @@ -1,14 +0,0 @@ -package live - -import cc.sukazyo.cono.morny.bot.api.EventEnv -import cc.sukazyo.cono.morny.test.utils.BiliToolTest - -@main def LiveMain (args: String*): Unit = { - - val env: EventEnv = EventEnv(null) - - env provide "abcdefg" - - env use classOf[String] consume { (str: String) => println(str) } onfail { println("no str found in the env") } - -}