Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added log output trim #157

Merged
merged 1 commit into from
Nov 10, 2022
Merged

Conversation

matllubos
Copy link
Collaborator

No description provided.

@coveralls
Copy link

coveralls commented Nov 9, 2022

Pull Request Test Coverage Report for Build 3437739245

  • 92 of 92 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 92.03%

Totals Coverage Status
Change from base Build 2818596547: 0.3%
Covered Lines: 3314
Relevant Lines: 3601

💛 - Coveralls

if len(value) <= max_length:
return value

truncated_value = value[len(value) + 2 - (max_length):]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+2 is added because of '…\n'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this comment to the code :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also unnecessary () around max_length.


.. attribute:: SECURITY_LOG_STRING_OUTPUT_TRUNCATE_OFFSET

Because too often string truncation can cause high CPU load text is truncated is truncated by more characters. Setting define this value which default value is ``1000``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Because too often string truncation can cause high CPU load text is truncated is truncated by more characters. Setting define this value which default value is ``1000``.
Because too frequent string truncation can cause high CPU load, log string is truncated by more characters. This setting defines this value which is by default ``1000``.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar description is also down below, in the docstring.

@@ -32,17 +32,31 @@ def is_atty_string(s):
return bool(re.search(r'^' + re.escape('\x1b[') + r'\d+m$', s))


def truncate_newline_from_left(value, max_length):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather name it truncate_lines_from_left.

if len(value) <= max_length:
return value

truncated_value = value[len(value) + 2 - (max_length):]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this comment to the code :-)

truncated_value_split_by_newline[0]
if len(truncated_value_split_by_newline) == 1
else truncated_value_split_by_newline[1]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note. I can imagine a situation, when command prints a looooong line with for example IDs processed, and then prints "DONE". Then the log will contain just "DONE".

Processed IDs: 1,2,3,4,5,6,7,8,9.................
DONE.

This can be a surprising behavior. Maybe it should be mentioned in the documentation.

def test_log_string_io_should_be_truncated_according_to_settings(self):
log_string_io = LogStringIO(write_time=False)
for _ in range(20):
log_string_io.write((4*'a') + '\n')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces between operator and operands. Also on the line below.

assert_equal(log_string_io.getvalue(), 20 * (4*'a' + '\n'))
assert_equal(len(log_string_io.getvalue()), 100)
log_string_io.write('b')
assert_equal(log_string_io.getvalue(), '…\n' + 17 * (4 * 'a' + '\n') + 'b')
Copy link
Member

@rubickcz rubickcz Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 17 instead of 19? The b causes exceeding the limit, so one line is stripped off and replaced with …\n. (I know tests are passing, maybe I am a little tired today and not getting it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons, offset 10 characters which will do 18 but, b character is computed to the limit to. This is the reason why 17

@matllubos matllubos force-pushed the DjangoSecurityLoggerAddLogOutputTrim branch from 2bccdad to 09500b0 Compare November 10, 2022 14:41
@matllubos matllubos force-pushed the DjangoSecurityLoggerAddLogOutputTrim branch from 09500b0 to 8ae9f93 Compare November 10, 2022 14:42
@matllubos matllubos merged commit 37088ab into master Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants