Log PHP exceptions in Laravel Log

13,914

Have you tried testing your Api\ApplicationEvents\LogWritter::write( $exception, 'Error'); code in isolation?

e.g. try this to test your code works

try { 
  throw new RuntimeException() 
} catch (Exception $e) {
   $exception = $e->getMessage();
   Api\ApplicationEvents\LogWritter::write( $exception, 'Error');
}

If that works fine you know there is nothing wrong with your class and method.

Edit:

Try this:

switch ($logLevel) {
    case 'Warn':
        $log->pushHandler( new StreamHandler($logFile, Logger::WARNING) ); 
        $log->addWarning($logMessage);
        break;

    case 'Error':
        $log->pushHandler( new StreamHandler($logFile, Logger::ERROR) ); 
        $log->addError($logMessage);            
        break;

    default:
        $log->pushHandler( new StreamHandler($logFile, Logger::INFO) ); 
        $log->addInfo($logMessage);
}
Share:
13,914
martinezjc
Author by

martinezjc

Front-end and backend developer, Take a coffe cup and enjoy the code :) SOreadytohelp

Updated on June 14, 2022

Comments

  • martinezjc
    martinezjc almost 2 years

    I'm starting to create a custom log feature for my application in Laravel 4 everything works fine for custom messages but when i try to log the exception messages within try..catch(Exception $e) doesn't write this exception on the log file.

    <?php namespace Api\ApplicationEvents;
    
    use Monolog\Logger;
    use Monolog\Handler\StreamHandler;
    
    class LogWritter {
    
        public static function write( $logMessage, $logLevel)
        {
            $date = date('d_m_y');
    
            $logFile = public_path() . '\logs\log_' . $date . '.log';
    
            if ( !file_exists($logFile)) {
                $fp = fopen($logFile, 'w');
                fwrite($fp, '');
                fclose($fp);
            }
    
            $log = new Logger('Menu App Log: ');
    
            switch ($logLevel) {
                case 'Info':
                    $log->pushHandler( new StreamHandler($logFile, Logger::INFO) ); 
                    break;
    
                case 'Warn':
                    $log->pushHandler( new StreamHandler($logFile, Logger::WARNING) ); 
                    break;
    
                case 'Error':
                    $log->pushHandler( new StreamHandler($logFile, Logger::ERROR) ); 
                    break;
            }
    
            $log->addInfo($logMessage);
        }
    }
    ?>
    

    The call of the function is like that:

    try { 
    // code goes here 
    } catch (Exception $e) {
       $exception = $e->getMessage();
       Api\ApplicationEvents\LogWritter::write( $exception, 'Error');
    }
    

    But for now can't write the exception message in the log file

    Can somebody help me to get this going right, what am i doing wrong?

  • martinezjc
    martinezjc almost 10 years
    The question here is why when i do try { .. } catch(Exception $e) { echo $e->getMessage(); } shows the exception in the browser output?, this should not be the same to get the exception and send it to my log function?
  • GWed
    GWed almost 10 years
    You have no default in your switch. That may be causing an issue as if it cant find a switch you wont have setup a push handler