mirror of
https://github.com/dtomlinson91/panaetius.git
synced 2025-12-22 04:55:44 +00:00
adding latest working
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
from panaetius.config import Config
|
from panaetius.config import Config
|
||||||
from panaetius.library import set_config
|
from panaetius.library import set_config
|
||||||
|
from panaetius.logging import set_logger, SimpleLogger, AdvancedLogger, CustomLogger
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ class Config:
|
|||||||
else pathlib.Path.home() / ".config"
|
else pathlib.Path.home() / ".config"
|
||||||
)
|
)
|
||||||
self._missing_config = False
|
self._missing_config = False
|
||||||
|
|
||||||
|
# default logging options
|
||||||
self.logging_path: str | None = None
|
self.logging_path: str | None = None
|
||||||
|
self.logging_rotate_bytes: int = 0
|
||||||
|
self.logging_backup_count: int = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def config(self) -> dict[str, Any]:
|
def config(self) -> dict[str, Any]:
|
||||||
@@ -82,7 +86,7 @@ class Config:
|
|||||||
return self.__load_default_value(default)
|
return self.__load_default_value(default)
|
||||||
return self.__load_value(value, coerce)
|
return self.__load_value(value, coerce)
|
||||||
|
|
||||||
def __load_value(self, value: str, coerce: bool) -> Any:
|
def __load_value(self, value: str, coerce: bool) -> Any: # noqa
|
||||||
value = str(value).lower() if isinstance(value, bool) else value
|
value = str(value).lower() if isinstance(value, bool) else value
|
||||||
return (
|
return (
|
||||||
toml.loads(f"value = {value}")["value"]
|
toml.loads(f"value = {value}")["value"]
|
||||||
@@ -90,7 +94,7 @@ class Config:
|
|||||||
else toml.loads(f'value = "{value}"')["value"]
|
else toml.loads(f'value = "{value}"')["value"]
|
||||||
)
|
)
|
||||||
|
|
||||||
def __load_default_value(self, default: Any) -> Any:
|
def __load_default_value(self, default: Any) -> Any: # noqa
|
||||||
if isinstance(default, str):
|
if isinstance(default, str):
|
||||||
return toml.loads(f'value = "{default}"')["value"]
|
return toml.loads(f'value = "{default}"')["value"]
|
||||||
# if default is bool convert to lower case toml syntax
|
# if default is bool convert to lower case toml syntax
|
||||||
|
|||||||
@@ -1,53 +1,91 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
import logging
|
import logging
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
import pathlib
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from panaetius import Config
|
from panaetius import Config
|
||||||
|
from panaetius.library import set_config
|
||||||
|
|
||||||
|
|
||||||
def set_logger(config_inst: Config, logging_format: Logger):
|
def set_logger(config_inst: Config, logging_format_inst: LoggingData) -> logging.Logger:
|
||||||
logger = logging.getLogger(config_inst.header_variable)
|
logger = logging.getLogger(config_inst.header_variable)
|
||||||
loghandler_sys = logging.StreamHandler(sys.stdout)
|
log_handler_sys = logging.StreamHandler(sys.stdout)
|
||||||
|
|
||||||
# check if log path is set
|
# configure file handler
|
||||||
if config_inst.logging_path is not None:
|
if config_inst.logging_path is not None:
|
||||||
logging_file = pathlib.Path(config_inst.logging_path)
|
logging_file = (
|
||||||
|
pathlib.Path(config_inst.logging_path)
|
||||||
|
/ config_inst.header_variable
|
||||||
|
/ f"{config_inst.header_variable}.log"
|
||||||
|
).expanduser()
|
||||||
|
|
||||||
|
if config_inst.logging_rotate_bytes == 0:
|
||||||
|
set_config(config_inst, "logging.rotate_bytes", 512000)
|
||||||
|
if config_inst.logging_backup_count == 0:
|
||||||
|
set_config(config_inst, "logging.backup_count", 3)
|
||||||
|
|
||||||
|
log_handler_file = RotatingFileHandler(
|
||||||
|
str(logging_file),
|
||||||
|
"a",
|
||||||
|
config_inst.logging_rotate_bytes,
|
||||||
|
config_inst.logging_backup_count,
|
||||||
|
)
|
||||||
|
|
||||||
|
log_handler_file.setFormatter(logging.Formatter(logging_format_inst.format))
|
||||||
|
logger.addHandler(log_handler_file)
|
||||||
|
|
||||||
|
# configure stdout handler
|
||||||
|
log_handler_sys.setFormatter(logging.Formatter(logging_format_inst.format))
|
||||||
|
logger.addHandler(log_handler_sys)
|
||||||
|
logger.setLevel(logging_format_inst.logging_level)
|
||||||
|
return logger
|
||||||
|
|
||||||
|
|
||||||
class Logger(ABC):
|
class LoggingData(metaclass=ABCMeta):
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def format(self):
|
def format(self) -> str:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self, logging_level: str):
|
||||||
|
self.logging_level = logging_level
|
||||||
|
|
||||||
class SimpleLogger(Logger):
|
|
||||||
|
class SimpleLogger(LoggingData):
|
||||||
@property
|
@property
|
||||||
def format(self):
|
def format(self) -> str:
|
||||||
return (
|
return str(
|
||||||
'{\n\t"time": "%(asctime)s",\n\t"logging_level":'
|
'{\n\t"time": "%(asctime)s",\n\t"logging_level":'
|
||||||
'"%(levelname)s",\n\t"message": "%(message)s"\n}',
|
'"%(levelname)s",\n\t"message": "%(message)s"\n}',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, logging_level: str = "INFO"):
|
||||||
|
self.logging_level = logging_level
|
||||||
|
|
||||||
class AdvancedLogger(Logger):
|
|
||||||
|
class AdvancedLogger(LoggingData):
|
||||||
@property
|
@property
|
||||||
def format(self):
|
def format(self) -> str:
|
||||||
return (
|
return str(
|
||||||
'{\n\t"time": "%(asctime)s",\n\t"file_name": "%(filename)s",'
|
'{\n\t"time": "%(asctime)s",\n\t"file_name": "%(filename)s",'
|
||||||
'\n\t"module": "%(module)s",\n\t"function":"%(funcName)s",\n\t'
|
'\n\t"module": "%(module)s",\n\t"function":"%(funcName)s",\n\t'
|
||||||
'"line_number": "%(lineno)s",\n\t"logging_level":'
|
'"line_number": "%(lineno)s",\n\t"logging_level":'
|
||||||
'"%(levelname)s",\n\t"message": "%(message)s"\n}',
|
'"%(levelname)s",\n\t"message": "%(message)s"\n}',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, logging_level: str = "INFO"):
|
||||||
|
self.logging_level = logging_level
|
||||||
|
|
||||||
class CustomLogger(Logger):
|
|
||||||
|
class CustomLogger(LoggingData):
|
||||||
@property
|
@property
|
||||||
def format(self):
|
def format(self) -> str:
|
||||||
return self._format
|
return str(self._format)
|
||||||
|
|
||||||
def __init__(self, logging_format: str):
|
def __init__(self, logging_format: str, logging_level: str = "INFO"):
|
||||||
|
self.logging_level = logging_level
|
||||||
self._format = logging_format
|
self._format = logging_format
|
||||||
|
|||||||
25
rewrite.todo
25
rewrite.todo
@@ -11,10 +11,33 @@ Coding:
|
|||||||
✔ Create SimpleLogger, AdvancedLogger, CustomLogger classes @done(21-10-16 16:22)
|
✔ Create SimpleLogger, AdvancedLogger, CustomLogger classes @done(21-10-16 16:22)
|
||||||
should simply have the different logging strings to output
|
should simply have the different logging strings to output
|
||||||
should both specify whether to save to file or not
|
should both specify whether to save to file or not
|
||||||
☐ Logging path should take by default the config path unless overwritten?
|
✔ Logging path should take by default the config path unless overwritten? @done(21-10-16 23:49)
|
||||||
|
|
||||||
|
Errors:
|
||||||
|
☐ Check logging path + config path are valid, if not raise error.
|
||||||
|
☐ Add tests for these.
|
||||||
|
|
||||||
|
Linting:
|
||||||
|
☐ Check all functions and annotations.
|
||||||
|
|
||||||
|
Docstrings:
|
||||||
|
☐ Write the docstrings for public functions/methods.
|
||||||
|
|
||||||
Documentation:
|
Documentation:
|
||||||
☐ Rewrite documentation using `mkdocs` and using `.md`. @2h
|
☐ Rewrite documentation using `mkdocs` and using `.md`. @2h
|
||||||
|
|
||||||
|
|
||||||
Tests:
|
Tests:
|
||||||
|
Config File:
|
||||||
|
☐
|
||||||
|
|
||||||
|
Environment Variable:
|
||||||
|
☐
|
||||||
|
|
||||||
|
__init__:
|
||||||
|
☐ Test default config path set to "~/.config"
|
||||||
|
☐ Test config path is set when passed in
|
||||||
|
|
||||||
|
config property:
|
||||||
|
☐ Check testing config file is returned as dict
|
||||||
|
☐ Check _self.missing_config and empty dict is returned
|
||||||
|
|||||||
Reference in New Issue
Block a user