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 のロガーを自身のロギング設定で完全に制御することです。 具体的には以下の手順で行います。
1. discord.py のデフォルトロガーを無効化する:
discord.py はデフォルトでロガーを設定しており、これが二重のログの原因となっています。これを無効化するには、discord.py のクライアントを作成する前に、discord.py のロガーのレベルを CRITICAL に設定します。これにより、discord.py 自体のログ出力は抑制されます。
import logging # discord.py のロガーを無効化 logging.getLogger('discord').setLevel(logging.CRITICAL)2. discord.py のイベントハンドラから自身のロガーを使用する:
discord.py のイベントハンドラ (例:
on_ready,on_message) 内でログを出力する際に、discord.py のロガーではなく、自身のロガー (logger) を使用するように変更します。# 例: on_ready イベントハンドラ @client.event async def on_ready(): logger.info(f"Successfully logged in as {client.user.name}#{client.user.discriminator}")3.
commands.Botを使用している場合、commands.Cog内のログも自身のロガーを使用するように変更する:commands.Cog内でログを出力する場合も、同様に discord.py のロガーではなく、自身のロガーを使用するように変更します。# 例: CommandsCog 内のログ class CommandsCog(commands.Cog): def __init__(self, bot): super().__init__() logger.info("hi from CommandsCog!")変更後の
main.pyの例:# main.py import os from logging import getLogger from logging.config import dictConfig import discord from discord.ext import commands import logging # logging モジュールをインポート # 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__) # discord.py のロガーを無効化 logging.getLogger('discord').setLevel(logging.CRITICAL) intents = discord.Intents.default() intents.message_content = True client = commands.Bot(command_prefix="!", intents=intents) @client.event async def on_ready(): logger.info(f"Successfully logged in as {client.user.name}#{client.user.discriminator}") class CommandsCog(commands.Cog): def __init__(self, bot): super().__init__() logger.info("hi from CommandsCog!") client.add_cog(CommandsCog(client)) client.run("YOUR_BOT_TOKEN")補足:
logging.getLogger('discord').setLevel(logging.CRITICAL)は、discord.py のロガーがログを出力しないように設定します。logger.info(...)のように、自身のロガーを使用することで、設定したフォーマットでログが出力されるようになります。YOUR_BOT_TOKENは実際のボットトークンに置き換えてください。
これらの手順を実行することで、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.