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

Move logging setup outside lifespan and db initialisation logic #309

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## [unreleased]
### Changed
### CHanged
- Updated PR template
- Generalised issue templates to make them more user-friendly for people outside our organisation
- Moved logging setup out of app lifespan and db initialisation logic
### Fixed
- Updated version of external images used in GitHub actions

Expand Down
18 changes: 9 additions & 9 deletions src/chanjo2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
(overview.router, "overview"),
]

# Configure logging
northwestwitch marked this conversation as resolved.
Show resolved Hide resolved
console_formatter = uvicorn.logging.ColourizedFormatter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an informative type hint for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's an instance of the class uvicorn.logging.ColourizedFormatter. I think it's redundant to write it down

"{levelprefix} {asctime} : {message}", style="{", use_colors=True
)
if LOG.handlers:
LOG.handlers[0].setFormatter(console_formatter)
else:
logging.basicConfig()


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have this in a function in a separate module from main to keep it as free from logic as possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, good suggestion! 👍🏻

def create_db_and_tables():
Base.metadata.create_all(engine)
Expand Down Expand Up @@ -56,15 +65,6 @@ def configure_static(app):


async def startup_db():
# Configure logging
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bad place for the logging setup anyway, being a function that starts the database

console_formatter = uvicorn.logging.ColourizedFormatter(
"{levelprefix} {asctime} : {message}", style="{", use_colors=True
)
if LOG.handlers:
LOG.handlers[0].setFormatter(console_formatter)
else:
logging.basicConfig()

# Create database tables
create_db_and_tables()

Expand Down
Loading