Skip to content

Commit

Permalink
implements stdout log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed May 30, 2024
1 parent 5b5fa81 commit 9ac550b
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 40 deletions.
24 changes: 17 additions & 7 deletions lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# lib/__init__.py
import logging
import sys

logging.basicConfig(
level=logging.INFO,
filename='app.log',
filemode='a',
format='%(asctime)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Creates log handlers
file_handler = logging.FileHandler("app.log")
stdout_handler = logging.StreamHandler(sys.stdout)

# Creates log formatter and add it to handlers
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
stdout_handler.setFormatter(formatter)

if not logger.hasHandlers():
logger.addHandler(file_handler)
logger.addHandler(stdout_handler)


def parse_error(error):
Expand All @@ -15,4 +25,4 @@ def parse_error(error):
return f"{exc_type} exception: {exc_obj}"


from .api import app # noqa
from .api import app # noqa
4 changes: 1 addition & 3 deletions lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from fastapi.openapi.utils import get_openapi
from fastapi.responses import RedirectResponse, JSONResponse

from lib import logging, parse_error
from lib import logger, parse_error
from lib.routes import flight, environment, motor, rocket

logger = logging.getLogger(__name__)

app = FastAPI(
swagger_ui_parameters={
"defaultModelsExpandDepth": 0,
Expand Down
4 changes: 1 addition & 3 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rocketpy.environment.environment import Environment as RocketPyEnvironment
from fastapi import HTTPException, status

from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.environment import Env
from lib.repositories.environment import EnvRepository
from lib.views.environment import (
Expand All @@ -17,8 +17,6 @@
EnvPickle,
)

logger = logging.getLogger(__name__)


class EnvController:
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import jsonpickle

from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.rocket import Rocket, RocketOptions
from lib.models.motor import MotorKinds
from lib.models.flight import Flight
Expand All @@ -32,8 +32,6 @@
from lib.controllers.environment import EnvController
from lib.controllers.rocket import RocketController

logger = logging.getLogger(__name__)


class FlightController:
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rocketpy.motors.hybrid_motor import HybridMotor
import jsonpickle

from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.motor import Motor, MotorKinds
from lib.repositories.motor import MotorRepository
from lib.views.motor import (
Expand All @@ -17,8 +17,6 @@
MotorPickle,
)

logger = logging.getLogger(__name__)


class MotorController:
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/controllers/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from rocketpy.rocket.aero_surface import Tail as RocketPyTail

from lib import logging, parse_error
from lib import logger, parse_error
from lib.controllers.motor import MotorController
from lib.models.rocket import Rocket, RocketOptions
from lib.models.motor import MotorKinds
Expand All @@ -36,8 +36,6 @@
RocketPickle,
)

logger = logging.getLogger(__name__)


class RocketController:
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/repositories/environment.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Union
from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.environment import Env
from lib.repositories.repo import Repository

logger = logging.getLogger(__name__)


class EnvRepository(Repository):
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/repositories/flight.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Union
from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.flight import Flight
from lib.repositories.repo import Repository

logger = logging.getLogger(__name__)


class FlightRepository(Repository):
"""
Expand Down
4 changes: 1 addition & 3 deletions lib/repositories/motor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Union
from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.motor import Motor
from lib.repositories.repo import Repository

logger = logging.getLogger(__name__)


class MotorRepository(Repository):
"""
Expand Down
10 changes: 4 additions & 6 deletions lib/repositories/repo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import asyncio
import logging
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.server_api import ServerApi
from lib import logger
from lib.secrets import Secrets

logger = logging.getLogger(__name__)


class Repository:
"""
Expand Down Expand Up @@ -37,7 +35,7 @@ def _on_init_done(self, future):
try:
future.result()
except Exception as e:
logging.error("Initialization failed: %s", e, exc_info=True)
logger.error("Initialization failed: %s", e, exc_info=True)
raise e from e

async def _async_init(self):
Expand Down Expand Up @@ -68,9 +66,9 @@ def _initialize_connection(self):
serverSelectionTimeoutMS=15000,
)
self._collection = self._client.rocketpy[self._collection_name]
logging.info("MongoDB client initialized for %s", self.__class__)
logger.info("MongoDB client initialized for %s", self.__class__)
except Exception as e:
logging.error(
logger.error(
f"Failed to initialize MongoDB client: {e}", exc_info=True
)
raise ConnectionError(
Expand Down
4 changes: 1 addition & 3 deletions lib/repositories/rocket.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Union
from lib import logging, parse_error
from lib import logger, parse_error
from lib.models.rocket import Rocket
from lib.repositories.repo import Repository

logger = logging.getLogger(__name__)


class RocketRepository(Repository):
"""
Expand Down

0 comments on commit 9ac550b

Please sign in to comment.