Skip to content
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

fix function log from Slim/Log.php #851

Merged
merged 3 commits into from
Feb 25, 2015
Merged

fix function log from Slim/Log.php #851

merged 3 commits into from
Feb 25, 2015

Conversation

alexandrubagu
Copy link

Now you can print mixed variables: $app->log->debug(mixed )

$response = array( "data" => "value" );
$app->log->debug($response);

and also:

$my_obj = new stdClass;
$my_obj->fix = 1;
$my_obj->email = "[email protected]";
$app->log->debug($my_obj);

@danielgsims
Copy link

I see one problem with this. If you have an object, it will always return the print_r of that object. If the object can be cast a string using the __toString magic method, it won't be triggered by the print_r function.

http://us3.php.net/manual/en/function.print-r.php#111738

Of course you could always go with something like this:

if (is_array($object) || (is_object($object) && !method_exists($object, "__toString"))) {
    $message = print_r($object,1);
} else {
    $message = (string) $object;
}

There are a lot of other ways to cast an object to a string out there. This kind of code is an antipattern because you can't cover all of your cases. For example, what if I implement the Json Serializable interface and want to log all of my classes as JSON? I think it's preferable to go one of two paths: handle all objects the same way, or create a user definable way to handle these logging methods.

@alexandrubagu
Copy link
Author

I think your solution is the best. I modified the Log.php file to reflect your sulution.

For other behavior I think a solution is to define a CustomLogWriter

new CustomLogWriter() ));

@maldoinc
Copy link

maldoinc commented Jul 5, 2014

I'd also recommend passing true to the print_r function as per it's signature, rather than relying on php's implicit type conversion

@alexandrubagu
Copy link
Author

I modified the print_r statement to use true instead of 1(int)

@bensquire
Copy link

Is there any chance this fix can make it into a tagged release? That would be really useful :)

codeguy added a commit that referenced this pull request Feb 25, 2015
fix function log from Slim/Log.php
@codeguy codeguy merged commit 9f1ee51 into slimphp:master Feb 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants