Skip to content

Commit

Permalink
Use uvicorn logger and handle output dir location given in config.yam…
Browse files Browse the repository at this point in the history
…l file (#150)

* - Used uvicorn logger.
- Added print statement of config loaded.
- Removed some unwanted logging statements.

* Full File Path logic enhanced.

---------

Co-authored-by: shreyas.cd <[email protected]>
  • Loading branch information
shreyas-damle and shreyas.cd authored Feb 14, 2024
1 parent 2ca8b60 commit 47f171f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
16 changes: 14 additions & 2 deletions pebblo/app/config/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import yaml
from tqdm import tqdm

from pydantic import BaseSettings, Field
import pathlib

# Default config value
dir_path = pathlib.Path().absolute()


# Port BaseModel
class PortConfig(BaseSettings):
host: str = Field(default='localhost')
Expand All @@ -30,8 +30,14 @@ class Config(BaseSettings):
reports: ReportConfig
logging: LoggingConfig

def print_config_output(config_output, p_bar=None):
if isinstance(p_bar, tqdm):
p_bar.write(config_output)
else:
print(config_output)


def load_config(path) -> Config:
def load_config(path, p_bar=None) -> Config:
try:
# If Path does not exist in command, set default config value
conf_obj = Config(
Expand All @@ -49,6 +55,9 @@ def load_config(path) -> Config:
)
if not path:
# Setting Default config details
config_details = f"Config values : {conf_obj.dict()}"
print_config_output(config_details, p_bar)

return conf_obj.dict()

# If Path exist, set config value
Expand All @@ -59,6 +68,9 @@ def load_config(path) -> Config:
cred_json = yaml.safe_load(output)
parsed_config = Config.parse_obj(cred_json)
config_dict = parsed_config.dict()
config_details = f"Config values : {config_dict}"
print_config_output(config_details, p_bar)

return config_dict
except IOError as err:
print(f"no credentials file found at {con_file}")
Expand Down
2 changes: 1 addition & 1 deletion pebblo/app/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ logging:
level: info
reports:
format: pdf
outputDir: ~/.pebblo
outputDir: ~/.pebblo
2 changes: 1 addition & 1 deletion pebblo/app/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def start():
parser.add_argument('--config', type=str, help="Config file path")
args = parser.parse_args()
path = args.config
config_details = load_config(path)
config_details = load_config(path, p_bar)
classifier_init()
server_start(config_details)
print("Pebblo server Stopped. BYE!")
Expand Down
13 changes: 2 additions & 11 deletions pebblo/app/libs/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@
Module to handle logging functionality
"""
import logging
from pebblo.app.daemon import config_details


def get_logger():
"""Get object of logger"""
logger_level = config_details.get('logging', {}).get('level', 'info').upper()
logger_obj = logging.getLogger("Pebblo Logger")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_handle = logging.StreamHandler()
console_handle.setFormatter(formatter)
logger_obj.setLevel(logger_level)
logger_obj.propagate = False
if not logger_obj.handlers:
logger_obj.addHandler(console_handle)
"""Get uvicorn logger"""
logger_obj = logging.getLogger("uvicorn.error")
return logger_obj


Expand Down
3 changes: 0 additions & 3 deletions pebblo/app/service/doc_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def _get_finding_details(self, doc, data_source_findings, entity_type, file_coun
data_source_findings[label_name]["unique_snippets"].add(source_path)

def _get_doc_report_metadata(self, doc, raw_data):
logger.debug("In Function: _get_doc_report_metadata")
# Initialize variables
loader_source_snippets = raw_data["loader_source_snippets"]
total_findings = raw_data["total_findings"]
Expand Down Expand Up @@ -163,7 +162,6 @@ def _get_data_source_details(self, raw_data):
return data_source_obj_list

def _generate_final_report(self, raw_data):
logger.debug("In Function: _generate_final_report")
loader_source_snippets = raw_data["loader_source_snippets"]
file_count_restricted_data = 0
for file_dict in self.app_details["loader_source_files"]:
Expand Down Expand Up @@ -216,7 +214,6 @@ def process_docs_and_generate_report(self):
"sourceSize": loader_details.get("source_size"),
"type": loader_details.get("source_type")}
input_doc_list = self.data.get('docs', [])
logger.debug("In Function: _get_doc_details_and_generate_report")
last_used = datetime.now()
docs = self.app_details.get("docs", [])
raw_data = {"total_findings": 0, "findings_entities": 0, "findings_topics": 0,
Expand Down
2 changes: 1 addition & 1 deletion pebblo/app/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from pebblo.app.utils.utils import write_json_to_file, read_json_file, get_full_path
from pebblo.app.models.models import LoaderMetadata, Metadata, AiApp, InstanceDetails
from pebblo.app.service.doc_helper import DocHelper
from pebblo.app.libs.logger import logger
from pydantic import ValidationError
from fastapi import HTTPException

logger = logging.getLogger("uvicorn.error")

class AppDiscover:
def __init__(self, data: dict):
Expand Down
23 changes: 19 additions & 4 deletions pebblo/app/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from os import makedirs, path
from os import makedirs, path, getcwd
from json import JSONEncoder, dump
from pebblo.app.libs.logger import logger

Expand Down Expand Up @@ -41,6 +41,21 @@ def read_json_file(file_path):


def get_full_path(file_path):
home_dir = path.expanduser("~")
full_file_path = path.join(home_dir, file_path)
return full_file_path
try:
# path starting with '~'
if file_path.startswith("~"):
full_file_path = path.expanduser(file_path)
return full_file_path
# handle path starting with '.'
elif file_path.startswith("."):
base_dir = getcwd()
full_file_path = path.join(base_dir, file_path)
return full_file_path
# handle absolute path
elif file_path.startswith("/"):
return file_path
# error case
else:
logger.error(f"Could not find {file_path} location.")
except Exception as e:
logger.error(f"Failed to figure out path for input : {file_path}. Exception: {e}")

0 comments on commit 47f171f

Please sign in to comment.