I have the script is which runned by Cron jobs. So, while it runs, I can't see what output of the task is. (errors and so on.)
So I need to write browser output to log file using PHP. How can I do this ?
6 Answers
You can use output buffers to write browser output to a log file,
Example:
<?php
ob_start();
echo 'Lorem ipsum dolor sit amet consectetur adipiscing elit';
echo 'Cras in dolor fringilla est fermentum porttito';
echo 'bla bla bla...';
echo 'bla bla bla...';
file_put_contents('/path/to/log.txt',ob_get_contents());
ob_end_flush();
?>
Comments
The I prefer to do is the output redirection using bash features:
php myscript.php > logfile.log
But you need to know that PHP outputs errors to stderr. So, you need to redirect stderr too. Everything is described here: http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/ . Some examples according to this page:
php myscript.php &>file
or
php myscript.php > file-name 2>&1
Also, you cannot use output control functions like ob_start() and friends alone, because some fatal error can abort output buffering. So, if you prefer PHP-only way, you need to define:
- Output buffering
- Error handler (or you can fetch errors using output buffering)
- Shutdown function - to fetch fatal errors
P.S. Usually, cron job`s output is written to crontab`s log
Comments
I use this script for error logs:
// Destinations
define("ADMIN_EMAIL", "[email protected]");
define("LOG_FILE", "/my/home/errors.log");
// Destination types
define("DEST_EMAIL", "1");
define("DEST_LOGFILE", "3");
/**
* my_error_handler($errno, $errstr, $errfile, $errline)
*
* Author(s): thanosb, ddonahue
* Date: May 11, 2008
*
* custom error handler
*
* Parameters:
* $errno: Error level
* $errstr: Error message
* $errfile: File in which the error was raised
* $errline: Line at which the error occurred
*/
function my_error_handler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
// Send an e-mail to the administrator
error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);
// Write the error to our log file
error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
break;
case E_USER_WARNING:
// Write the error to our log file
error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
case E_USER_NOTICE:
// Write the error to our log file
error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
default:
// Write the error to our log file
error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
break;
}
// Don't execute PHP's internal error handler
return TRUE;
}
// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler("my_error_handler");
It gives full control over what actions to take when an error is raised.
Taken directly from Web Services Wiki of Stanford University
Comments
You could redirect the output of the php script in the cron instruction using the file redirection "php yourfile.php > logfile.log" or you can use php's file_put_contents to save to a file anywhere you would have called print.
Comments
firstly create javascript code in html page
var UI_LOG_ENABLED_FOR = 'ALL';
var userTokensLog = UI_LOG_ENABLED_FOR.split(',');
var logTextServer = [];
if( UI_LOG_ENABLED_FOR =='ALL' )
{
var writeLogIntoFile = function(type,text1,text2,text3){
logTextServer.push([new Date(),type,text1,text2,text3]);
}
// define a new console
var console=(function(oldCons){
return {
log: function(text1,text2=null,text3=null){
oldCons.log(text1,text2,text3);
writeLogIntoFile('log',text1,text2,text3);
},
info: function (text1,text2=null,text3=null) {
oldCons.info(text1,text2,text3);
writeLogIntoFile('info',text1,text2,text3);
},
warn: function (text1,text2=null,text3=null) {
oldCons.warn(text1,text2,text3);
writeLogIntoFile('warn',text1,text2,text3);
},
table: function (text1,text2=null,text3=null) {
oldCons.warn(text1,text2,text3);
writeLogIntoFile('warn',text1,text2,text3);
},
error: function (text1,text2=null,text3=null) {
oldCons.error(text1,text2,text3);
writeLogIntoFile('error',text1,text2,text3);
}
};
}(window.console));
//Then redefine the old console
window.console = console;
//Amol Patil
setInterval(function(){
try{
if(logTextServer.length >0){
$.post('/api/v1/store-ui-log', {username:'xyz-user',log_data:logTextServer}, function(response){
});
}
logTextServer = [];
}catch(err){
console.error(err);
}
},5000);
}
then call PHP API PHP CODE :
public function createLog(Request $request)
{
$path = storage_path() . '/ui_logs';
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$path = storage_path() . '/ui_logs/Log_'.date('Y-m-d');
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$path = $path.'/'.$request->username.'.txt';
$myfile = fopen($path, "a+") or die("Unable to open file!");
foreach($request->get('log_data') as $log){
fwrite($myfile, json_encode($log).PHP_EOL);
}
fclose($myfile);
return "done";
}