Skip to content

Feature Request: Encode stack traces as an array #35

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

Closed
1hakusai1 opened this issue Dec 16, 2024 · 4 comments · Fixed by #39
Closed

Feature Request: Encode stack traces as an array #35

1hakusai1 opened this issue Dec 16, 2024 · 4 comments · Fixed by #39
Labels
enhancement New feature or request open for discussion Community feedback is wanted

Comments

@1hakusai1
Copy link
Contributor

1hakusai1 commented Dec 16, 2024

Hi! I often use this library. It is very useful and I love it.

When the logger using JsonFormatter logs exceptions, the output is like this.

{
  "asctime": "2024-12-16 16:29:19,170",
  "levelname": "ERROR",
  "name": "my-logger",
  "message": "Unexpected Error",
  "exc_info": "Traceback (most recent call last):\n  File \"/Users/hakusai/src/hakusai/stacktrace-as-array/main.py\", line 20, in <module>\n    do_something()\n  File \"/Users/hakusai/src/hakusai/stacktrace-as-array/main.py\", line 14, in do_something\n    raise Exception(\"something wrong\")\nException: something wrong"
}

The stack trace is hard to read. It should be more readable like this.

{
  "asctime": "2024-12-16 16:48:29,666",
  "levelname": "ERROR",
  "name": "my-logger",
  "message": "Unexpected Error",
  "exc_info": [
    "Traceback (most recent call last):",
    "  File \"/Users/hakusai/src/hakusai/stacktrace-as-array/main.py\", line 20, in <module>",
    "    do_something()",
    "  File \"/Users/hakusai/src/hakusai/stacktrace-as-array/main.py\", line 14, in do_something",
    "    raise Exception(\"something wrong\")",
    "Exception: something wrong"
  ]
}

Proposal

  • Add Option such like stack_trace_as_array to JsonFormatter.
    • default: False for backward compatibility.
    • if True, The formatter encodes stack traces as an array.

If you agree this feature, I will create PR.

@nhairs nhairs added enhancement New feature or request open for discussion Community feedback is wanted labels Dec 16, 2024
@nhairs
Copy link
Owner

nhairs commented Dec 16, 2024

Interesting idea - definitely not opposed to it, but would like to hear what others have to say about it.

@edgarrmondragon
Copy link

FWIW, structlog does something similar

{
    "event": "Cannot compute!",
    "exception": [
        {
            "exc_type": "ZeroDivisionError",
            "exc_value": "division by zero",
            "syntax_error": null,
            "is_cause": false,
            "frames": [
                {
                    "filename": "<doctest default[3]>",
                    "lineno": 2,
                    "name": "<module>",
                    "locals": {...,
                        "var": "'spam'"
                    }
                }
            ]
        }
    ]
}

https://www.structlog.org/en/stable/api.html#structlog.processors.JSONRenderer

@nhairs
Copy link
Owner

nhairs commented Dec 17, 2024

That's good to know thanks @edgarrmondragon

@1hakusai1
Copy link
Contributor Author

Thanks! I will implement it.

nhairs added a commit that referenced this issue Feb 4, 2025
close #35 

This PR add options to encode stack information into an array.
- If `exc_info_as_array` is True (Defualt: False), formatter encode
`exc_info` into an array.
- If `stack_info_as_array` is True (Defualt: False), formatter encode
`stack_info` into an array.

### Test plan

- run tests

---------

Co-authored-by: Nicholas Hairs <[email protected]>
@nhairs nhairs closed this as completed in #39 Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request open for discussion Community feedback is wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants