CodeIgniter Forums
Display error log - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Display error log (/showthread.php?tid=71905)



Display error log - imabot - 10-07-2018

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 ?


RE: Display error log - dave friend - 10-07-2018

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.


RE: Display error log - imabot - 10-07-2018

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 !