silinternational / yii2-json-log-targets

A collection of Yii2 log targets that format the log message as a JSON string.
MIT License
5 stars 2 forks source link

Provide stack traces on exception errors #27

Open cluwong opened 3 years ago

cluwong commented 3 years ago

When there's exception errors, there are no stack trace information where the error comes from.

{
  "level": "error",
  "category": "yii\\base\\ErrorException:8",
  "message": {
    "code": 8,
    "exception": "Undefined variable: data1"
  }
}

Can this be set in config somewhere? Or be enhanced to include such feature?

briskt commented 3 years ago

There is no config to generate a stack trace. Even if we were to add such capability, I think it would not be useful because any stack trace obtained in this library will only give you the stack at the point of logging the message. What you really want is the stack at the point of failure. You could do something like this in your code:

        try {
            doSomething();
        } catch(\Exception $e) {
            \Yii::error([
                    "exception_message" => $e->getMessage(),
                    "stack" => $e->getTraceAsString(),
            ]);
        }

However, that will only give you the stack trace from the point of doSomething() not where the exception actually occurred.