Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Commit

Permalink
added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherd-06 committed Feb 10, 2019
1 parent 8bbf90a commit ddaab14
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 53 deletions.
14 changes: 10 additions & 4 deletions ZathuraProject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ZathuraProject.utility import Utility
from ZathuraProject.zathura import Zathura

CURRENT_VERSION = 'v0.0.3-dev14'
CURRENT_VERSION = 'v0.0.4-alpha1'
known_commands = ('v', 'developer', 'debug_origin', 'error_user', 'all_debug', 'error_name', 'date', 'all_error', 'origin', 'mark_resolve', 'delete_debug', 'help',)

def create_app():
Expand Down Expand Up @@ -132,13 +132,19 @@ def print_stuff_nice_and_good(payload:dict, message: str = None, date_filter_aft
payload: dict the payload you just received from the sqlite_utility file
message: str any extra message you want to add?
"""
os.system('clear')
if payload is None:
return
total = payload['total'] if 'total'in payload else None

if 'error' in payload:
error_message = payload[Utility.Tag_error_message]
print("[[[[[ Error occurred. ]]]]]\nMessage: ```{}```".format(error_message))
else:
os.system('clear')

total = payload[Utility.Tag_Total] if Utility.Tag_Total in payload else None
if total is None:
return
logs = payload['log'] if 'log' in payload else None
logs = payload[Utility.Tag_Log] if Utility.Tag_Log in payload else None
if logs is None:
return

Expand Down
26 changes: 26 additions & 0 deletions ZathuraProject/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@
from datetime import datetime

class Utility:

Tag_User = 'user'
Tag_Error_Name = 'error_name'
Tag_Error_Description = 'error_description'
Tag_Origin = 'point_of_origin'
Tag_Logged_At = 'logged_at'
Tag_Logged_At_Unix = 'logged_at_unix'
Tag_Is_Resolved = 'is_resolved'
Tag_Resolved_At = 'resolved_at'
Tag_Resolved_At_Unix = 'resolved_at_unix'
Tag_Warning_Level = 'warning-level'
Tag_Message = 'message-data'
Tag_Total = 'total'
Tag_Log = 'log'
Tag_Log_CRITICAL = 5
Tag_Log_ERROR = 4
Tag_Log_WARNING = 3
Tag_Log_INFO = 2
Tag_Log_DEBUG = 1
Tag_Log_NotSet = 0
Tag_error_message = 'message'
Tag_Text_Resolved = 'Resolved'
Tag_Text_Not_Resolved = 'Not Resolved'



@staticmethod
def get_timeformat():
"""
Expand Down
136 changes: 88 additions & 48 deletions ZathuraProject/zathura.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import logging
from ZathuraProject.sqlite_definition import ErrorLog, DebugLog, database_connection, close_db, database_start
from datetime import datetime
from ZathuraProject.utility import Utility
Expand All @@ -7,8 +8,44 @@

class Zathura:
def __init__(self):
# TODO: should check if database is already connected or not.
self.empty_result = {'error': True}
self.logger = logging.getLogger('zathura')
__formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
__log_stream_handler = logging.StreamHandler()
__log_stream_handler.setFormatter(__formatter)
self.logger.setLevel(10)
self.logger.addHandler(__log_stream_handler)

def get_logger(self):
"""
returns the logger to other classes if needed
"""
return self.logger

def __logs(self, message: str, level: int):
"""
message: str message to deliver
level: int ``` 0 : No Info
1 : Debug
2 : Info
3 : Warning
4 : Error
5: Critical
```
"""
if level >= 0 and level < 1:
self.logger.log(message)
elif level >= 1 and level < 2:
self.logger.debug(message)
elif level >= 2 and level < 3:
self.logger.info(message)
elif level >= 3 and level < 4:
self.logger.warning(message)
elif level >= 4 and level < 5:
self.logger.error(message)
else:
self.logger.critical(message)


def insert_error_log(self, user:str, error_name:str, error_description:str, warning:int = 0):
"""
Expand All @@ -21,30 +58,26 @@ def insert_error_log(self, user:str, error_name:str, error_description:str, warn
database_connection() # initiate database connection before doing anything.
if warning < 0:
warning = 0
elif warning > 3:
warning = 3
elif warning > 5:
warning = 5
point_of_origin = (inspect.stack()[1].function).lower()
error_log = ErrorLog(_id = str(uuid4()),user = user, error_name = error_name.lower(), error_description = error_description, point_of_origin = point_of_origin, warning_level=warning)
return error_log.save() # number of modified rows are returned. (Always be 1)
except ValueError:
# TODO: add logger
print("Wrong warning field value")
self.logger.exception("Error occurred", exc_info=True)
except SyntaxError:
# TODO: add logger
print("Wrong warning field value")
self.logger.exception("Error occurred", exc_info=True)
finally:
close_db()
# TODO: for future, log if any errors occurred while storing.
else:
if user is None:
print("username cannot be None. It would be easier to log against the user")
if error_name is None:
print("log against a specific error_name.")
if error_description is None:
print("give an appropiate error_description")
if point_of_origin is None:
print("Provide appropiate error entry registry for better understanding!")
print("unknown error occurred")
self.__logs("username cannot be None. It would be easier to log against the user", 4)
elif error_name is None:
self.__logs("log against a specific error_name.", 4)
elif error_description is None:
self.__logs("give an appropiate error_description", 4)
else:
self.__logs("unknown error occurred", 5)
return 0

def insert_debug_log(self, message_data:str, developer:str = 'zathura'):
Expand All @@ -64,8 +97,9 @@ def insert_debug_log(self, message_data:str, developer:str = 'zathura'):
return debug_log.save()
else:
if message_data is None:
print("message_data should not be NONE. Since it's what you are currently logging to remember right?")
print("unknown error occurred")
self.__logs("message_data should not be NONE. Since it's what you are currently logging to remember right?", 3)
else:
self.__logs("unknown error occurred", 3)
return 0

def __error_obj_to_dict(self, error_log_object: ErrorLog):
Expand All @@ -74,16 +108,16 @@ def __error_obj_to_dict(self, error_log_object: ErrorLog):
error_log_object: ErrorLog a ErrorLog object
"""
return {
'user': error_log_object.user,
'error_name': error_log_object.error_name,
'error_description': error_log_object.error_description,
'point_of_origin': error_log_object.point_of_origin,
'logged_at': Utility.milli_to_datetime(error_log_object.logged_at),
'logged_at_unix': error_log_object.logged_at,
'is_resolved': "Resolved" if error_log_object.is_resolved else "Not Resolved",
'resolved_at': error_log_object.resolved_at if error_log_object.resolved_at is None else Utility.milli_to_datetime(error_log_object.resolved_at),
'resolved_at_unix': error_log_object.resolved_at,
'warning_level': self.__get_warning_level_in_text(error_log_object.warning_level),
Utility.Tag_User: error_log_object.user,
Utility.Tag_Error_Name: error_log_object.error_name,
Utility.Tag_Error_Description: error_log_object.error_description,
Utility.Tag_Origin: error_log_object.point_of_origin,
Utility.Tag_Logged_At: Utility.milli_to_datetime(error_log_object.logged_at),
Utility.Tag_Logged_At_Unix: error_log_object.logged_at,
Utility.Tag_Is_Resolved: Utility.Tag_Text_Resolved if error_log_object.is_resolved else Utility.Tag_Text_Not_Resolved,
Utility.Tag_Resolved_At: error_log_object.resolved_at if error_log_object.resolved_at is None else Utility.milli_to_datetime(error_log_object.resolved_at),
Utility.Tag_Resolved_At_Unix: error_log_object.resolved_at,
Utility.Tag_Warning_Level: self.__get_warning_level_in_text(error_log_object.warning_level),
}

@staticmethod
Expand All @@ -93,11 +127,11 @@ def __debug_obj_to_dict(debug_log_object: DebugLog):
debug_log_object: DebugLog a DebugLog object
"""
return {
"user": debug_log_object.user,
"message-data": debug_log_object.message_data,
"point_of_origin": debug_log_object.point_of_origin,
"logged_at": Utility.milli_to_datetime(debug_log_object.logged_at),
"logged_at_unix": debug_log_object.logged_at,
Utility.Tag_User: debug_log_object.user,
Utility.Tag_Message: debug_log_object.message_data,
Utility.Tag_Origin: debug_log_object.point_of_origin,
Utility.Tag_Logged_At: Utility.milli_to_datetime(debug_log_object.logged_at),
Utility.Tag_Logged_At_Unix: debug_log_object.logged_at,
}

def __generate_error_return_payload(self, log_paylod: ModelSelect):
Expand All @@ -107,15 +141,17 @@ def __generate_error_return_payload(self, log_paylod: ModelSelect):
all_error_logs = list()
for err in log_paylod:
all_error_logs.append(self.__error_obj_to_dict(err))
return {"total": len(all_error_logs), "log": all_error_logs}
return {Utility.Tag_Total: len(all_error_logs), Utility.Tag_Log: all_error_logs}

@staticmethod
def __get_warning_level_in_text(warning_level: int):
_ = {
'0': 'warning',
'1': 'Level - 1',
'2': 'Level - 2',
'3': 'Critical'
'0': 'NOTSET',
'1': 'DEBUG',
'2': 'INFO',
'3': 'WARNING',
'4': 'ERROR',
'5': 'CRITICAL'
}
return _[str(warning_level)]

Expand All @@ -126,7 +162,7 @@ def __generate_verbose_return_payload(self, debug_payload: ModelSelect):
all_logs = list()
for log in debug_payload:
all_logs.append(self.__debug_obj_to_dict(log))
return {"total": len(all_logs), "log": all_logs}
return {Utility.Tag_Total: len(all_logs), Utility.Tag_Log: all_logs}

def get_all_error_log(self, show_all: bool = False, desc: bool = False):
"""
Expand Down Expand Up @@ -170,7 +206,8 @@ def get_error_by_user(self, user: str, limit: int=0, desc:bool = False, first_li
"""
if len(user) == 0:
result = self.empty_result
result['message'] = "Username cannot be empty for this function!"
result[Utility.Tag_error_message] = "Username cannot be empty for this function!"
self.__logs(result[Utility.Tag_error_message], Utility.Tag_Log_ERROR)
return result
user = user.strip()
database_connection() # initiate database connection before doing anything.
Expand Down Expand Up @@ -226,7 +263,8 @@ def get_error_by_date_limit(self, beginning_limit: datetime, ending_limit: datet
"""
if beginning_limit is None:
result = self.empty_result
result['message'] = "Please insert the first date to search after a specific time."
result[Utility.Tag_error_message] = "Please insert the first date to search after a specific time."
self.__logs(result[Utility.Tag_error_message], Utility.Tag_Log_ERROR)
return result
first_limit = Utility.unix_time_millis(beginning_limit)
if ending_limit is None:
Expand Down Expand Up @@ -266,7 +304,8 @@ def get_error_by_error_name(self, error_name: str, first_limit: datetime = None,
"""
if error_name is None or len(error_name) == 0:
result = self.empty_result
result['message'] = "Error name cannot be empty on this search"
result[Utility.Tag_error_message] = "Error name cannot be empty on this search"
self.__logs(result[Utility.Tag_error_message], Utility.Tag_Log_ERROR)
return result
error_name = error_name.strip()
error_name = error_name.lower()
Expand Down Expand Up @@ -438,10 +477,12 @@ def mark_resolve(self, error_name: str, origin: str):
"""
result = self.empty_result
if error_name is None or len(error_name) == 0:
result['message'] = "missing error name!"
result[Utility.Tag_error_message] = "missing error name!"
self.__logs(result[Utility.Tag_error_message], Utility.Tag_Log_ERROR)
return result
if origin is None or len(origin) == 0:
result['message'] = 'missing error origin!'
result[Utility.Tag_error_message] = 'missing error origin!'
self.__logs(result[Utility.Tag_error_message], Utility.Tag_Log_ERROR)
return result
database_connection() # initiate database connection before doing anything.
error_name = error_name.strip().lower()
Expand All @@ -454,14 +495,13 @@ def mark_resolve(self, error_name: str, origin: str):
close_db()
return result

@staticmethod
def delete_old_debug():
def delete_old_debug(self):
from datetime import timedelta
database_connection() # initiate database connection before doing anything.
limit = (datetime.now() - timedelta(days=7)).replace(hour=0, minute=0, second=0, microsecond=0)
print("Deleting record older than {}".format(limit.strftime('%A, %d %B, %Y')))
self.__logs("Deleting record older than {}".format(limit.strftime('%A, %d %B, %Y')), Utility.Tag_Log_INFO)
today = Utility.unix_time_millis(limit)
delete_stuff = DebugLog.delete().where(DebugLog.logged_at < today)
result = delete_stuff.execute()
close_db()
print("Deleted {} debug entries".format(result))
self.__logs("Deleted {} debug entries".format(result), Utility.Tag_Log_INFO)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(name='zathura',
packages = ['ZathuraProject'],
version='0.0.3.dev14',
version='0.0.4.a1',
description='Zathura is a small space logger. It logs stuff into her logbook as she drifts into vast unknown of development phase.',
url='https://github.com/ibtehaz-shawon/Zathura',
author='Ibtehaz Shawon',
Expand Down

0 comments on commit ddaab14

Please sign in to comment.