1- import os
21import logging
2+ import os
33import pathlib
44from datetime import datetime
55
1313
1414from .models import Restaurant , Review , engine
1515
16- logging .basicConfig (level = logging .DEBUG )
17-
16+ # Setup logger and Azure Monitor:
17+ logger = logging .getLogger ("app" )
18+ logger .setLevel (logging .INFO )
1819if os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" ):
1920 configure_azure_monitor ()
2021
21- print ("Setting up FastAPI app..." )
22- logging .warning ("From logging: Setting up FastAPI app..." )
2322
23+ # Setup FastAPI app:
2424app = FastAPI ()
2525parent_path = pathlib .Path (__file__ ).parent .parent
2626app .mount ("/mount" , StaticFiles (directory = parent_path / "static" ), name = "static" )
2929# Use relative path for url_for, so that it works behind a proxy like Codespaces
3030templates .env .globals ["url_for" ] = app .url_path_for
3131
32+
3233# Dependency to get the database session
3334def get_db_session ():
3435 with Session (engine ) as session :
@@ -37,8 +38,7 @@ def get_db_session():
3738
3839@app .get ("/" , response_class = HTMLResponse )
3940async def index (request : Request , session : Session = Depends (get_db_session )):
40- print ("root called" )
41- logging .warning ("From logging: root called" )
41+ logger .info ("root called" )
4242 statement = (
4343 select (Restaurant , func .avg (Review .rating ).label ("avg_rating" ), func .count (Review .id ).label ("review_count" ))
4444 .outerjoin (Review , Review .restaurant == Restaurant .id )
@@ -59,7 +59,7 @@ async def index(request: Request, session: Session = Depends(get_db_session)):
5959
6060@app .get ("/create" , response_class = HTMLResponse )
6161async def create_restaurant (request : Request ):
62- print ("Request for add restaurant page received" )
62+ logger . info ("Request for add restaurant page received" )
6363 return templates .TemplateResponse ("create_restaurant.html" , {"request" : request })
6464
6565
@@ -68,7 +68,7 @@ async def add_restaurant(
6868 request : Request , restaurant_name : str = Form (...), street_address : str = Form (...), description : str = Form (...),
6969 session : Session = Depends (get_db_session )
7070):
71- print ( f "name: { restaurant_name } address: { street_address } description: { description } " )
71+ logger . info ( "name: %s address: %s description: %s" , restaurant_name , street_address , description )
7272 restaurant = Restaurant ()
7373 restaurant .name = restaurant_name
7474 restaurant .street_address = street_address
0 commit comments