Skip to content

Commit

Permalink
Merge pull request #47 from itsFelixH/main-log-rotate
Browse files Browse the repository at this point in the history
Rotating logs + Better linux scheduling guide
  • Loading branch information
InsertDisc authored Jan 15, 2024
2 parents aeeb1f3 + ee0f3d2 commit 3666792
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,15 @@ When creating a schedule or running manually, make sure to run PATTRMM before ru
When using the standalone version you can create a service and run it using a timer (e.g. on a daily basis).
1. Create a service file for PATTRMM:
1. Setup a virtual environment for PATTRMM. Navigate to your pattrmm install folder (`/path/to/pattrmm`) and execute:
```bash
python3 -m venv pmm-venv
```
2. Install the requirements:
```bash
python -m pip install -r requirements.txt
```
4. Create a service file for PATTRMM:

```bash
sudo nano /etc/systemd/system/pattrmm.service
Expand All @@ -547,19 +555,14 @@ When using the standalone version you can create a service and run it using a ti
Environment=LC_ALL=C.UTF-8
Environment=LANG=C.UTF-8
WorkingDirectory=/path/to/pattrmm
ExecStart=/usr/bin/python /path/to/pattrmm/pattrmm.py
Restart=always
RestartSec=10

[Install]
WantedBy=default.target
ExecStart=/path/to/pattrmm/pmm-venv/bin/python /path/to/pattrmm/pattrmm.py
```

Change USER and GROUP to reflect your user and group.

Change /path/to/pattrmm to reflect where you’ve copied/installed PATTRMM.

2. Create a timer file for PATTRMM:
5. Create a timer file for PATTRMM:

```bash
sudo nano /etc/systemd/system/pattrmm.timer
Expand All @@ -579,17 +582,17 @@ When using the standalone version you can create a service and run it using a ti
Unit=pattrmm.service

[Install]
WantedBy=default.target
WantedBy=multi-user.target
```

3. Enable and start the timer/service:
6. Enable and start the timer/service:

```
sudo systemctl enable pattrmm.timer
sudo systemctl start pattrmm.timer
```

This will run pattrmm.py every day at 2 a.m.
The `pattrmm.timer` will trigger the `pattrmm.service`. This will run pattrmm.py every day at 2 a.m.


## Support, ideas and feedback
Expand Down
27 changes: 20 additions & 7 deletions pattrmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
from io import StringIO
import xml.etree.ElementTree as ET
import logging
from logging.handlers import RotatingFileHandler
import sys

LOG_DIR = "data/logs"
MAIN_LOG = "pattrmm.log"

# functions
def verify_or_create_folder(folder_path, folder_name_for_logging):
Expand All @@ -47,6 +50,21 @@ def verify_or_create_file(file_path, file_name_for_logging):
else:
print(file_name_for_logging.capitalize() + " file present...")

def log_setup():
"""Setup log formatter and handler"""
log_path = LOG_DIR + '/' + MAIN_LOG
need_roll = os.path.isfile(log_path)

logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_handler = RotatingFileHandler(log_path, backupCount=5)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s', "%Y-%m-%d %H:%M:%S")
log_handler.setFormatter(log_formatter)
logger.addHandler(log_handler)

# roll log if already present
if need_roll:
logger.handlers[0].doRollover()


print("Verifying files...")
Expand All @@ -57,14 +75,9 @@ def verify_or_create_file(file_path, file_name_for_logging):
# history folder for timestamps
verify_or_create_folder("./data/history", "stats")

# logs folder
# logs setup
verify_or_create_folder("data/logs", "logs")

# pattrmm log file
log_file = "data/logs/pattrmm.log"
verify_or_create_file(log_file, "log")

logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
log_setup()

# preferences folder
verify_or_create_folder("preferences", "preferences")
Expand Down

0 comments on commit 3666792

Please sign in to comment.