Skip to content

Commit

Permalink
Added the logger (2/3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Debianov committed Jul 12, 2024
1 parent 8b3b3a3 commit e227cac
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__pycache__/

# Logs
bot.log
*.log

# Unit test / coverage reports
.pytest_cache/
Expand Down
8 changes: 3 additions & 5 deletions bot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Logic of the all user commands.
"""

import logging.handlers
from typing import Any, Union

import discord
Expand All @@ -20,9 +21,7 @@
from .main import BotConstructor
from .utils import ContextProvider, removeNesting

import logging.handlers

logging = logging.getLogger(__name__)
logger = logging.getLogger(__name__)

class UserLog(commands.Cog):

Expand Down Expand Up @@ -63,8 +62,7 @@ async def _on_command_error(
"указали корректные данные."
f" Необработанная часть сообщения: {ctx.current_argument}")
else:
logging.error(f"Unhadle exception {error}",
extra={"module_func": "commands -> _on_command_error"})
logger.error(f"Unhadle exception {error}")
raise error

@commands.group(aliases=["logs"], invoke_without_command=True)
Expand Down
11 changes: 4 additions & 7 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ def _init_logging() -> None:
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
logging.getLogger('discord.http').setLevel(logging.INFO)

handler = logging.handlers.RotatingFileHandler(
filename='vtc-bot.log',
encoding='utf-8',
maxBytes=32 * 1024 * 1024, # 32 MiB
backupCount=5, # Rotate through 5 files
maxBytes=32 * 1024 * 1024,
backupCount=5,
)
dt_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(
'[{asctime}] [{levelname:<8}] {module_func}: {message}',
dt_fmt, style='{')
formatter = logging.Formatter("%(asctime)s - [%(levelname)s] - "
"%(name)s - (%(filename)s).%(funcName)s(%(lineno)d) - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

Expand Down
2 changes: 2 additions & 0 deletions vtc-bot.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2024-07-13 01:47:29,172 - [INFO] - discord.client - (client.py).login(611) - logging in using static token
2024-07-13 01:47:32,354 - [INFO] - discord.gateway - (gateway.py).received_message(563) - Shard ID None has connected to Gateway (Session ID: f094d08933fe682f7379854f924b1862).

0 comments on commit e227cac

Please sign in to comment.