mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-01 01:25:07 +08:00
🔊 change log types
This commit is contained in:
parent
1f09023f66
commit
858098b946
@ -109,8 +109,8 @@ def get_bots() -> Dict[str, Bot]:
|
||||
return driver.bots
|
||||
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Env, Config
|
||||
from nonebot.log import logger, default_filter
|
||||
from nonebot.adapters.cqhttp import Bot as CQBot
|
||||
|
||||
try:
|
||||
@ -147,11 +147,13 @@ def init(*, _env_file: Optional[str] = None, **kwargs):
|
||||
"""
|
||||
global _driver
|
||||
env = Env()
|
||||
logger.debug(f"Current Env: {env.environment}")
|
||||
logger.opt(
|
||||
colors=True).debug(f"Current <y><b>Env: {env.environment}</b></y>")
|
||||
config = Config(**kwargs, _env_file=_env_file or f".env.{env.environment}")
|
||||
|
||||
logger.setLevel(logging.DEBUG if config.debug else logging.INFO)
|
||||
logger.debug(f"Loaded config: {config.dict()}")
|
||||
default_filter.level = "DEBUG" if config.debug else "INFO"
|
||||
logger.opt(
|
||||
colors=True).debug(f"Loaded <y><b>Config</b></y>: {config.dict()}")
|
||||
|
||||
DriverClass: Type[Driver] = getattr(importlib.import_module(config.driver),
|
||||
"Driver")
|
||||
@ -196,4 +198,4 @@ def run(host: Optional[str] = None,
|
||||
get_driver().run(host, port, *args, **kwargs)
|
||||
|
||||
|
||||
from nonebot.plugin import load_plugins, get_loaded_plugins
|
||||
from nonebot.plugin import load_plugin, load_plugins, get_loaded_plugins
|
||||
|
@ -20,7 +20,8 @@ class BaseDriver(abc.ABC):
|
||||
@classmethod
|
||||
def register_adapter(cls, name: str, adapter: Type[Bot]):
|
||||
cls._adapters[name] = adapter
|
||||
logger.debug(f'Succeeded to load adapter "{name}"')
|
||||
logger.opt(
|
||||
colors=True).debug(f'Succeeded to load adapter "<y>{name}</y>"')
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
|
@ -84,17 +84,18 @@ class Driver(BaseDriver):
|
||||
LOGGING_CONFIG = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"default": {
|
||||
"()": "logging.Formatter",
|
||||
"fmt": "[%(asctime)s %(name)s] %(levelname)s: %(message)s",
|
||||
},
|
||||
},
|
||||
# "formatters": {
|
||||
# "default": {
|
||||
# "()": "logging.Formatter",
|
||||
# "fmt": "[%(asctime)s %(name)s] %(levelname)s: %(message)s",
|
||||
# },
|
||||
# },
|
||||
"handlers": {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stdout",
|
||||
"class": "nonebot.log.LoguruHandler",
|
||||
# "formatter": "default",
|
||||
# "class": "logging.StreamHandler",
|
||||
# "stream": "ext://sys.stdout",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
|
@ -13,7 +13,7 @@ NoneBot 使用 `loguru`_ 来记录日志信息。
|
||||
"""
|
||||
|
||||
import sys
|
||||
# import logging
|
||||
import logging
|
||||
|
||||
from loguru import logger as logger_
|
||||
|
||||
@ -35,10 +35,6 @@ logger = logger_
|
||||
.. code-block:: python
|
||||
|
||||
from nonebot.log import logger
|
||||
|
||||
# 也可以这样
|
||||
import logging
|
||||
logger = logging.getLogger("nonebot")
|
||||
"""
|
||||
|
||||
# default_handler = logging.StreamHandler(sys.stdout)
|
||||
@ -46,8 +42,45 @@ logger = logger_
|
||||
# logging.Formatter("[%(asctime)s %(name)s] %(levelname)s: %(message)s"))
|
||||
# logger.addHandler(default_handler)
|
||||
|
||||
|
||||
class Filter:
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.level = "DEBUG"
|
||||
|
||||
def __call__(self, record):
|
||||
record["name"] = record["name"].split(".")[0]
|
||||
levelno = logger.level(self.level).no
|
||||
return record["level"].no >= levelno
|
||||
|
||||
|
||||
class LoguruHandler(logging.Handler):
|
||||
|
||||
def emit(self, record):
|
||||
try:
|
||||
level = logger.level(record.levelname).name
|
||||
except ValueError:
|
||||
level = record.levelno
|
||||
|
||||
frame, depth = logging.currentframe(), 2
|
||||
while frame.f_code.co_filename == logging.__file__:
|
||||
frame = frame.f_back
|
||||
depth += 1
|
||||
|
||||
logger.opt(depth=depth,
|
||||
exception=record.exc_info).log(level, record.getMessage())
|
||||
|
||||
|
||||
logger.remove()
|
||||
default_format = ("<lvl>{level: <8}</lvl>|"
|
||||
"<green>{time:MM-DD HH:mm:ss}</green>|"
|
||||
"<lvl>{message}</lvl>")
|
||||
logger.add(sys.stdout, colorize=True, format=default_format)
|
||||
default_filter = Filter()
|
||||
default_format = (
|
||||
"<g>{time:MM-DD HH:mm:ss}</g>|"
|
||||
"<lvl>{level: ^8}</lvl>|"
|
||||
"<c><u>{name}</u></c>| "
|
||||
# "<c>{function}:{line}</c>| "
|
||||
"{message}")
|
||||
logger.add(sys.stdout,
|
||||
colorize=True,
|
||||
diagnose=False,
|
||||
filter=default_filter,
|
||||
format=default_format)
|
||||
|
@ -170,11 +170,11 @@ def load_plugin(module_path: str) -> Optional[Plugin]:
|
||||
module = importlib.import_module(module_path)
|
||||
plugin = Plugin(module_path, module, _tmp_matchers.copy())
|
||||
plugins[module_path] = plugin
|
||||
logger.info(f"Succeeded to import \"{module_path}\"")
|
||||
logger.opt(colors=True).info(f'Succeeded to import "{module_path}"')
|
||||
return plugin
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to import \"{module_path}\", error: {e}")
|
||||
logger.exception(e)
|
||||
logger.opt(colors=True, exception=e).error(
|
||||
f'<r><bg #f8bbd0>Failed to import "{module_path}"</bg #f8bbd0></r>')
|
||||
return None
|
||||
|
||||
|
||||
@ -196,10 +196,10 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
plugin = Plugin(name, module, _tmp_matchers.copy())
|
||||
plugins[name] = plugin
|
||||
loaded_plugins.add(plugin)
|
||||
logger.info(f"Succeeded to import \"{name}\"")
|
||||
logger.opt(colors=True).info(f'Succeeded to import "<y>{name}</y>"')
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to import \"{name}\", error: {e}")
|
||||
logger.exception(e)
|
||||
logger.opt(colors=True, exception=e).error(
|
||||
f'<r><bg #f8bbd0>Failed to import "{name}"</bg #f8bbd0></r>')
|
||||
return loaded_plugins
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user