nylen/PHP-Error-Log-Viewer

Define a function for generating warnings

Closed this issue · 5 comments

Description

Have a function, let'say codepotent_log_warning($x) that triggers a warn that can be well-formatted by the plugin page.

Context

When debugging I use to add something like:
trigger_error(print_r($what-is-inside-this, true), E_USER_WARNING);

But of course this breaks into multiple line and it (obviously) shows bad in the log expecially using that wonderful Reverse Sort.
So I'd like something that shows up as well as for Stack Traces.

I am able to trigger errors as you described and they show up in the error log formatted as expected. For example, if I assign a variable, $t.

$t = 'whatever';

...and then trigger a user level error, warning, or notice with any of the following.

trigger_error(print_r($t, true), E_USER_NOTICE);
trigger_error(print_r($t, true), E_USER_WARNING);
trigger_error(print_r($t, true), E_USER_ERROR);

...they show up in the error log with the correct formatting. It seems to work as expected.

Perhaps I haven't understood your request?

Maybe i'm wrong but please try with an array or object.

Great, thanks for the quick follow-up! I have reproduced the behavior you described and agree this should be resolved.

Rather than adding a new method, I think the approach will be to amend the convert_error_log_into_properties() and markup_error_rows() methods to support user-level errors. I will look into this more carefully before committing to this approach, but, one way or another, this will be implemented.

I did end up defining a new method:

public static function log($data, $type='notice', $file=false, $line=false)

Here's how it would be used in long form:

\CodePotent\PhpErrorLogViewer\PhpErrorLogViewer::log($data, 'notice', __FILE__, __LINE__);

A better option is importing the class at the top of your file and giving it a short name:

use CodePotent\PhpErrorLogViewer\PhpErrorLogViewer as sweeeet;

You can then trigger errors like this...

sweeeet::log($data, 'warning', __FILE__, __LINE__);

...or... if the type/filename/line are not important, just log the data.

sweeeet::log($data);

So, that's where I'm at with it and I hope to get a release together in the next week or so. :) Here's an example of triggering a warning while passing in a multidimensional array as $data. It works with objects, as well.

image

Implemented in 2.2.0.