public function testRedisHandle() { $redis = $this->getMock('Redis', array('rpush')); // Redis uses rPush $redis->expects($this->once())->method('rPush')->with('key', 'test'); $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass(), 'foo' => 34)); $handler = new RedisHandler($redis, 'key'); $handler->setFormatter(new LineFormatter("%message%")); $handler->handle($record); }
public function testPredisHandleCapped() { $redis = $this->getMock('Predis\\Client', array('transaction')); $redisTransaction = $this->getMock('Predis\\Client', array('rpush', 'ltrim')); $redisTransaction->expects($this->once())->method('rpush')->will($this->returnSelf()); $redisTransaction->expects($this->once())->method('ltrim')->will($this->returnSelf()); // Redis uses multi $redis->expects($this->once())->method('transaction')->will($this->returnCallback(function ($cb) use($redisTransaction) { $cb($redisTransaction); })); $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass(), 'foo' => 34)); $handler = new RedisHandler($redis, 'key', Logger::DEBUG, true, 10); $handler->setFormatter(new LineFormatter("%message%")); $handler->handle($record); }
/** * Register the service provider. * * @return void */ public function register() { $this->package('tieday/laravel-logstash', 'laravel-logstash'); $cacheStore = $this->app->make('cache.store')->getStore(); if ($cacheStore instanceof \Illuminate\Cache\RedisStore) { $redisClient = $this->app->make('cache.store')->getStore()->connection(); $redisHandler = new RedisHandler($redisClient, Config::get('laravel-logstash::redis_key')); $formatter = new LogstashFormatter(Config::get('laravel-logstash::application_name')); $redisHandler->setFormatter($formatter); $logger = new Writer(new Logger($this->app['env'], [$redisHandler])); } else { $logger = new Writer(new Logger($this->app['env']), $this->app['events']); } $this->app->instance('log', $logger); // If the setup Closure has been bound in the container, we will resolve it // and pass in the logger instance. This allows this to defer all of the // logger class setup until the last possible second, improving speed. if (isset($this->app['log.setup'])) { call_user_func($this->app['log.setup'], $logger); } }
protected static function _writeTo($stringData) { if (!file_exists(INIT::$LOG_REPOSITORY) || !is_dir(INIT::$LOG_REPOSITORY)) { mkdir(INIT::$LOG_REPOSITORY); } if (!empty(self::$fileName)) { self::$fileNamePath = LOG_REPOSITORY . "/" . self::$fileName; } else { self::$fileNamePath = LOG_REPOSITORY . "/" . LOG_FILENAME; } if (self::$useMonolog) { try { if (empty(self::$logger)) { $matecatRedisHandler = new \RedisHandler(); // Init a RedisHandler with a LogstashFormatter. // The parameters may differ depending on your configuration of Redis. // Important: The parameter 'logs' must be equal to the key you defined // in your logstash configuration. $redisHandler = new RedisHandler($matecatRedisHandler->getConnection(), 'phplogs'); $logStashFormatter = new LogstashFormatter('MateCat', gethostname()); $redisHandler->setFormatter($logStashFormatter); //Log on file $fileHandler = new StreamHandler(self::$fileNamePath); $fileFormatter = new LineFormatter(null, null, true, true); $fileHandler->setFormatter($fileFormatter); // Create a Logger instance with the RedisHandler self::$logger = new Logger('MateCat', array($redisHandler, $fileHandler)); } self::$logger->debug(rtrim($stringData)); } catch (Exception $e) { file_put_contents(self::$fileNamePath, $stringData, FILE_APPEND); } } else { file_put_contents(self::$fileNamePath, $stringData, FILE_APPEND); } }
/** * @param Redis $redis * @param string $appName * @param string $key * @param int $level * @param bool $bubble */ public function __construct($redis, $appName, $key = 'logging', $level = Logger::DEBUG, $bubble = true) { $this->appName = $appName; parent::__construct($redis->redis, $key, $level, $bubble); }
protected function write(array $record) { parent::write($this->format($record)); }
/** * get redis handler * * @param $stream * @return RedisHandler */ protected static function getRedisHandler($stream) { $stream_info = parse_url($stream); $redis = new \Redis(); $connect_result = $redis->connect($stream_info['host'], $stream_info['port']); if ($connect_result === false) { throw new \RuntimeException("connect to redis failed"); } $handler = new RedisHandler($redis, substr($stream_info['path'], 1)); $handler->setFormatter(new MessageFormatter()); return $handler; }
} class eLifeJsonFormatter extends Monolog\Formatter\JsonFormatter { public function format(array $record) { return parent::format(el_format_record($record)); } public function formatBatch(array $records) { $records = array_map('el_format_record', $records); return parent::formatBatch($records); } } $log = new Logger('example-app'); # send a wrangled JSON record to a file $formatter = new eLifeJsonFormatter(); $stream = new StreamHandler('/tmp/monolog-test.log', Logger::DEBUG); $stream->setFormatter($formatter); $log->pushHandler($stream); # send regular JSON record to Redis $client = new Predis\Client(); $queue_name = "log_messages"; $redis = new RedisHandler($client, $queue_name); $redis->setFormatter(new Monolog\Formatter\JsonFormatter()); $log->pushHandler($redis); # # log stuff # $log->addDebug('this is a debug'); $log->addWarning('this is a warning'); $log->addError('this is an error');
function rlog($name = null) { static $logs = []; $name = is_null($name) ? SITE_NAME . '.log' : SITE_NAME . '.' . $name . '.log'; $log = isAke($logs, $name, false); if (false === $log) { $log = new Logger('name'); $handler = new RH(redis(), $name); $handler->setFormatter(new LF("%message%")); $log->pushHandler($handler); } return $log; }