Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logging persistent by logging to files on host #43

Merged
merged 15 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@ MODE=dev|prod


### Discord env var

# You can follow https://realpython.com/how-to-make-a-discord-bot-python/#creating-an-application to get a discord token
DISCORD_TOKEN=XXX

# The ID of the channel where the bot should send the Root-Me new solves and news
# You can follow https://turbofuture.com/internet/Discord-Channel-ID to get your channel ID
CHANNEL_ID=XXX


### Logging
# accept integer or valid strings as described in the doc: https://docs.python.org/3/library/logging.html#logging.Logger.setLevel
LOG_LEVEL=INFO
LOG_FOLDER=path/to/log/folder

# [OPTIONAL] in Bytes, the maximum size a log file can grow (default is 10e6)
LOG_FILE_SIZE_MAX=
# [OPTIONAL] the maximum number of log file generated, if exceeded the older are removed (default is 5)
LOG_FILES_NUMBER=
# Note that if either of the previous variables are set to 0 the log file will grow indefinitely


### Root-me API
# get an API Key by logging in on Root-me then go to 'my settings'
API_KEY_ROOTME=<Root-Me API key>
API_URL=https://api.www.root-me.org/

# [OPTIONAL] the maximum number of time a single request is retried (if relevant)
MAX_API_ATTEMPT=5

### Bot Configuration
REFRESH_DELAY= # in seconds ! (default value if not set here is 10 seconds)
# [OPTIONAL] in seconds, the delay between new solve checking (default is 10)
REFRESH_DELAY=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
**/.env
**/.env.prod
**/.env.dev

# Logs
logs/**
9 changes: 8 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ NAME="root-pythia"
run__prod () {
# prod mode
docker build --file Dockerfile -t ${NAME}:latest .
docker run --rm --interactive --tty --detach --env-file .env.prod --name ${NAME} ${NAME}:latest

source ./.env.prod
docker run --rm --interactive --tty \
--detach \
--volume $(realpath -P ${LOG_FOLDER}):/opt/${NAME}/logs \
--env-file .env.prod \
--name ${NAME} \
${NAME}:latest
}

run__dev () {
Expand Down
46 changes: 46 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
import logging
import logging.handlers
from os import getenv
import sys

import discord

from bot import BOT as root_pythia

### Default global variables
# in bytes
DEFAULT_LOG_FILE_SIZE_MAX = 10e6
# the number of files the rotating file handler will generate at max
# if exceeded it removes the older one
# see https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler
DEFAULT_LOG_FILES_NUMBER = 5


### Global variables
MODE = getenv("MODE")
DISCORD_TOKEN = getenv("DISCORD_TOKEN")
LOG_LEVEL = getenv("LOG_LEVEL")
if LOG_LEVEL.isnumeric():
LOG_LEVEL = int(LOG_LEVEL)

LOG_FILE_SIZE_MAX = getenv("LOG_FILE_SIZE_MAX") or DEFAULT_LOG_FILE_SIZE_MAX
try:
LOG_FILE_SIZE_MAX = int(LOG_FILE_SIZE_MAX)
except ValueError as exc:
logging.exception("LOG_FILE_SIZE_MAX is not an integer")
sys.exit(1)

LOG_FILES_NUMBER = getenv("LOG_FILES_NUMBER") or DEFAULT_LOG_FILES_NUMBER
try:
LOG_FILES_NUMBER = int(LOG_FILES_NUMBER)
except ValueError as exc:
logging.exception("LOG_FILES_NUMBER is not an integer")
sys.exit(1)


def main():
# Setup a beautiful root logger
discord.utils.setup_logging(root=True, level=LOG_LEVEL)

root_logger = logging.getLogger()
discord_log_formatter = root_logger.handlers[0]

# Add a file handler to the root logger
file_handler = logging.handlers.RotatingFileHandler(
"./logs/RootPythia.log", mode="a", maxBytes=LOG_FILE_SIZE_MAX, backupCount=LOG_FILES_NUMBER
)
file_handler.setFormatter(discord_log_formatter)
root_logger.addHandler(file_handler)
logging.info("FileHandler added to root logger it will write to '%s'", file_handler.stream.name)

# Add a specific file handler to save warnings and errors
warning_file_handler = logging.FileHandler(
"./logs/RootPythiaErrors.log", mode="a"
)
warning_file_handler.setLevel(logging.WARNING)
warning_file_handler.setFormatter(discord_log_formatter)
root_logger.addHandler(warning_file_handler)

# are these call secure??
logging.debug("discord token: %s", DISCORD_TOKEN)

Expand Down