Skip to content

Commit 1645c80

Browse files
committed
Update Apify log formatter to contain logger name
1 parent b1a104a commit 1645c80

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
Changelog
22
=========
33

4-
[1.1.5](../../releases/tag/v1.4.5) - Unreleased
4+
[1.1.6](../../releases/tag/v1.4.5) - Unreleased
55
-----------------------------------------------
66

7+
...
8+
9+
[1.1.5](../../releases/tag/v1.4.5) - 2023-10-03
10+
-----------------------------------------------
11+
12+
### Added
13+
14+
- Update the Apify log formatter to contain an option for adding the logger name
15+
716
### Internal changes
817

918
- rewrote documentation publication to use Docusaurus

src/apify/log.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Logger used throughout the library
1818
logger = logging.getLogger(logger_name)
1919

20+
_LOG_NAME_COLOR = Fore.LIGHTBLACK_EX
2021

2122
_LOG_LEVEL_COLOR = {
2223
logging.DEBUG: Fore.BLUE,
@@ -54,6 +55,14 @@ class ActorLogFormatter(logging.Formatter):
5455
# and extract all the extra ones not present in the empty log record
5556
empty_record = logging.LogRecord('dummy', 0, 'dummy', 0, 'dummy', None, None)
5657

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+
5766
def _get_extra_fields(self, record: logging.LogRecord) -> Dict[str, Any]:
5867
extra_fields: Dict[str, Any] = {}
5968
for key, value in record.__dict__.items():
@@ -72,6 +81,8 @@ def format(self, record: logging.LogRecord) -> str:
7281
- then has the stringified extra log fields
7382
- then, if an exception is a part of the log record, prints the formatted exception.
7483
"""
84+
logger_name_string = f'{_LOG_NAME_COLOR}[{record.name}]{Style.RESET_ALL} '
85+
7586
# Colorize the log level, and shorten it to 6 chars tops
7687
level_color_code = _LOG_LEVEL_COLOR.get(record.levelno, '')
7788
level_short_alias = _LOG_LEVEL_SHORT_ALIAS.get(record.levelno, record.levelname)
@@ -97,4 +108,8 @@ def format(self, record: logging.LogRecord) -> str:
97108
log_string = super().format(record)
98109
log_string = textwrap.indent(log_string, _LOG_MESSAGE_INDENT).lstrip()
99110

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

Comments
 (0)