• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display error log

#1
In the admin back office of my website, I would like to display logged errors (located in /application/logs). Is there a simple way to do that or should we parse the files with file_get_contents ?
Reply

#2
file_get_contents() will work just fine. The log files are plain text so no "parsing" is needed. Simply take the string returned and pass it to your view.

You could also use fopen() and its associated filesystem functions to read the data. A more robust and reusable implementation might want to be able to read compressed files. My solution to that uses gzopen() instead of fopen() and looks like this.

PHP Code:
define('LINESP'"<br><br>");  //if you want
$logFile "";
$handle  gzopen($file"rb");  //$file is the full path to the file
if($handle)
{
 
   while( ! gzeof($handle))
 
   {
 
       $buffer gzgets($handle4096);
 
       $logFile .= $buffer LINESP;
 
   }
 
   gzclose($handle);
}
//pass file contents to view
$data['logFile'] = $logFile;
$this->load->view("log_viewer_view"$data); 

gzopen() also reads uncompressed files making it a multi-purpose solution.
Reply

#3
Thank you, that's perfect. 

By the way, I made a parser for displaying formatted error messages. I assumed that one error = one line, but I realized that SQL queries made with the Query Builder Class contains carriage return. When an error contains a SQL query, it become impossible to differentiate this single multi-line message from several single-line messages. 

Of course, checking for DEBUG, INFO at the beginning of the line is possible, but not very satisfying if the query contains one of this keyword ?

That's not really important, but may be an improvement for CodeIgniter 4 !
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.