Untitled conversation
- User
# main.py import os from logging import getLogger from logging.config import dictConfig import discord from discord.ext import commands # Logging logging_settings = { "version": 1, "disable_existing_loggers": False, "formatters": { "color": { "()": "colorlog.ColoredFormatter", "format": "%(log_color)s%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]:%(reset)s %(message)s", "log_colors": { "DEBUG": "white", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white" }, "style": "%" }, "simple": { "format": "%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]: %(message)s" } }, "handlers": { "consoleHandler": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "color", "stream": "ext://sys.stdout" }, "fileHandler": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "formatter": "simple", "filename": "application.log", "maxBytes": 10485760, "backupCount": 5, "encoding": "utf-8" } }, "root": { "level": "DEBUG", "handlers": ["consoleHandler", "fileHandler"] } } dictConfig(logging_settings) logger = getLogger(__name__)"D:\LLM API\Nyabi\venv\Scripts\python.exe" "D:\LLM API\Nyabi\main.py" 2025-05-19 21:24:32,388 discord.client:318 __init__ [WARNING]: PyNaCl is not installed, voice will NOT be supported 2025-05-19 21:24:32,391 discord.client:643 login [INFO]: logging in using static token [2025-05-19 21:24:32] [INFO ] discord.client: logging in using static token 2025-05-19 21:24:33,801 discord.gateway:562 received_message [INFO]: Shard ID None has connected to Gateway (Session ID: 061ea72f2210795798de7da89203a05b). [2025-05-19 21:24:33] [INFO ] discord.gateway: Shard ID None has connected to Gateway (Session ID: 061ea72f2210795798de7da89203a05b). 2025-05-19 21:24:35,809 src.cogs.commands:13 __init__ [INFO]: hi from CommandsCog! 2025-05-19 21:24:36,191 __main__:77 on_ready [INFO]: Successfully logged in as Nyabi#3449discord.py側のログが二重になっていたり、なっていなかったりする。この一貫性のなさの原因は?
Assistant
Discord.py のログが二重に出力されたり、一貫性がなかったりするのは、複数のロガーが同じメッセージを処理しているためです。具体的には、Discord.py がデフォルトのロギング設定を使用しており、そのログメッセージがあなたのカスタムロギング設定とも重複して処理されている可能性があります。
これを解決するために、Discord.py のロギング設定を無効にし、すべてのログをあなたのカスタムロギング設定に統一する必要があります。以下は、Discord.py のロギング設定を無効にする方法を示しています。
import os from logging import getLogger, getLogger as get_logger, NullHandler from logging.config import dictConfig import discord from discord.ext import commands # Logging logging_settings = { "version": 1, "disable_existing_loggers": False, "formatters": { "color": { "()": "colorlog.ColoredFormatter", "format": "%(log_color)s%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]:%(reset)s %(message)s", "log_colors": { "DEBUG": "white", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white" }, "style": "%" }, "simple": { "format": "%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]: %(message)s" } }, "handlers": { "consoleHandler": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "color", "stream": "ext://sys.stdout" }, "fileHandler": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "formatter": "simple", "filename": "application.log", "maxBytes": 10485760, "backupCount": 5, "encoding": "utf-8" } }, "root": { "level": "DEBUG", "handlers": ["consoleHandler", "fileHandler"] } } dictConfig(logging_settings) logger = getLogger(__name__) # Disable discord.py's default logging discord_logger = get_logger('discord') discord_logger.setLevel(logging.CRITICAL) discord_logger.addHandler(NullHandler()) # Your bot code continues hereこのコードでは、
discordロガーのレベルをCRITICALに設定し、NullHandlerを追加して、Discord.py のデフォルトのロギングを無効にしています。これにより、Discord.py からのログメッセージが二重に出力されることを防ぎ、すべてのログがあなたのカスタムロギング設定を通じて一貫して出力されるようになります。- User
discord.pyは何かしら特殊な方法でログを出力している?
Assistant
Discord.py は Python の標準ロギングモジュールを使用してログを出力していますが、特殊な点として、ライブラリ内のさまざまなコンポーネント(
discord.client,discord.gatewayなど)が個別のロガーを使用しており、これらのロガーはデフォルトで標準出力にログを出力するように設定されています。Discord.py のロギング設定は、ライブラリの内部で次のように行われています:
import logging logger = logging.getLogger('discord') logger.setLevel(logging.INFO) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]: %(message)s') ch.setFormatter(formatter) logger.addHandler(ch)この設定により、
discordロガーは INFO レベル以上のログメッセージを標準出力に送信します。あなたのカスタムロギング設定と競合しないようにするには、Discord.py のデフォルトのロギング設定を無効にし、すべてのログをあなたのカスタムロギング設定に統一する必要があります。先ほど示したように、
NullHandlerを追加して Discord.py のデフォルトのロギングを無効にすることができます。import os from logging import getLogger, getLogger as get_logger, NullHandler from logging.config import dictConfig import discord from discord.ext import commands # Logging logging_settings = { "version": 1, "disable_existing_loggers": False, "formatters": { "color": { "()": "colorlog.ColoredFormatter", "format": "%(log_color)s%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]:%(reset)s %(message)s", "log_colors": { "DEBUG": "white", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white" }, "style": "%" }, "simple": { "format": "%(asctime)s %(name)s:%(lineno)d %(funcName)s [%(levelname)s]: %(message)s" } }, "handlers": { "consoleHandler": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "color", "stream": "ext://sys.stdout" }, "fileHandler": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "formatter": "simple", "filename": "application.log", "maxBytes": 10485760, "backupCount": 5, "encoding": "utf-8" } }, "root": { "level": "DEBUG", "handlers": ["consoleHandler", "fileHandler"] } } dictConfig(logging_settings) logger = getLogger(__name__) # Disable discord.py's default logging discord_logger = get_logger('discord') discord_logger.setLevel(logging.CRITICAL) discord_logger.addHandler(NullHandler()) # Your bot code continues hereこれにより、Discord.py からのログメッセージが二重に出力されることを防ぎ、すべてのログがあなたのカスタムロギング設定を通じて一貫して出力されるようになります。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.