[GH-ISSUE #1603] Why is includeStacktraces an option that cannot be set in constructor? #683

Closed
opened 2026-03-04 02:16:57 +03:00 by kerem · 4 comments
Owner

Originally created by @rajyan on GitHub (Oct 9, 2021).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1603

Monolog version 2

Thank you for this great logging tool!

I wanted to ask,

Why is includeStacktraces an option that cannot be set in constructor in LineFormatter and JsonFormatter?

because we have to call includeStacktraces() to enable stacktrace, we need to create an custom formatter just to enable stacktrace in some frameworks.

ex.

note.

If includeStacktraces could be set in constructor, we don't need to create a custom formatter class for these usages and configure it in framework config files.

Originally created by @rajyan on GitHub (Oct 9, 2021). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1603 Monolog version 2 Thank you for this great logging tool! I wanted to ask, Why is includeStacktraces an option that cannot be set in constructor in LineFormatter and JsonFormatter? because we have to call `includeStacktraces()` to enable stacktrace, we need to create an custom formatter just to enable stacktrace in some frameworks. ex. * using JsonFormatter with Laravel * Laravel default Lineformatter enables stacktrace like this, but no way to enable it in JsonFormatter by config. * https://github.com/laravel/framework/blob/b0ae2123b18b689b4bdd8f932f362ebc2531ed2c/src/Illuminate/Log/LogManager.php#L424-L426 * https://github.com/contributte/monolog note. * Symfony handles it it monolog-bundle and can configure it in config files. * https://github.com/symfony/monolog-bundle/blob/92bd02cc10c40605ff5b4cea88a2911e421aed6b/MonologBundle.php#L43-L52 If includeStacktraces could be set in constructor, we don't need to create a custom formatter class for these usages and configure it in framework config files.
kerem 2026-03-04 02:16:57 +03:00
  • closed this issue
  • added the
    Feature
    label
Author
Owner

@juan-morales commented on GitHub (Oct 22, 2021):

Thanks to this issue I found that the code needs to be refactor (IMO). I Will create a PR sometime this Week End or next week... and let you know also

<!-- gh-comment-id:949876153 --> @juan-morales commented on GitHub (Oct 22, 2021): Thanks to this issue I found that the code needs to be refactor (IMO). I Will create a PR sometime this Week End or next week... and let you know also
Author
Owner

@juan-morales commented on GitHub (Oct 26, 2021):

I honestly dont see like a big benefit of adding this parameter in the constructor.

I am thinking that maybe, the method includeStacktraces can return self, so for the laravel code snippet could look like:

protected function formatter()
    {
        return (new LineFormatter(null, $this->dateFormat, true, true))->includeStacktraces();
    }

But still, dont see the benefit.

Also, in LineFormatter the constructor accepts parameters that are overwritten by calling method includeStacktraces() ... so, just adding this parameter in the constructor will not be semantically correct.

I would like to know someone else opinion on this one.

<!-- gh-comment-id:952042727 --> @juan-morales commented on GitHub (Oct 26, 2021): I honestly dont see like a big benefit of adding this parameter in the constructor. I am thinking that maybe, the method `includeStacktraces` can return `self`, so for the laravel code snippet could look like: ```php protected function formatter() { return (new LineFormatter(null, $this->dateFormat, true, true))->includeStacktraces(); } ``` But still, dont see the benefit. Also, in `LineFormatter` the constructor accepts parameters that are overwritten by calling method `includeStacktraces()` ... so, just adding this parameter in the constructor will not be semantically correct. I would like to know someone else opinion on this one.
Author
Owner

@Seldaek commented on GitHub (Mar 13, 2022):

I think it could be added. It would still implicitly force allowInlineLineBreaks to true in LineFormatter but that can be documented (and possibly throw if allowInlineLineBreaks=false is passed with includeStacktraces=true)

<!-- gh-comment-id:1066171048 --> @Seldaek commented on GitHub (Mar 13, 2022): I think it could be added. It would still implicitly force allowInlineLineBreaks to true in LineFormatter but that can be documented (and possibly throw if allowInlineLineBreaks=false is passed with includeStacktraces=true)
Author
Owner

@rajyan commented on GitHub (Mar 14, 2022):

@Seldaek
Thanks for your response and fixes!

<!-- gh-comment-id:1066689992 --> @rajyan commented on GitHub (Mar 14, 2022): @Seldaek Thanks for your response and fixes!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#683
No description provided.