Conversation
The logger is working now. Also changed some layout of the timer message. Half implemented the storage functionality. Will complete more later.
Jh123x
left a comment
There was a problem hiding this comment.
Thanks a lot for your contribution to the MR, especially the types for the __main__ file.
| logs | ||
| database No newline at end of file |
There was a problem hiding this comment.
| logs | |
| database | |
| logs/ | |
| database/ |
Maybe we can add the / at the back to denote directories
| if os.path.isfile(f"./database/storage.json"): | ||
| logger.info("Loading stored events...") | ||
| with open("./database/storage.json", "r") as storageFile: | ||
| try: | ||
| self.storage = json.load(storageFile) | ||
| except Exception as e: | ||
| logger.error("Error loading stored events", {str(e)}) | ||
| self.storage = {} | ||
| else: | ||
| logger.info("No saved events found.") | ||
| self.storage = {} | ||
| os.makedirs("./database", exist_ok=True) | ||
| with open("./database/storage.json", "w") as storageFile: | ||
| json.dump(self.storage, storageFile, indent=4) | ||
|
|
There was a problem hiding this comment.
This is not an ideal solution for writing into a file.
If server is stopped unexpectedly, the events will not saved.
In this case it might be better to use something like sqlite3.
| """Stores the information for each user""" | ||
| # Load the shelve db if possible | ||
| self.storage = {} | ||
| if os.path.isfile(f"./database/storage.json"): |
There was a problem hiding this comment.
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
...
path = os.path.join(BASE_DIR,"database","storage.json")Filepath can be improved using the snippet above instead of ./ as the location may vary based on the directory where the command was run.
| format = "(black){asctime}(reset) (levelcolor){levelname}(reset) (green){name}(reset) {message}" | ||
| format = format.replace("(black)", self.black + self.bold) | ||
| format = format.replace("(reset)", self.reset) | ||
| format = format.replace("(levelcolor)", log_color) | ||
| format = format.replace("(green)", self.green + self.bold) |
There was a problem hiding this comment.
| format = "(black){asctime}(reset) (levelcolor){levelname}(reset) (green){name}(reset) {message}" | |
| format = format.replace("(black)", self.black + self.bold) | |
| format = format.replace("(reset)", self.reset) | |
| format = format.replace("(levelcolor)", log_color) | |
| format = format.replace("(green)", self.green + self.bold) | |
| format = "{black}{{asctime}}{reset} {levelcolor}{{levelname}}{reset} {green}{{name}}{reset} {{message}}".format( | |
| black= self.black + self.bold, | |
| reset = self.reset, | |
| levelcolor=log_color, | |
| green=self.green+self.bold, | |
| ) |
We can make use of {{ and }} to escape the { and } so that we can format them later.
| """ | ||
| Throws ValueError when the format is incorrect | ||
| """ | ||
| chat_id = str(chat_id) |
There was a problem hiding this comment.
Maybe we can create another class to deal with the file logging (opening/closing/writing to files ) and only write to the file when the destructor of the class is called.
| seconds = time.seconds % 60 | ||
| return TIME_FORMAT.format(days=time.days, hours=hours, minutes=minutes, seconds=seconds) | ||
|
|
||
| time_string = "" |
There was a problem hiding this comment.
The formatting logic here can be handled by changing the TIME_FORMAT variable
|
|
||
| # Logger Format | ||
| LOGGER_FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' | ||
| # LOGGER_FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' |
There was a problem hiding this comment.
Maybe we can remove commented code. If we have a need to retrieve it, we can look at the previous versions of the code.
The logger is working now.
Also changed some layout of the timer message.
Half implemented the storage functionality. Will complete more later.
Merge if you like it. I would love to contribute more. :)