4

I'm trying to collect debug information in my error handler function. I can analyze the stack and get the file and line number. I also have an array of arguments which I would like to print into my log as well but I don't want to bloat the log file.

I want to print all numbers, booleans, strings (for example the first 50 characters), class names for objects, size of arrays, null for nulls and whatever comes up later in a reasonable form. It should produce no new lines so I have one line per stack entry.

Since I don't want to reinvent the wheel, is there a function out of the box exist in PHP which will behave as described?

3 Answers 3

1

You can check if there are php packages or libs that do logging. For example, pear has some logging packages you can use under Logging.

Sign up to request clarification or add additional context in comments.

1 Comment

I would prefer not to add another lib since this is supposed to be simple
1

First I tried to do it myself:

debug_backtrace(); 

gives array of which I processed using:

protected function backTraceLine(array $line)
{
    $ret = "";
    if (isset ($line['file']))
        $ret = $ret . $line['file'];
    if (isset ($line['function']))
        $ret = $ret . $line['function'];
    if (isset ($line['file']))
        $ret = "[" . $ret . $line['line'] . "]";
    try {
        $args = $line['args'];
        if (is_array($args))
            $ret = $ret . "(" . implode(",", $args) . ")";
    } catch (Exception $e) {
    }
    return $ret;
}

But later I found this method which does exactly what I need:

$exception->getTraceAsString()

If there's no Exception in place we can create new one and get stack from there

Comments

1

You can also use XDebug to generate a stack trace: https://xdebug.org/docs/stack_trace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.