Can Laravel 4 log to a MySQL database?

As you can see if you read further the headline, Monolog natively supports writing to Redis, MongoDB and CouchDB. Those three are all supporting fairly write heavy (and very write heavy in the case of Redis) use-cases. MySQL is not there because logging to MySQL isn't really the best idea in the world.

If you really want to do it though, you can check the docs on creating your own handler, which explains how to create and use a PDO handler to write to a SQL database: https://github.com/Seldaek/monolog/blob/master/doc/extending.md - I still think it's a bad idea, but maybe the use case warrants it.


Yep, You could create a listener to log everything in the routes.php

Event::listen('laravel.log', function($type,$message)
{
    $log = new Log();
    $log->message = $message;
    $log->type = $type;
    $log->update;
});

Or alternatively if you wanted to only log errors 400 and 500 Larvavel there is a Log event in the Routes.php file which listens to errors 404 and 500, you can write your own code in this event listener. So assuming you have a Model called Log defined,

Event::listen('404', function()
{
    $error = "404: " . URL::full();
    Log::error($error);
    $update = new Log();
    $update->error = $error;
    $update->update;
    return Response::error('404');
});

Event::listen('500', function()
{
    Log::error($error);
    $update = new Log();
    $update->error = $error;
    $update->update;
    return Response::error('500');
});