-
-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite logger to support custom config files (#3104)
- Loading branch information
Showing
19 changed files
with
278 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
docs/docs/documentation/getting-started/installation/logs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.