mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 08:12:14 +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
|
return driver.bots
|
||||||
|
|
||||||
|
|
||||||
from nonebot.log import logger
|
|
||||||
from nonebot.config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
|
from nonebot.log import logger, default_filter
|
||||||
from nonebot.adapters.cqhttp import Bot as CQBot
|
from nonebot.adapters.cqhttp import Bot as CQBot
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -147,11 +147,13 @@ def init(*, _env_file: Optional[str] = None, **kwargs):
|
|||||||
"""
|
"""
|
||||||
global _driver
|
global _driver
|
||||||
env = Env()
|
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}")
|
config = Config(**kwargs, _env_file=_env_file or f".env.{env.environment}")
|
||||||
|
|
||||||
logger.setLevel(logging.DEBUG if config.debug else logging.INFO)
|
default_filter.level = "DEBUG" if config.debug else "INFO"
|
||||||
logger.debug(f"Loaded config: {config.dict()}")
|
logger.opt(
|
||||||
|
colors=True).debug(f"Loaded <y><b>Config</b></y>: {config.dict()}")
|
||||||
|
|
||||||
DriverClass: Type[Driver] = getattr(importlib.import_module(config.driver),
|
DriverClass: Type[Driver] = getattr(importlib.import_module(config.driver),
|
||||||
"Driver")
|
"Driver")
|
||||||
@ -196,4 +198,4 @@ def run(host: Optional[str] = None,
|
|||||||
get_driver().run(host, port, *args, **kwargs)
|
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
|
@classmethod
|
||||||
def register_adapter(cls, name: str, adapter: Type[Bot]):
|
def register_adapter(cls, name: str, adapter: Type[Bot]):
|
||||||
cls._adapters[name] = adapter
|
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
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
|
@ -84,17 +84,18 @@ class Driver(BaseDriver):
|
|||||||
LOGGING_CONFIG = {
|
LOGGING_CONFIG = {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"disable_existing_loggers": False,
|
"disable_existing_loggers": False,
|
||||||
"formatters": {
|
# "formatters": {
|
||||||
"default": {
|
# "default": {
|
||||||
"()": "logging.Formatter",
|
# "()": "logging.Formatter",
|
||||||
"fmt": "[%(asctime)s %(name)s] %(levelname)s: %(message)s",
|
# "fmt": "[%(asctime)s %(name)s] %(levelname)s: %(message)s",
|
||||||
},
|
# },
|
||||||
},
|
# },
|
||||||
"handlers": {
|
"handlers": {
|
||||||
"default": {
|
"default": {
|
||||||
"formatter": "default",
|
"class": "nonebot.log.LoguruHandler",
|
||||||
"class": "logging.StreamHandler",
|
# "formatter": "default",
|
||||||
"stream": "ext://sys.stdout",
|
# "class": "logging.StreamHandler",
|
||||||
|
# "stream": "ext://sys.stdout",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"loggers": {
|
"loggers": {
|
||||||
|
@ -13,7 +13,7 @@ NoneBot 使用 `loguru`_ 来记录日志信息。
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
# import logging
|
import logging
|
||||||
|
|
||||||
from loguru import logger as logger_
|
from loguru import logger as logger_
|
||||||
|
|
||||||
@ -35,10 +35,6 @@ logger = logger_
|
|||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
|
|
||||||
# 也可以这样
|
|
||||||
import logging
|
|
||||||
logger = logging.getLogger("nonebot")
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# default_handler = logging.StreamHandler(sys.stdout)
|
# default_handler = logging.StreamHandler(sys.stdout)
|
||||||
@ -46,8 +42,45 @@ logger = logger_
|
|||||||
# logging.Formatter("[%(asctime)s %(name)s] %(levelname)s: %(message)s"))
|
# logging.Formatter("[%(asctime)s %(name)s] %(levelname)s: %(message)s"))
|
||||||
# logger.addHandler(default_handler)
|
# 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()
|
logger.remove()
|
||||||
default_format = ("<lvl>{level: <8}</lvl>|"
|
default_filter = Filter()
|
||||||
"<green>{time:MM-DD HH:mm:ss}</green>|"
|
default_format = (
|
||||||
"<lvl>{message}</lvl>")
|
"<g>{time:MM-DD HH:mm:ss}</g>|"
|
||||||
logger.add(sys.stdout, colorize=True, format=default_format)
|
"<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)
|
module = importlib.import_module(module_path)
|
||||||
plugin = Plugin(module_path, module, _tmp_matchers.copy())
|
plugin = Plugin(module_path, module, _tmp_matchers.copy())
|
||||||
plugins[module_path] = plugin
|
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
|
return plugin
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to import \"{module_path}\", error: {e}")
|
logger.opt(colors=True, exception=e).error(
|
||||||
logger.exception(e)
|
f'<r><bg #f8bbd0>Failed to import "{module_path}"</bg #f8bbd0></r>')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -196,10 +196,10 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
|||||||
plugin = Plugin(name, module, _tmp_matchers.copy())
|
plugin = Plugin(name, module, _tmp_matchers.copy())
|
||||||
plugins[name] = plugin
|
plugins[name] = plugin
|
||||||
loaded_plugins.add(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:
|
except Exception as e:
|
||||||
logger.error(f"Failed to import \"{name}\", error: {e}")
|
logger.opt(colors=True, exception=e).error(
|
||||||
logger.exception(e)
|
f'<r><bg #f8bbd0>Failed to import "{name}"</bg #f8bbd0></r>')
|
||||||
return loaded_plugins
|
return loaded_plugins
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user