Skip to content

Instantly share code, notes, and snippets.

@ryun
Last active August 29, 2015 14:24
Show Gist options
  • Save ryun/60d45c581f9d71c00921 to your computer and use it in GitHub Desktop.
Save ryun/60d45c581f9d71c00921 to your computer and use it in GitHub Desktop.
Monolog Redis Setup
<?php
// Setup Redis Logger
$app->configureMonologUsing(function ($monolog) use ($app) {
$redis = new RedisHandler('logs', 'logs:'.gethostname().':'.date('Y.m.d'));
$monolog->pushHandler($redis);
});
<?php namespace App\Core\Logs;
use DateTime;
use Illuminate\Redis\Database;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
/**
* Logs to a Redis key using rpush
*/
class RedisHandler extends AbstractProcessingHandler
{
private $redisClient;
private $redisKey;
# redis instance, key to use
public function __construct($redis = null, $key, $level = Logger::DEBUG, $bubble = true)
{
$this->redisClient = (new Database(app()['config']['database.redis']))->connection('logs');
$this->redisKey = $key;
parent::__construct($level, $bubble);
}
public function handle(array $record)
{
if (!$this->isHandling($record)) {
return false;
}
$record = $this->processRecord($record);
$record['server'] = gethostname();
$record['date'] = $record['datetime']->format(DateTime::ISO8601);
$record['formatted'] = $this->getFormatter()->format($record);
$this->write($record);
return false === $this->bubble;
}
protected function write(array $record)
{
$id = $this->redisClient->rpush($this->redisKey.':'.$record['level'], $record["formatted"]);
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter()
{
return new JsonFormatter();
}
}
<?php namespace App\Core\Logs;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
/**
* RedisRepository
*
* @property redis
* @package App\Core\Logs
*/
class RedisRepository
{
protected $redis;
protected $keys = [];
protected $servers = [];
protected $delimiter;
/**
* RedisRepository constructor.
*/
public function __construct($redis = 'logs', $delimiter = ':')
{
$this->redis = Redis::connection($redis);
$this->delimiter = $delimiter;
}
public function getKeys($pattern = 'logs:*')
{
if (is_null($pattern)) {
$pattern = 'logs:*';
}
if (empty($this->keys)) {
if (strpos($pattern, '-') !== false) {
}
$this->keys = $this->redis->keys($pattern);
foreach ($this->keys as $key) {
$tokens = explode($this->delimiter, $key);
$this->servers[$tokens[1]] = 1;
}
$this->servers = array_keys($this->servers);
}
return $this->keys;
}
public function get($key, $start = 0, $end = 25, $level = '*')
{
$parsedLogs = [];
$it = null;
if (strpos($key, '-') !== false) {
$dates = explode(':', $key);
$prefix = $dates[0].':'.$dates[1].':';
$dates = $dates[2];
list($startDate, $endDate) = explode('-', $dates);
$date = Carbon::createFromFormat('Y.m.d', $startDate);
$lastDate = Carbon::createFromFormat('Y.m.d', $endDate);
$days = $date->diffInDays($lastDate);
$newKeys = [];
for ($i = 0; $i <= $days; $i++) {
$arrKeys = $this->redis->scan(0, 'match', $prefix.$date->format('Y.m.d').':'.$level);
foreach ($arrKeys[1] as $key) {
$newKeys[] = $key;
}
$date->addDay();
}
$arrKeys = $newKeys;
} else {
$arrKeys = $this->redis->scan(0, 'match', $key.':'.$level);
}
$parsedLogs = $this->parseLogs($arrKeys, $parsedLogs);
return $parsedLogs;
}
public function parseLogs($keys = [], $parsedLogs = [])
{
if (isset($keys[1]) && is_array($keys[1])) {
return $this->parseLogs($keys[1], $parsedLogs);
} else {
foreach ($keys as $key) {
$logs = $this->redis->lrange($key, 0, 1000);
foreach ($logs as $log) {
$log = json_decode($log);
$parsedLogs[] = $log;
}
}
return $parsedLogs;
}
}
public function sort($ary, $asc = true)
{
if ($asc) {
return array_values($ary);
} else {
return array_reverse($ary);
}
}
public function count($key)
{
$this->redis->llen($key);
}
public function getServers($pattern = 'logs:*')
{
if (empty($this->servers)) {
$this->getKeys($pattern);
}
return $this->servers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment