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

Support field width with alignment in YScopeFormatter. #139

Open
junhaoliao opened this issue Nov 28, 2024 · 2 comments
Open

Support field width with alignment in YScopeFormatter. #139

junhaoliao opened this issue Nov 28, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@junhaoliao
Copy link
Collaborator

junhaoliao commented Nov 28, 2024

Request

In log formatting, consistent and readable output is critical for debugging and analysis. Fields like log levels (INFO, WARNING) often have variable lengths, making the output misaligned. A field width with alignment formatter ensures that fields occupy consistent space in the output, improving readability and enabling proper alignment.

Use Case Analysis: Example Logs

Input Example:

2023-03-21 23:45:49 I [conn11] Slow query
2023-03-21 23:45:49 D3 [conn11] WT begin_transaction

Problem:
The log levels (I, D3) have variable lengths:

  • I is 1 character.
  • D3 is 2 characters.
    Without padding, the alignment of subsequent fields (e.g., [conn11]) shifts, making the logs harder to read at a glance.

Solution:
Using a field width formatter, the log levels can be aligned to a fixed width (e.g., 2 characters) by padding shorter levels and ensuring uniform spacing.
Desired output:

2023-03-21 23:45:49  I [conn11] Slow query
2023-03-21 23:45:49 D3 [conn11] WT begin_transaction

Key Features

  1. Minimum width of the field:
    • If the content is shorter than the width, padding is added.
    • If the content is longer, it is not truncated.
  2. Alignment:
    • Left: Content is aligned to the left (padding added on the right).
    • Right: Content is aligned to the right (padding added on the left).
    • Center: Content is centered (padding added on both sides).
  3. Padding (Filling) Character:
    • allow specifying a character to use for padding (default is a space).

Possible implementation

Field width formatter syntax

Field formatter name: field_width
Options string syntax: <width>[[;<left|center|right>];<delimiter>]
Components of the syntax:

  1. <width> (Required). Examples:
    • 10: Ensures the field is at least 10 characters wide.
  2. [;<left|center|right>] (Optional unless a delimiter is specified). Examples:
    • 10;center: Ensures the field is 10 characters wide, with content centered.
    • 15;right: Ensures the field is 15 characters wide, with content right-aligned.
  3. [;<fill>] (Optional). Examples:
    • 10;left;-: Ensures the field is 10 characters wide, left-aligned, with - for padding.
    • 8;right;*: Ensures the field is 8 characters wide, right-aligned, with * for padding.

Reference

Inspired by:

  1. https://docs.python.org/3/library/string.html#formatstrings
  2. Cpp:
    1. https://cplusplus.com/reference/ios/ios_base/width/
    2. https://cplusplus.com/reference/ios/ios_base/setf/
@junhaoliao junhaoliao added the enhancement New feature or request label Nov 28, 2024
@junhaoliao
Copy link
Collaborator Author

@davemarco / @kirkrodrigues please help review

@kirkrodrigues
Copy link
Member

The proposal makes sense, but now I'm wondering how this works for fields of different types and whether that might be confusing to users.

For instance, we could allow using this formatter on all field types, and then we simply pad the stringified version of the field. This would work in most cases, but for a negative float field where the user wants to 0-pad it on the left, this formatter wouldn't work (since the 0 padding would be placed left of the negative sign, e.g., "000-5.5"). So in this case, we'd probably need a numeric padding formatter, right? That's also doable, but I have a feeling that a padding formatter could handle both if we made the syntax more advanced somehow. But then again, I haven't thought it through and there might be limitations to trying to handle both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants