17
17
# Logger used throughout the library
18
18
logger = logging .getLogger (logger_name )
19
19
20
+ _LOG_NAME_COLOR = Fore .LIGHTBLACK_EX
20
21
21
22
_LOG_LEVEL_COLOR = {
22
23
logging .DEBUG : Fore .BLUE ,
@@ -54,6 +55,14 @@ class ActorLogFormatter(logging.Formatter):
54
55
# and extract all the extra ones not present in the empty log record
55
56
empty_record = logging .LogRecord ('dummy' , 0 , 'dummy' , 0 , 'dummy' , None , None )
56
57
58
+ def __init__ (self , include_logger_name : bool = False , * args : tuple , ** kwargs : dict ) -> None :
59
+ """ # noqa
60
+ Args:
61
+ include_logger_name: Include logger name at the beginning of the log line. Defaults to False.
62
+ """
63
+ super ().__init__ (* args , ** kwargs ) # type: ignore
64
+ self .include_logger_name = include_logger_name
65
+
57
66
def _get_extra_fields (self , record : logging .LogRecord ) -> Dict [str , Any ]:
58
67
extra_fields : Dict [str , Any ] = {}
59
68
for key , value in record .__dict__ .items ():
@@ -72,6 +81,8 @@ def format(self, record: logging.LogRecord) -> str:
72
81
- then has the stringified extra log fields
73
82
- then, if an exception is a part of the log record, prints the formatted exception.
74
83
"""
84
+ logger_name_string = f'{ _LOG_NAME_COLOR } [{ record .name } ]{ Style .RESET_ALL } '
85
+
75
86
# Colorize the log level, and shorten it to 6 chars tops
76
87
level_color_code = _LOG_LEVEL_COLOR .get (record .levelno , '' )
77
88
level_short_alias = _LOG_LEVEL_SHORT_ALIAS .get (record .levelno , record .levelname )
@@ -97,4 +108,8 @@ def format(self, record: logging.LogRecord) -> str:
97
108
log_string = super ().format (record )
98
109
log_string = textwrap .indent (log_string , _LOG_MESSAGE_INDENT ).lstrip ()
99
110
100
- return f'{ level_string } { log_string } { extra_string } { exception_string } '
111
+ if self .include_logger_name :
112
+ # Include logger name at the beginning of the log line
113
+ return f'{ logger_name_string } { level_string } { log_string } { extra_string } { exception_string } '
114
+ else :
115
+ return f'{ level_string } { log_string } { extra_string } { exception_string } '
0 commit comments