mymotherland
mymotherland

Reputation: 8226

How do i format the error log message in log file using PHP

Suppose I want to display the sql error in log file which has been created by me. For example ,error log file called "myerror.log "

Now i am using the following code to print my message in log file , " error_log(mysql_error(), 3, "tmp/myerror.log"); ". So Every time when message is printed in myerror.log file with same line, Here i want to print the message one after another.

Kindly help me Thanks Dinesh Kumar Manoharan

Upvotes: 0

Views: 2213

Answers (2)

LazyOne
LazyOne

Reputation: 165138

As I understand you want to have each entry/message on new line. If so:

error_log(mysql_error() . PHP_EOL, 3, "tmp/myerror.log");

If you want to have timestamp as well, you will have to add it yourself:

$dt = date('Y-m-d H:i:s', time());
error_log("[{$dt}] " . mysql_error() . PHP_EOL, 3, "tmp/myerror.log");

If you need to constantly use such formatting I recommend to create your own function (something like this):

define('MY_ERROR_LOG', 'tmp/myerror.log');

function myErrorLog($message)
{
    $dt = date('Y-m-d H:i:s', time());
    error_log("[{$dt}] " . $message . PHP_EOL, 3, MY_ERROR_LOG);
}
// use it
myErrorLog(mysql_error());

Upvotes: 3

Teneff
Teneff

Reputation: 32158

maybe you need function like this:

function error_log($message, $log_file) {
    file_put_contents($log_file, $message . "\n");
}

Upvotes: 1

Related Questions