-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from MEHRSHAD-MIRSHEKARY/feature/colorized-con…
…sole-logs Feature/colorized console logs
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class AnsiColors: | ||
BLACK = "\033[0;30m" | ||
RED = "\033[0;31m" | ||
RED_BACKGROUND = "\033[1;41m" | ||
GREEN = "\033[0;32m" | ||
YELLOW = "\033[0;33m" | ||
BLUE = "\033[0;34m" | ||
MAGENTA = "\033[0;35m" | ||
CYAN = "\033[0;36m" | ||
GRAY = "\033[0;37m" | ||
WHITE = "\033[0;38m" | ||
RESET = "\033[0m" | ||
BRIGHT_BLACK = "\033[0;90m" | ||
BRIGHT_RED = "\033[0;91m" | ||
BRIGHT_GREEN = "\033[0;92m" | ||
BRIGHT_YELLOW = "\033[0;93m" | ||
BRIGHT_BLUE = "\033[0;94m" | ||
BRIGHT_MAGENTA = "\033[0;95m" | ||
BRIGHT_CYAN = "\033[0;96m" | ||
BRIGHT_WHITE = "\033[0;97m" | ||
BLACK_BACKGROUND = "\033[40m" | ||
RED_BACKGROUND = "\033[41m" | ||
GREEN_BACKGROUND = "\033[42m" | ||
YELLOW_BACKGROUND = "\033[43m" | ||
BLUE_BACKGROUND = "\033[44m" | ||
MAGENTA_BACKGROUND = "\033[45m" | ||
CYAN_BACKGROUND = "\033[46m" | ||
WHITE_BACKGROUND = "\033[47m" | ||
|
||
|
||
# Mapping log levels to ANSI colors | ||
LOG_LEVEL_COLORS = { | ||
"DEBUG": AnsiColors.BLUE, | ||
"INFO": AnsiColors.GREEN, | ||
"WARNING": AnsiColors.YELLOW, | ||
"ERROR": AnsiColors.RED, | ||
"CRITICAL": AnsiColors.RED_BACKGROUND, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .colorized_formatter import ColorizedFormatter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
from django_logging.settings.conf import LogConfig | ||
from django_logging.utils.colorizer import colorize_log_format | ||
|
||
|
||
class ColorizedFormatter(logging.Formatter): | ||
def format(self, record): | ||
original_format = self._style._fmt | ||
|
||
# checks that the format does not have any color it's self | ||
if LogConfig.remove_ansi_escape_sequences(original_format) == original_format: | ||
colorized_format = colorize_log_format(original_format, record.levelname) | ||
self._style._fmt = colorized_format | ||
|
||
formatted_output = super().format(record) | ||
|
||
# Reset to the original format string | ||
self._style._fmt = original_format | ||
|
||
return formatted_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django_logging.constants.ansi_colors import LOG_LEVEL_COLORS, AnsiColors | ||
|
||
|
||
def colorize_log_format(log_format, levelname): | ||
color_mapping = { | ||
"%(asctime)s": f"{AnsiColors.CYAN}%(asctime)s{AnsiColors.RESET}", | ||
"%(created)f": f"{AnsiColors.BRIGHT_BLUE}%(created)f{AnsiColors.RESET}", | ||
"%(relativeCreated)d": f"{AnsiColors.MAGENTA}%(relativeCreated)d{AnsiColors.RESET}", | ||
"%(msecs)d": f"{AnsiColors.YELLOW}%(msecs)d{AnsiColors.RESET}", | ||
"%(levelname)s": f"{LOG_LEVEL_COLORS.get(levelname, '')}%(levelname)s{AnsiColors.RESET}", | ||
"%(levelno)d": f"{AnsiColors.RED}%(levelno)d{AnsiColors.RESET}", | ||
"%(name)s": f"{AnsiColors.BRIGHT_MAGENTA}%(name)s{AnsiColors.RESET}", | ||
"%(module)s": f"{AnsiColors.BRIGHT_GREEN}%(module)s{AnsiColors.RESET}", | ||
"%(filename)s": f"{AnsiColors.YELLOW}%(filename)s{AnsiColors.RESET}", | ||
"%(pathname)s": f"{AnsiColors.CYAN}%(pathname)s{AnsiColors.RESET}", | ||
"%(lineno)d": f"{AnsiColors.RED}%(lineno)d{AnsiColors.RESET}", | ||
"%(funcName)s": f"{AnsiColors.BRIGHT_BLUE}%(funcName)s{AnsiColors.RESET}", | ||
"%(process)d": f"{AnsiColors.MAGENTA}%(process)d{AnsiColors.RESET}", | ||
"%(thread)d": f"{AnsiColors.CYAN}%(thread)d{AnsiColors.RESET}", | ||
"%(threadName)s": f"{AnsiColors.BRIGHT_MAGENTA}%(threadName)s{AnsiColors.RESET}", | ||
"%(message)s": f"{AnsiColors.GRAY}%(message)s{AnsiColors.RESET}", | ||
} | ||
|
||
for placeholder, colorized in color_mapping.items(): | ||
log_format = log_format.replace(placeholder, colorized) | ||
|
||
return log_format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters