Skip to content

Commit

Permalink
Use recommended dictConfig for logger configuration (#83)
Browse files Browse the repository at this point in the history
* Bump pre-commit ruff version

* Apply ruff formatting

* Include review comments from @danielhuppmann
  • Loading branch information
glatterf42 authored Apr 19, 2024
1 parent 9e7ad3e commit 03be111
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 193 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
entry: bash -c "poetry run mypy ."
language: system
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
rev: v0.3.3
hooks:
- id: ruff
- id: ruff-format
Expand Down
6 changes: 2 additions & 4 deletions ixmp4/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class PlatformInfo(pydantic.BaseModel):


class Config(Protocol):
def list_platforms(self) -> list[PlatformInfo]:
...
def list_platforms(self) -> list[PlatformInfo]: ...

def get_platform(self, key: str) -> PlatformInfo:
...
def get_platform(self, key: str) -> PlatformInfo: ...
24 changes: 0 additions & 24 deletions ixmp4/conf/logging/debug.conf

This file was deleted.

21 changes: 21 additions & 0 deletions ixmp4/conf/logging/debug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters":{
"generic": {
"format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s",
"datefmt": "%H:%M:%S"
}
},
"root": {
"level": "DEBUG",
"handlers": ["console"]
},
"handlers":{
"console":{
"class": "logging.StreamHandler",
"level": "NOTSET",
"formatter": "generic"
}
}
}
24 changes: 0 additions & 24 deletions ixmp4/conf/logging/development.conf

This file was deleted.

21 changes: 21 additions & 0 deletions ixmp4/conf/logging/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters":{
"generic":{
"format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s",
"datefmt": "%H:%M:%S"
}
},
"root":{
"level": "INFO",
"handlers": ["console"]
},
"handlers":{
"console":{
"class": "logging.StreamHandler",
"level": "NOTSET",
"formatter": "generic"
}
}
}
30 changes: 0 additions & 30 deletions ixmp4/conf/logging/production.conf

This file was deleted.

23 changes: 23 additions & 0 deletions ixmp4/conf/logging/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"generic": {
"format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s",
"datefmt": "%H:%M:%S"
}
},
"loggers": {
"ixmp4": {
"level": "INFO",
"handlers": ["console"]
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "NOTSET",
"formatter": "generic"
}
}
}
73 changes: 0 additions & 73 deletions ixmp4/conf/logging/server.conf

This file was deleted.

71 changes: 71 additions & 0 deletions ixmp4/conf/logging/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters":{
"generic": {
"format": "[%(levelname)s] %(asctime)s - %(name)s: %(message)s",
"datefmt": "%H:%M:%S"
}
},
"loggers": {
"fastapi": {
"level": "NOTSET",
"handlers": ["debug","error","console"]
},
"httpx": {
"level": "NOTSET",
"handlers": ["debug","error"]
},
"sqlalchemy": {
"level": "NOTSET",
"handlers": ["debug","error"]
},
"uvicorn": {
"level": "NOTSET",
"handlers": ["debug","error","console"]
},
"uvicorn.access": {
"level": "NOTSET",
"handlers": ["access"]
},
"watchfiles.main": {
"level": "ERROR",
"handlers": ["error","debug"]
}
},
"handlers": {
"access": {
"class": "logging.handlers.RotatingFileHandler",
"level": "INFO",
"formatter": "generic",
"filename": "os.getenv('IXMP4_ACCESS_LOG')",
"mode": "a",
"maxBytes": 250000
},
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "generic"
},
"debug": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "generic",
"filename": "os.getenv('IXMP4_DEBUG_LOG')",
"mode": "a",
"maxBytes": 250000
},
"error": {
"class": "logging.handlers.RotatingFileHandler",
"level": "WARN",
"formatter": "generic",
"filename": "os.getenv('IXMP4_ERROR_LOG')",
"mode": "a",
"maxBytes": 250000
}
},
"root": {
"level": "NOTSET",
"handlers": ["debug","error"]
}
}
13 changes: 8 additions & 5 deletions ixmp4/conf/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import logging.config
import os
Expand All @@ -22,9 +23,9 @@


class Settings(BaseSettings):
mode: Literal["production"] | Literal["development"] | Literal[
"debug"
] = "production"
mode: Literal["production"] | Literal["development"] | Literal["debug"] = (
"production"
)
storage_directory: Path = Field(
"~/.local/share/ixmp4/", json_schema_extra={"env": "ixmp4_dir"}
)
Expand Down Expand Up @@ -144,8 +145,10 @@ def configure_logging(self, config: str):
os.environ.setdefault("IXMP4_DEBUG_LOG", str(debug_file.absolute()))
os.environ.setdefault("IXMP4_ERROR_LOG", str(error_file.absolute()))

logging_config = here / f"logging/{config}.conf"
logging.config.fileConfig(logging_config, disable_existing_loggers=False)
logging_config = here / f"logging/{config}.json"
with open(logging_config) as file:
config_dict = json.load(file)
logging.config.dictConfig(config_dict)

def check_credentials(self):
if self.default_credentials is not None:
Expand Down
Loading

0 comments on commit 03be111

Please sign in to comment.