Skip to content

Commit

Permalink
rewrite logger to support custom config files (#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Apr 16, 2024
1 parent cba076b commit 6bd5a82
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 287 deletions.
1 change: 1 addition & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- 9091:9000
environment:
ALLOW_SIGNUP: "false"
LOG_LEVEL: "DEBUG"

DB_ENGINE: sqlite # Optional: 'sqlite', 'postgres'
# =====================================
Expand Down
7 changes: 4 additions & 3 deletions docker/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ init
GUNICORN_PORT=${API_PORT:-9000}

# Start API
hostip=`/sbin/ip route|awk '/default/ { print $3 }'`
HOST_IP=`/sbin/ip route|awk '/default/ { print $3 }'`

if [ "$WEB_GUNICORN" = 'true' ]; then
echo "Starting Gunicorn"
exec gunicorn mealie.app:app -b 0.0.0.0:$GUNICORN_PORT --forwarded-allow-ips=$hostip -k uvicorn.workers.UvicornWorker -c /app/gunicorn_conf.py --preload
exec gunicorn mealie.app:app -b 0.0.0.0:$GUNICORN_PORT --forwarded-allow-ips=$HOST_IP -k uvicorn.workers.UvicornWorker -c /app/gunicorn_conf.py --preload
else
exec uvicorn mealie.app:app --host 0.0.0.0 --forwarded-allow-ips=$hostip --port $GUNICORN_PORT
exec python /app/mealie/main.py
fi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
| API_DOCS | True | Turns on/off access to the API documentation locally. |
| TZ | UTC | Must be set to get correct date/time on the server |
| ALLOW_SIGNUP<super>\*</super> | false | Allow user sign-up without token |
| LOG_CONFIG_OVERRIDE | | Override the config for logging with a custom path |
| LOG_LEVEL | info | logging level configured |

<super>\*</super> Starting in v1.4.0 this was changed to default to `false` as apart of a security review of the application.

Expand All @@ -27,15 +29,15 @@

### Database

| Variables | Default | Description |
| ----------------- | :------: | -------------------------------- |
| DB_ENGINE | sqlite | Optional: 'sqlite', 'postgres' |
| POSTGRES_USER | mealie | Postgres database user |
| POSTGRES_PASSWORD | mealie | Postgres database password |
| POSTGRES_SERVER | postgres | Postgres database server address |
| POSTGRES_PORT | 5432 | Postgres database port |
| POSTGRES_DB | mealie | Postgres database name |
| POSTGRES_URL_OVERRIDE | None | Optional Postgres URL override to use instead of POSTGRES_* variables |
| Variables | Default | Description |
| --------------------- | :------: | ----------------------------------------------------------------------- |
| DB_ENGINE | sqlite | Optional: 'sqlite', 'postgres' |
| POSTGRES_USER | mealie | Postgres database user |
| POSTGRES_PASSWORD | mealie | Postgres database password |
| POSTGRES_SERVER | postgres | Postgres database server address |
| POSTGRES_PORT | 5432 | Postgres database port |
| POSTGRES_DB | mealie | Postgres database name |
| POSTGRES_URL_OVERRIDE | None | Optional Postgres URL override to use instead of POSTGRES\_\* variables |

### Email

Expand Down Expand Up @@ -96,7 +98,7 @@ For usage, see [Usage - OpenID Connect](../authentication/oidc.md)
| OIDC_PROVIDER_NAME | OAuth | The provider name is shown in SSO login button. "Login with <OIDC_PROVIDER_NAME\>" |
| OIDC_REMEMBER_ME | False | Because redirects bypass the login screen, you cant extend your session by clicking the "Remember Me" checkbox. By setting this value to true, a session will be extended as if "Remember Me" was checked |
| OIDC_SIGNING_ALGORITHM | RS256 | The algorithm used to sign the id token (examples: RS256, HS256) |
| OIDC_USER_CLAIM | email | Optional: 'email', 'preferred_username'
| OIDC_USER_CLAIM | email | Optional: 'email', 'preferred_username' |

### Themeing

Expand Down
16 changes: 16 additions & 0 deletions docs/docs/documentation/getting-started/installation/logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Logs

:octicons-tag-24: v1.5.0

## Highlighs

- Logs are written to `/app/data/mealie.log` by default in the container.
- Logs are also written to stdout and stderr.
- You can adjust the log level using the `LOG_LEVEL` environment variable.

## Configuration

Starting in v1.5.0 logging is now highly configurable. Using the `LOG_CONFIG_OVERRIDE` you can provide the application with a custom configuration to log however you'd like. This configuration file is based off the [Python Logging Config](https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig). It can be difficult to understand the configuration at first, so here are some resources to help get started.

- This [YouTube Video](https://www.youtube.com/watch?v=9L77QExPmI0) for a great walkthrough on the logging file format.
- Our [Logging Config](https://github.com/mealie-recipes/mealie/blob/mealie-next/mealie/core/logger/logconf.prod.json)
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ nav:
- PostgreSQL: "documentation/getting-started/installation/postgres.md"
- Backend Configuration: "documentation/getting-started/installation/backend-config.md"
- Security: "documentation/getting-started/installation/security.md"
- Logs: "documentation/getting-started/installation/logs.md"
- Usage:
- Backup and Restoring: "documentation/getting-started/usage/backups-and-restoring.md"
- Permissions and Public Access: "documentation/getting-started/usage/permissions-and-public-access.md"
Expand Down
1 change: 0 additions & 1 deletion frontend/lib/api/types/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ export interface MaintenanceStorageDetails {
}
export interface MaintenanceSummary {
dataDirSize: string;
logFileSize: string;
cleanableImages: number;
cleanableDirs: number;
}
Expand Down
22 changes: 0 additions & 22 deletions frontend/pages/admin/maintenance/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<template #title> {{ $t("admin.maintenance.page-title") }} </template>
</BasePageTitle>

<div class="d-flex justify-end">
<ButtonLink to="/admin/maintenance/logs" text="Logs" :icon="$globals.icons.file" />
</div>

<section>
<BaseCardSectionTitle class="pb-0" :icon="$globals.icons.wrench" :title="$tc('admin.maintenance.summary-title')">
</BaseCardSectionTitle>
Expand Down Expand Up @@ -110,7 +106,6 @@ export default defineComponent({
const infoResults = ref<MaintenanceSummary>({
dataDirSize: i18n.tc("about.unknown-version"),
logFileSize: i18n.tc("about.unknown-version"),
cleanableDirs: 0,
cleanableImages: 0,
});
Expand All @@ -121,25 +116,19 @@ export default defineComponent({
infoResults.value = data ?? {
dataDirSize: i18n.tc("about.unknown-version"),
logFileSize: i18n.tc("about.unknown-version"),
cleanableDirs: 0,
cleanableImages: 0,
};
state.fetchingInfo = false;
}
const info = computed(() => {
return [
{
name: i18n.t("admin.maintenance.info-description-data-dir-size"),
value: infoResults.value.dataDirSize,
},
{
name: i18n.t("admin.maintenance.info-description-log-file-size"),
value: infoResults.value.logFileSize,
},
{
name: i18n.t("admin.maintenance.info-description-cleanable-directories"),
value: infoResults.value.cleanableDirs,
Expand Down Expand Up @@ -184,12 +173,6 @@ export default defineComponent({
// ==========================================================================
// Actions
async function handleDeleteLogFile() {
state.actionLoading = true;
await adminApi.maintenance.cleanLogFile();
state.actionLoading = false;
}
async function handleCleanDirectories() {
state.actionLoading = true;
await adminApi.maintenance.cleanRecipeFolders();
Expand All @@ -209,11 +192,6 @@ export default defineComponent({
}
const actions = [
{
name: i18n.t("admin.maintenance.action-delete-log-files-name"),
handler: handleDeleteLogFile,
subtitle: i18n.t("admin.maintenance.action-delete-log-files-description"),
},
{
name: i18n.t("admin.maintenance.action-clean-directories-name"),
handler: handleCleanDirectories,
Expand Down
109 changes: 0 additions & 109 deletions frontend/pages/admin/maintenance/logs.vue

This file was deleted.

67 changes: 67 additions & 0 deletions mealie/core/logger/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import json
import logging
import pathlib
import typing
from logging import config as logging_config

__dir = pathlib.Path(__file__).parent
__conf: dict[str, str] | None = None


def _load_config(path: pathlib.Path, substitutions: dict[str, str] | None = None) -> dict[str, typing.Any]:
with open(path) as file:
if substitutions:
contents = file.read()
for key, value in substitutions.items():
# Replaces the key matches
#
# Example:
# {"key": "value"}
# "/path/to/${key}/file" -> "/path/to/value/file"
contents = contents.replace(f"${{{key}}}", value)

json_data = json.loads(contents)

else:
json_data = json.load(file)

return json_data


def log_config() -> dict[str, str]:
if __conf is None:
raise ValueError("logger not configured, must call configured_logger first")

return __conf


def configured_logger(
*,
mode: str,
config_override: pathlib.Path | None = None,
substitutions: dict[str, str] | None = None,
) -> logging.Logger:
"""
Configure the logger based on the mode and return the root logger
Args:
mode (str): The mode to configure the logger for (production, development, testing)
config_override (pathlib.Path, optional): A path to a custom logging config. Defaults to None.
substitutions (dict[str, str], optional): A dictionary of substitutions to apply to the logging config.
"""
global __conf

if config_override:
__conf = _load_config(config_override, substitutions)
else:
if mode == "production":
__conf = _load_config(__dir / "logconf.prod.json", substitutions)
elif mode == "development":
__conf = _load_config(__dir / "logconf.dev.json", substitutions)
elif mode == "testing":
__conf = _load_config(__dir / "logconf.test.json", substitutions)
else:
raise ValueError(f"Invalid mode: {mode}")

logging_config.dictConfig(config=__conf)
return logging.getLogger()
17 changes: 17 additions & 0 deletions mealie/core/logger/logconf.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": 1,
"disable_existing_loggers": false,
"handlers": {
"rich": {
"class": "rich.logging.RichHandler"
}
},
"loggers": {
"root": {
"level": "DEBUG",
"handlers": [
"rich"
]
}
}
}

0 comments on commit 6bd5a82

Please sign in to comment.