Skip to content

Commit

Permalink
refactor: extract logging config
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNewThinkTank committed Sep 23, 2024
1 parent 8d2556e commit 2120cf8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
17 changes: 9 additions & 8 deletions logs/model.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
02-08 22:52 root INFO Running Fitness-Tracker/src/model/model.py ...
02-08 22:52 model.area1 INFO data_model: real
02-08 22:52 model.area1 DEBUG db: <TinyDB tables=['weight_training_log'], tables_count=1, default_table_documents_count=0, all_tables_documents_count=['weight_training_log=127']>
02-08 22:52 model.area1 DEBUG table: <Table name='weight_training_log', total=127, storage=<tinydb.storages.JSONStorage object at 0x10d167c90>>
02-08 22:52 model.area2 INFO Exercise: squat
02-08 22:52 model.area2 INFO Workout timestamps: [1639177200.0, 1640386800.0, 1640991600.0, 1642201200.0, 1643238000.0, 1644015600.0, 1644620400.0, 1645830000.0, 1646434800.0, 1647212400.0, 1648504800.0, 1649628000.0, 1650319200.0, 1651183200.0, 1651874400.0, 1652479200.0, 1652997600.0, 1653688800.0, 1655416800.0, 1656280800.0, 1656712800.0, 1657576800.0, 1658008800.0, 1658354400.0, 1658959200.0, 1660600800.0, 1661464800.0, 1662760800.0, 1663279200.0, 1663711200.0, 1673564400.0, 1674342000.0, 1674946800.0, 1675724400.0]
02-08 22:52 model.area2 INFO 1 RM estimates: [102.86, 91.43, 102.86, 108.11, 108.11, 108.11, 108.11, 108.11, 114.29, 114.29, 117.65, 117.65, 117.65, 117.65, 117.65, 125.0, 125.0, 125.0, 133.33, 121.21, 53.33, 60.0, 66.67, 73.33, 80.0, 80.0, 80.0, 80.0, 80.0, 80.0, 91.43, 91.43, 91.43, 91.43]
02-08 22:52 model.area2 INFO Volume: [2880.0, 1600.0, 1800.0, 2000.0, 2000.0, 2000.0, 3200.0, 4000.0, 2400.0, 3200.0, 3200.0, 2400.0, 3000.0, 3000.0, 3600.0, 2400.0, 3000.0, 3000.0, 1440.0, 2400.0, 4000.0, 4500.0, 5000.0, 5500.0, 6000.0, 6000.0, 6000.0, 6000.0, 6000.0, 6000.0, 1200.0, 1200.0, 1200.0, 1200.0]
09-23 13:39 root INFO Logger initialized
09-23 13:39 root INFO Running fitness-tracker/src/model/model.py ...
09-23 13:39 model.area1 INFO data_model: real
09-23 13:39 model.area1 DEBUG db: <TinyDB tables=['weight_training_log'], tables_count=1, default_table_documents_count=0, all_tables_documents_count=['weight_training_log=52']>
09-23 13:39 model.area1 DEBUG table: <Table name='weight_training_log', total=52, storage=<custom_storage.YAMLStorage object at 0x12e569e10>>
09-23 13:39 model.area2 INFO Exercise: squat
09-23 13:39 model.area2 INFO Workout timestamps: [1704992400.0, 1706288400.0, 1708189200.0, 1708794000.0, 1709917200.0, 1711213200.0, 1711731600.0, 1712854800.0, 1714842000.0, 1718211600.0, 1720198800.0, 1720630800.0, 1721149200.0, 1722358800.0, 1723222800.0, 1725728400.0]
09-23 13:39 model.area2 INFO 1 RM estimates: [87.5, 87.5, 87.5, 93.33, 87.5, 120.0, 85.71, 81.25, 80.0, 85.71, 87.5, 50.0, 87.5, 68.75, 120.0, 75.0]
09-23 13:39 model.area2 INFO Volume: [1680.0, 1680.0, 1680.0, 2100.0, 1680.0, 3240.0, 2160.0, 1560.0, 1800.0, 2160.0, 1680.0, 960.0, 1680.0, 1650.0, 2700.0, 1620.0]
21 changes: 4 additions & 17 deletions src/CRUD/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,23 @@ def main() -> None:

import argparse
import logging
from helpers.logger_config import setup_logger, log_running_file

parser = argparse.ArgumentParser()
setup_logger(log_file="insert.log")
log_running_file(__file__)

parser = argparse.ArgumentParser()
parser.add_argument("--file_format", type=str, default='json')
parser.add_argument("--datatype", type=str, required=True)
parser.add_argument("--dates", type=str)
parser.add_argument("--workout_number", type=int)

args = parser.parse_args()

file_format = args.file_format # json or yml
datatype = args.datatype # real/simulated
dates = args.dates # 2022-02-03,2022-02-05
workout_number = args.workout_number

Path("logs/").mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
datefmt="%m-%d %H:%M",
filename="logs/insert.log",
filemode="w",
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
console.setFormatter(formatter)
logging.getLogger("").addHandler(console)
logging.info("Running %s ...", "/".join(__file__.split("/")[-4:]))

db, table, _ = set_db_and_table(datatype)
ic(db)

Expand Down
32 changes: 32 additions & 0 deletions src/helpers/logger_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging
from pathlib import Path


def setup_logger(log_file: str = "insert.log", log_dir: str = "logs/"):
# Create the log directory if it doesn't exist
Path(log_dir).mkdir(parents=True, exist_ok=True)

# Configure the logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
datefmt="%m-%d %H:%M",
filename=f"{log_dir}/{log_file}",
filemode="w",
)

# Console handler for the logger
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
console.setFormatter(formatter)

# Add console handler to the root logger
logging.getLogger("").addHandler(console)

logging.info("Logger initialized")


def log_running_file(file_path):
"""Optional utility for logging the running file."""
logging.info("Running %s ...", "/".join(file_path.split("/")[-4:]))
20 changes: 5 additions & 15 deletions src/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
import logging
import os
import pathlib
# import pathlib
import sys
from typing import Final

Expand Down Expand Up @@ -182,21 +182,11 @@ def get_data(df, y_col="1RM") -> tuple[list[float], list[float]]:
def main() -> None:
"""Prepare dfs, calc 1RM and do linear regression."""

pathlib.Path("logs/").mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
datefmt="%m-%d %H:%M",
filename="logs/model.log",
filemode="w",
)
from helpers.logger_config import setup_logger, log_running_file

setup_logger(log_file="model.log")
log_running_file(__file__)

console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
console.setFormatter(formatter)
logging.getLogger("").addHandler(console)
logging.info("Running %s ...", "/".join(__file__.split("/")[-4:]))
logger1 = logging.getLogger("model.area1")
logger2 = logging.getLogger("model.area2")

Expand Down

0 comments on commit 2120cf8

Please sign in to comment.