In this example I use Monolog for logging user activity in my cms admin. Usage: add the following line to the controller's constructor: UserLog::write();
<?php namespace Acme\Services\Logger;
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Processor\WebProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Formatter\LineFormatter;
use Request;
use Sentry;
use Config;
/**
* UserLog
*
* Custom monolog logger for CMS user activity logging
*
* @author Barna Szalai <sz.b@devartpro.com>
*/
class UserLog {
/**
* write
* @return void
*/
public function write()
{
// if feature is enabled..
if (Config::get('app.userlog'))
{
// logger instance
$log = new Logger('userlog');
// handler init, making days separated logs
$handler = new RotatingFileHandler(storage_path().'/logs/useractivity.log', 0, Logger::INFO);
// formatter, ordering log rows
$handler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %extra% %context%\n"));
// add handler to the logger
$log->pushHandler($handler);
// processor, adding URI, IP address etc. to the log
$log->pushProcessor(new WebProcessor);
// processor, memory usage
$log->pushProcessor(new MemoryUsageProcessor);
// custom: I add user id and permission group (by Sentry) to log
$user = Sentry::getUser();
$group = $user->getGroups();
$log->addInfo('USER ID: '.$user->id.' | USER GROUP: '.$group[0]->name.' |');
}
}
}
---
<?php namespace Acme\Facades;
use Illuminate\Support\Facades\Facade;
/**
* UserLog Facade
*
* @author Barna Szalai <sz.b@devartpro.com>
*/
class UserLog extends Facade {
protected static function getFacadeAccessor() { return 'userlog'; }
}
---
app/config/app.php:
'aliases' => array(
...
'UserLog' => 'Acme\Facades\UserLog',
),