Files
telegram-helper-bot/logs/custom_logger.py
Andrey 8f338196b7 Refactor Docker and configuration files for improved structure and functionality
- Updated `.dockerignore` to include additional development and temporary files, enhancing build efficiency.
- Modified `.gitignore` to remove unnecessary entries and streamline ignored files.
- Enhanced `docker-compose.yml` with health checks, resource limits, and improved environment variable handling for better service management.
- Refactored `Dockerfile.bot` to utilize a multi-stage build for optimized image size and security.
- Improved `Makefile` with new commands for deployment, migration, and backup, along with enhanced help documentation.
- Updated `requirements.txt` to include new dependencies for environment variable management.
- Refactored metrics handling in the bot to ensure proper initialization and collection.
2025-08-29 23:15:06 +03:00

45 lines
1.3 KiB
Python

import datetime
import os
import sys
from loguru import logger
# Remove default handler
logger.remove()
# Check if running in Docker/container
is_container = os.path.exists('/.dockerenv') or os.getenv('DOCKER_CONTAINER') == 'true'
if is_container:
# In container: log to stdout/stderr
logger.add(
sys.stdout,
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name} | {line} | {message}",
level=os.getenv("LOG_LEVEL", "INFO"),
colorize=True
)
logger.add(
sys.stderr,
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name} | {line} | {message}",
level="ERROR",
colorize=True
)
else:
# Local development: log to files
current_dir = os.path.dirname(os.path.abspath(__file__))
if not os.path.exists(current_dir):
os.makedirs(current_dir)
today = datetime.date.today().strftime('%Y-%m-%d')
filename = f'{current_dir}/helper_bot_{today}.log'
logger.add(
filename,
rotation="00:00",
retention=f"{os.getenv('LOG_RETENTION_DAYS', '30')} days",
format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {name} | {line} | {message}",
level=os.getenv("LOG_LEVEL", "INFO"),
)
# Bind logger name
logger = logger.bind(name='main_log')