mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono-python.git
synced 2024-09-27 18:56:20 +08:00
use pyssage as logging backend
This commit is contained in:
parent
5180739617
commit
b8ec9169ab
@ -1,26 +0,0 @@
|
||||
import time
|
||||
|
||||
class LogLevel:
|
||||
def __init__(self, level:float, name:str) :
|
||||
self.level = level
|
||||
self.name = name
|
||||
# def level (self) -> float : return self.level
|
||||
# def name (self) -> str : return self.name
|
||||
|
||||
class LogLevels:
|
||||
trace:LogLevel = LogLevel(-1, "TRAC")
|
||||
debug:LogLevel = LogLevel(0.1, "DBUG")
|
||||
info:LogLevel = LogLevel(0, "INFO")
|
||||
warn:LogLevel = LogLevel(0.5, "WARN")
|
||||
error:LogLevel = LogLevel(1, "ERRO")
|
||||
fatal:LogLevel = LogLevel(10, "FTAL")
|
||||
null:LogLevel = LogLevel(0, "NULL")
|
||||
|
||||
class Log:
|
||||
def __init__ (self, message:str, level:LogLevel) :
|
||||
self.timestamp = time.time_ns()
|
||||
self.level = level
|
||||
self.message = message
|
||||
# def timestamp (self) -> int : return self.timestamp
|
||||
# def level (self) -> LogLevel : return self.level
|
||||
# def message (self) -> str : return self.message
|
@ -1,31 +0,0 @@
|
||||
from datetime import datetime
|
||||
import threading
|
||||
|
||||
from morny.morny_system import Coeur_Def
|
||||
from .log import *
|
||||
|
||||
if Coeur_Def.is_file_logging() :
|
||||
log_file_archive = open(f"./logs/{time.strftime('%Y-%m-%d-%H:%M:%S.log', time.localtime())}", "x")
|
||||
log_file = open("./log__latest.log", "a")
|
||||
|
||||
def output (message:Log) :
|
||||
echo = format_message(message)
|
||||
print(echo)
|
||||
if Coeur_Def.is_file_logging() :
|
||||
log_file.write(echo+"\n")
|
||||
log_file_archive.write(echo+"\n")
|
||||
|
||||
def format_message (log:Log) -> str :
|
||||
|
||||
origins = log.message.split("\n")
|
||||
|
||||
message = ""
|
||||
message += f"[{datetime.fromtimestamp(log.timestamp/1000/1000/1000).strftime('%Y-%m-%d/%H:%M:%S:%f')}]" + \
|
||||
f"[{threading.current_thread().name }]"
|
||||
promptNewLine = "'"*len(message)
|
||||
message += f"[{log.level.name}]{origins[0]}"
|
||||
for i in range(len(origins)-1) :
|
||||
message += f"\n{promptNewLine}[{log.level.name}]{origins[i+1]}"
|
||||
|
||||
return message
|
||||
|
@ -1,10 +0,0 @@
|
||||
from logger.log import Log, LogLevels
|
||||
from logger.log_appender import output
|
||||
|
||||
|
||||
def trace (msg:str) : output(Log(msg, LogLevels.trace))
|
||||
def debug (msg:str) : output(Log(msg, LogLevels.debug))
|
||||
def info (msg:str) : output(Log(msg, LogLevels.info))
|
||||
def warn(msg:str) : output(Log(msg, LogLevels.warn))
|
||||
def error (msg:str) : output(Log(msg, LogLevels.error))
|
||||
def fatal (msg:str) : output(Log(msg, LogLevels.fatal))
|
50
morny/log.py
50
morny/log.py
@ -1,35 +1,21 @@
|
||||
from logger import logger as logger_impl
|
||||
from pyssage.components.restrictions import RestrictionToLog, LevelRestriction
|
||||
from pyssage.appender import ConsoleAppender
|
||||
from pyssage.formatter import SimpleFormatter
|
||||
from pyssage.log.log import levels
|
||||
from pyssage.logger import Logger
|
||||
|
||||
|
||||
restriction: RestrictionToLog = LevelRestriction(levels.INFO)
|
||||
|
||||
logger: Logger = Logger(ConsoleAppender(SimpleFormatter()))
|
||||
logger.restrictions.append(restriction)
|
||||
|
||||
class logger:
|
||||
|
||||
debug_mode: bool = False
|
||||
|
||||
@staticmethod
|
||||
def trace (msg: str):
|
||||
if (logger.debug_mode):
|
||||
logger_impl.trace(msg)
|
||||
|
||||
@staticmethod
|
||||
def debug (msg: str):
|
||||
if (logger.debug_mode):
|
||||
logger_impl.debug(msg)
|
||||
|
||||
@staticmethod
|
||||
def info (msg: str):
|
||||
logger_impl.info(msg)
|
||||
|
||||
@staticmethod
|
||||
def warn (msg: str):
|
||||
logger_impl.warn(msg)
|
||||
|
||||
@staticmethod
|
||||
def error (msg: str):
|
||||
logger_impl.error(msg)
|
||||
|
||||
@staticmethod
|
||||
def fatal (msg: str):
|
||||
logger_impl.fatal(msg)
|
||||
|
||||
|
||||
def set_debug_mode(mode: bool):
|
||||
logger.debug_mode = mode
|
||||
if mode:
|
||||
restriction.min_level = levels.ALL
|
||||
else:
|
||||
restriction.min_level = levels.INFO
|
||||
|
||||
def is_debug_mode () -> bool:
|
||||
return restriction.min_level < levels.INFO
|
||||
|
@ -97,7 +97,7 @@ def main():
|
||||
for __arg in _unknownArgs:
|
||||
logger.warn(f" {__arg}")
|
||||
|
||||
if (logger.debug_mode):
|
||||
if (log.is_debug_mode()):
|
||||
logger.warn("Debug log output enabled.\n It may lower your performance, make sure that you are not in production environment.")
|
||||
logger.debug("Debug log output enabled.")
|
||||
|
||||
|
@ -4,16 +4,4 @@ def strm(*strs: str) -> str:
|
||||
output += strs[i]
|
||||
output += "\n"
|
||||
output += strs[len(strs)-1]
|
||||
return output
|
||||
|
||||
# test for this
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(strm(
|
||||
"Aaa",
|
||||
" bbb",
|
||||
" ccc",
|
||||
"Xxx",
|
||||
" yyy"
|
||||
))
|
||||
|
||||
return output
|
@ -4,11 +4,14 @@ version = "0.1.2+coeur1.0.0-RC3.7"
|
||||
description = "A Coeur-Morny-Cono rewrite by python3"
|
||||
authors = ["Eyre_S <sukazyo@outlook.com>"]
|
||||
readme = "README.md"
|
||||
packages = [{include = "morny_coeur_python"}]
|
||||
packages = [
|
||||
{include = "morny"}
|
||||
]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
python-telegram-bot = "^13.14"
|
||||
pyssage = { git = "https://github.com/suk-ws/pyssage", tag="0.1.0" }
|
||||
|
||||
|
||||
[build-system]
|
||||
|
Loading…
Reference in New Issue
Block a user