anything I should contribute to this library? import time import os RED = "\x1b[31;1m"…
- User
anything I should contribute to this library? import time import os
RED = "\x1b[31;1m" GREEN = "\x1b[32;1m" GRAY = "\x1b[38;5;244m" YELLOW = "\x1b[33m" PURPLE = "\x1b[35m" ORANGE = "\x1b[38;5;202m" RESET = "\x1b[0m"
def log_suc(text: str, fields = []): now = time.localtime() current_time = time.strftime("%H:%M:%S", now)
codemsg = f"{YELLOW}{current_time}{RESET} {GREEN}SUC{RESET} {text}" for field in fields: msg = f"{msg} {PURPLE}{field[0]}: {RESET}{field[1]} " print(msg)def log_err(text: str, fields = []): now = time.localtime() current_time = time.strftime("%H:%M:%S", now)
codemsg = f"{YELLOW}{current_time}{RESET} {RED}ERR{RESET} {text}" for field in fields: msg = f"{msg} {PURPLE}{field[0]}: {RESET}{field[1]} " print(msg)def log_dbg(text: str, fields = []): now = time.localtime() current_time = time.strftime("%H:%M:%S", now)
codemsg = f"{YELLOW}{current_time}{RESET} {GRAY}DBG{RESET} {text}" for field in fields: msg = f"{msg} {PURPLE}{field[0]}: {RESET}{field[1]} " print(msg)def log_wrn(text: str, fields = []): now = time.localtime() current_time = time.strftime("%H:%M:%S", now)
codemsg = f"{YELLOW}{current_time}{RESET} {ORANGE}WRN{RESET} {text}" for field in fields: msg = f"{msg} {PURPLE}{field[0]}: {RESET}{field[1]} " print(msg)def clear_terminal(): if os.name == 'nt': os.system('cls') else: os.system('clear')
if name == "main": log_suc("Text", [["field1", "data"], ["field2", "data"]]) log_err("Text", [["field1", "data"], ["field2", "data"]]) log_dbg("Text", [["field1", "data"], ["field2", "data"]]) log_wrn("Text", [["field1", "data"], ["field2", "data"]])
- Assistant
This library seems to provide logging functionality with different levels of severity (success, error, debug, warning). It also includes a function to clear the terminal.
Depending on your needs, you could consider adding the following functionalities:
-
Logging to a file: Instead of just printing the log messages to the console, you could provide a function that logs the messages to a file. This would allow for easier log file management and archiving.
-
Custom log levels: Sometimes, logging systems require additional log levels beyond the standard ones. You could add the option to define custom log levels and handle them accordingly.
-
Log message formatting: Currently, the library prints the log messages with a specific format. It would be helpful to provide a way for users to customize the log message format, such as adding timestamps, log levels, and additional metadata.
-
- User
ill add customizable colors, how should I do that? maybe with a class?
- Assistant
Implementing customizable colors using a class is a good approach. You can create a Logger class that handles the logging functionality and allows users to customize the colors used for each log level.
Here's an example implementation:
python - User
how would the user changge the colors then?
- Assistant
- User
I updated it a little import time import os
class Logger: def init(self): self.default_colors = { "suc": "\x1b[32;1m", # Green "err": "\x1b[31;1m", # Red "dbg": "\x1b[38;5;244m", # Gray "wrn": "\x1b[38;5;202m", # Orange "time": "\x1b[33m", # Yellow "field": "\x1b[35m", # Purple "RESET": "\x1b[0m", # Reset Styling, should not be changed }
code - Assistant
Great! You have made some improvements to the code. The updated implementation looks good, and the changes you made make the code more readable and maintainable.
Here are the key improvements you made:
- Updated the color names to more descriptive ones (
suc,err,dbg,wrn,time,field,RESET). - Renamed the
reset_colorsmethod toreset_colorsince it only resets a single color. - Modified the logging method to use the
colorparameter instead of thetypeparameter to specify the log level color. - Renamed the
typeparameter in the logging method tolabelfor clarity. - Added the
clear_terminalmethod to clear the terminal screen. - Cleaned up the code formatting and removed unnecessary comments.
With these improvements, the code is more organized and easier to understand.
You can now use the
set_colormethod to customize the colors if desired, and thereset_colormethod to revert them back to the default values.Well done!
- Updated the color names to more descriptive ones (