logs folder is next to the active config folder.

It was wherever 'cbpi setup' was run from.
and if no config folder path is provided its still the same.
This also adds a file based global logger placed in the logs folder.
The sensor loggers respect the new logs folder location.
This commit is contained in:
prash3r 2022-09-09 18:38:08 +02:00
parent b5ca644e80
commit 4952861a58
6 changed files with 28 additions and 20 deletions

View file

@ -1,4 +1,5 @@
import logging
from pathlib import Path
import requests
from cbpi.configFolder import ConfigFolder
from cbpi.utils.utils import load_config
@ -230,7 +231,16 @@ def main(context, config_folder_path):
print("Welcome to CBPi")
print("---------------------")
level = logging.INFO
logging.basicConfig(level=level, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
logger = logging.getLogger()
logger.setLevel(level)
try:
logger.addHandler(logging.handlers.RotatingFileHandler(os.path.join(Path(config_folder_path).parent, 'logs', f"cbpi.log"), maxBytes=1000000, backupCount=3))
except:
print("there seems to be no log folder - continueing without (maybe you should run 'cbpi setup')")
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
for handler in logger.handlers:
handler.setLevel(level)
handler.setFormatter(formatter)
cbpi_cli = CraftBeerPiCli(ConfigFolder(config_folder_path))
context.obj = cbpi_cli
pass

View file

@ -90,11 +90,10 @@ class ConfigFolder:
['craftbeerpi.service', 'file'],
['chromium.desktop', 'file'],
['dashboard/cbpi_dashboard_1.json', 'file'],
['dashboard', 'folder'],
['dashboard/widgets', 'folder'],
['dashboard', 'folder'],
['fermenterrecipes', 'folder'],
['logs', 'folder'],
['logs/sensors', 'folder'],
['../logs', 'folder'],
['recipes', 'folder'],
['upload', 'folder']
]
@ -171,7 +170,7 @@ class ConfigFolder:
def create_folders(self):
pathlib.Path(self._rawPath).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, 'dashboard', 'widgets')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, 'logs', 'sensors')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, '..','logs')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, 'recipes')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, 'fermenterrecipes')).mkdir(parents=True, exist_ok=True)
pathlib.Path(os.path.join(self._rawPath, 'upload')).mkdir(parents=True, exist_ok=True)

View file

@ -1,6 +1,7 @@
from cbpi.api.dataclasses import Config
import logging
import os
from pathlib import Path
from cbpi.api.config import ConfigType
from cbpi.utils import load_config
@ -16,6 +17,7 @@ class ConfigController:
self.cbpi.register(self)
self.path = cbpi.config_folder.get_file_path("config.json")
self.path_static = cbpi.config_folder.get_file_path("config.yaml")
self.logger.info("Config folder path : " + os.path.join(Path(self.cbpi.config_folder._rawPath).absolute()))
def get_state(self):

View file

@ -8,6 +8,7 @@ import pandas as pd
import zipfile
import base64
import urllib3
from pathlib import Path
from cbpi.api import *
from cbpi.api.config import ConfigType
from cbpi.api.base import CBPiBase
@ -25,6 +26,8 @@ class LogController:
self.logger = logging.getLogger(__name__)
self.configuration = False
self.datalogger = {}
self.logsFolderPath = os.path.join(Path(self.cbpi.config_folder._rawPath).parent.absolute(), 'logs')
self.logger.info("Log folder path : " + self.logsFolderPath)
def log_data(self, name: str, value: str) -> None:
self.logfiles = self.cbpi.config.get("CSVLOGFILES", "Yes")
@ -37,7 +40,7 @@ class LogController:
data_logger = logging.getLogger('cbpi.sensor.%s' % name)
data_logger.propagate = False
data_logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('./logs/sensor_%s.log' % name, maxBytes=max_bytes, backupCount=backup_count)
handler = RotatingFileHandler(os.path.join(self.logsFolderPath, f"sensor_{name}.log"), maxBytes=max_bytes, backupCount=backup_count)
data_logger.addHandler(handler)
self.datalogger[name] = data_logger
@ -115,7 +118,7 @@ class LogController:
for name in names:
# get all log names
all_filenames = glob.glob('./logs/sensor_%s.log*' % name)
all_filenames = os.path.join(self.logsFolderPath, f"sensor_{name}.log*")
# concat all logs
df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', name], header=None) for f in all_filenames])
logging.info("Read all files for {}".format(names))
@ -157,7 +160,7 @@ class LogController:
for id in ids:
# df = pd.read_csv("./logs/sensor_%s.log" % id, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime',"Values"], header=None)
# concat all logs
all_filenames = glob.glob('./logs/sensor_%s.log*' % id)
all_filenames = glob.glob(os.path.join(self.logsFolderPath,f"./logs/sensor_{id}.log*"))
df = pd.concat([pd.read_csv(f, parse_dates=True, date_parser=dateparse, index_col='DateTime', names=['DateTime', 'Values'], header=None) for f in all_filenames])
df = df.resample('60s').max()
df = df.dropna()
@ -173,11 +176,11 @@ class LogController:
:return: list of log file names
'''
return [os.path.basename(x) for x in glob.glob('./logs/sensor_%s.log*' % name)]
return [os.path.basename(x) for x in os.path.join(self.logsFolderPath, f"sensor_{name}.log*")]
def clear_log(self, name:str ) -> str:
all_filenames = glob.glob('./logs/sensor_%s.log*' % name)
all_filenames = os.path.join(self.logsFolderPath, f"sensor_{name}.log*")
for f in all_filenames:
os.remove(f)
@ -193,7 +196,7 @@ class LogController:
:return:
'''
return [os.path.basename(x) for x in glob.glob('./logs/*-sensor-%s.zip' % name)]
return [os.path.basename(x) for x in glob.glob(os.path.join(self.logsFolderPath, f"*-sensor-{name}.zip"))]
def clear_zip(self, name:str ) -> None:
"""
@ -202,7 +205,7 @@ class LogController:
:return: None
"""
all_filenames = glob.glob('./logs/*-sensor-%s.zip' % name)
all_filenames = glob.glob(os.path.join(self.logsFolderPath, f"*-sensor-{name}.zip"))
for f in all_filenames:
os.remove(f)
@ -213,9 +216,9 @@ class LogController:
"""
formatted_time = strftime("%Y-%m-%d-%H_%M_%S", localtime())
file_name = './logs/%s-sensor-%s.zip' % (formatted_time, name)
file_name = os.path.join(self.logsFolderPath, f"./logs/{formatted_time}-sensor-{name}.zip" % (formatted_time, name))
zip = zipfile.ZipFile(file_name, 'w', zipfile.ZIP_DEFLATED)
all_filenames = glob.glob('./logs/sensor_%s.log*' % name)
all_filenames = os.path.join(self.logsFolderPath, f"sensor_{name}.log*")
for f in all_filenames:
zip.write(os.path.join(f))
zip.close()

View file

@ -301,12 +301,6 @@ class CraftBeerPi:
self._swagger_setup()
level = logging.INFO
logger = logging.getLogger()
logger.setLevel(level)
for handler in logger.handlers:
handler.setLevel(level)
return self.app
def start(self):