Skip to content

Commit

Permalink
feat: add log file
Browse files Browse the repository at this point in the history
  • Loading branch information
EuleMitKeule committed Mar 10, 2023
1 parent 42d234d commit e53ebc7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ RUN poetry install

ENV CONFIG_PATH /config/config.yml
ENV DB_PATH /config/estimenergy.db
ENV LOG_PATH /config/estimenergy.log
ENV LOG_LEVEL INFO
ENV ENABLE_METRICS true

VOLUME /config
Expand Down
2 changes: 1 addition & 1 deletion estimenergy/collectors/glow_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
collector_data: CollectorData
):
self.collector_data = collector_data
self.logger = logging.getLogger("energy_collector").getChild(self.collector_data.name)
self.logger = logging.getLogger("estimenergy").getChild(self.collector_data.name)

self.logger.info(f"Creating API Client for {self.collector_data.name} ({self.collector_data.host}:{self.collector_data.port})")

Expand Down
20 changes: 13 additions & 7 deletions estimenergy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ def create_gauge(self, registry) -> Gauge:
"()": "uvicorn.logging.ColourizedFormatter",
"format": "%(asctime)-25s %(name)-30s %(levelprefix)-8s %(message)s",
},
"access": {
"file": {
"()": "uvicorn.logging.ColourizedFormatter",
"format": "%(asctime)-25s %(name)-30s %(levelprefix)-8s %(message)s",
"use_colors": False
},
},
"handlers": {
Expand All @@ -104,25 +105,30 @@ def create_gauge(self, registry) -> Gauge:
"stream": "ext://sys.stderr",
},
"access": {
"formatter": "access",
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
"file": {
"formatter": "file",
"class": "logging.FileHandler",
"filename": "energy_collector.log",
}
},
"loggers": {
"uvicorn.error": {
"level": "INFO",
"handlers": ["default"],
"handlers": ["default", "file"],
"propagate": False,
},
"uvicorn.access": {
"level": "INFO",
"handlers": ["access"],
"handlers": ["access", "file"],
"propagate": False,
},
"energy_collector": {
"level": "DEBUG",
"handlers": ["default"],
"estimenergy": {
"level": "INFO",
"handlers": ["default", "file"],
"propagate": False,
},
},
Expand Down
9 changes: 9 additions & 0 deletions estimenergy/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import logging
import uvicorn
import asyncio
from fastapi import FastAPI
Expand Down Expand Up @@ -31,6 +32,14 @@
app.include_router(collector_router.router)

def start():
if settings.log_path is not None and settings.log_path != "":
LOGGING_CONFIG["handlers"]["file"]["filename"] = settings.log_path

if settings.log_level is not None and settings.log_level != "":
LOGGING_CONFIG["loggers"]["uvicorn.error"]["level"] = settings.log_level
LOGGING_CONFIG["loggers"]["uvicorn.access"]["level"] = settings.log_level
LOGGING_CONFIG["loggers"]["estimenergy"]["level"] = settings.log_level

uvicorn.run(
"estimenergy.main:app",
host="0.0.0.0",
Expand Down
2 changes: 2 additions & 0 deletions estimenergy/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Settings(BaseSettings):
config_path: str = "config.yml"
log_config_path: str = "logging.yml"
db_path: str = "estimenergy.db"
log_path: str = "estimenergy.log"
log_level: str = "INFO"
enable_metrics: bool = True
reload: bool = False

Expand Down

0 comments on commit e53ebc7

Please sign in to comment.