[php] How to print a debug log?

I'd like to debug some PHP code, but I guess printing a log to screen or file is fine for me.

How should I print a log in PHP code?

The usual print/printf seems to go to HTML output not the console.

I have Apache server executing the PHP code.

This question is related to php debugging logging

The answer is


You can use the php curl module to make calls to http://liveoutput.com/. This works great in an secure, corporate environment where certain restrictions in the php.ini exists that restrict usage of file_put_contents.


Simply way is trigger_error:

 trigger_error("My error");

but you can't put arrays or Objects therefore use

var_dump

If you are on Linux:

file_put_contents('your_log_file', 'your_content');

or

error_log ('your_content', 3, 'your_log_file');

and then in console

tail -f your_log_file

This will show continuously the last line put in the file.


I have used many of these, but since I usually need to debug when developing, and since I develop on localhost, I have followed the advice of others and now write to the browser's JavaScript debug console (see http://www.codeforest.net/debugging-php-in-browsers-javascript-console).

That means that I can look at the web page which my PHP is generating in my browser & press F12 to quickly show/hide any debug trace.

Since I am constantly looking at the developer tools for debugger, CSS layout, etc, it makes sense to look at my PHP loggon there.

If anyone does decide to us that code, I made one minor change. After

function debug($name, $var = null, $type = LOG) {

I added

$name = 'PHP: ' . $name;

This is because my server side PHP generates HTML conatining JavaScript & I find it useful to distinguish between output from PHP & JS.

(Note: I am currently updating this to allow me to switch on & off different output types: from PHP, from JS, and database access)


If you don't want to integrate a framework like Zend, then you can use the trigger_error method to log to the php error log.


I use cakephp so I use:

$this->log(YOUR_STRING_GOES_HERE, 'debug');

error_log(print_r($variable, TRUE)); 

might be useful


Are you debugging on console? There are various options for debugging PHP. The most common function used for quick & dirty debugging is var_dump.

That being said and out of the way, although var_dump is awesome and a lot of people do everything with just that, there are other tools and techniques that can spice it up a bit.

Things to help out if debugging in a webpage, wrap <pre> </pre> tags around your dump statement to give you proper formatting on arrays and objects.

Ie:

<div> some html code ....
      <a href="<?php $tpl->link;?>">some link to test</a>
</div>

      dump $tpl like this:

    <pre><?php var_dump($tpl); ?></pre>

And, last but not least make sure if debugging your error handling is set to display errors. Adding this at the top of your script may be needed if you cannot access server configuration to do so.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Good luck!


You can use

<?php
{
    AddLog("anypage.php","reason",ERR_ERROR);
}
?>

or if you want to print that statement in an log you can use

AddLog("anypage.php","string: ".$string,ERR_DEBUG_LOW);

This a great tool for debugging & logging php: PHp Debugger & Logger

It works right out of the box with just 3 lines of code. It can send messages to the js console for ajax debugging and can replace the error handler. It also dumps information about variables like var_dump() and print_r(), but in a more readable format. Very nice tool!


You can use error_log to send to your servers error log file (or an optional other file if you'd like)


You can use:

<?php
echo '<script>console.log("debug log")</script>';
?>

You need to change your frame of mind. You are writing PHP, not whatever else it is that you are used to write. Debugging in PHP is not done in a console environment.

In PHP, you have 3 categories of debugging solutions:

  1. Output to a webpage (see dBug library for a nicer view of things).
  2. Write to a log file
  3. In session debugging with xDebug

Learn to use those instead of trying to make PHP behave like whatever other language you are used to.


You can also write to a file like this:

$logFilePath = '../logs/debug.text';
ob_start();

// if you want to concatenate:
if (file_exists($logFilePath)) {
    include($logFilePath);
}
// for timestamp
$currentTime = date(DATE_RSS);

// echo log statement(s) here
echo "\n\n$currentTime - [log statement here]";

$logFile = fopen($logFilePath, 'w');
fwrite($logFile, ob_get_contents());
fclose($logFile);
ob_end_flush();

Make sure the proper permissions are set so php can access and write to the file (775).


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to logging

How to redirect docker container logs to a single file? Console logging for react? Hide strange unwanted Xcode logs Where are logs located? Retrieve last 100 lines logs Spring Boot - How to log all requests and responses with exceptions in single place? How do I get logs from all pods of a Kubernetes replication controller? Where is the Docker daemon log? How to log SQL statements in Spring Boot? How to do logging in React Native?