Merge pull request #86 from diolektor/add-logger

#77 Add monolog
This commit is contained in:
Yuriy Pikhtarev 2016-03-13 13:41:51 +03:00
commit 5c52c51e3e
6 changed files with 207 additions and 1 deletions

View file

@ -42,6 +42,10 @@ $di->register(new \TorrentPier\ServiceProviders\ConfigServiceProvider, [
'config.file.local.main' => __DIR__ . '/library/config.local.php',
]);
$di->register(new \TorrentPier\ServiceProviders\LogServiceProvider(), [
'config.log.handlers' => $di->config->log->handlers
]);
$di->register(new \TorrentPier\ServiceProviders\DbServiceProvider, [
'config.db' => $di->config->db->toArray()
]);

View file

@ -41,7 +41,8 @@
"zendframework/zend-loader": "^2.5",
"zendframework/zend-mail": "^2.5",
"zendframework/zend-session": "^2.5",
"zendframework/zend-version": "^2.5"
"zendframework/zend-version": "^2.5",
"monolog/monolog": "^1.18"
},
"autoload": {
"psr-4": {

View file

@ -62,6 +62,18 @@ $config = [
]
],
// Log
'log' => [
'handlers' => [
function () {
return new \Monolog\Handler\StreamHandler(
__DIR__.'/../internal_data/log/app.log',
\Monolog\Logger::DEBUG
);
}
]
],
// Aliases
// TODO: удалить
'db_alias' => [

147
src/Log.php Normal file
View file

@ -0,0 +1,147 @@
<?php
namespace TorrentPier;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
/**
* Class Log
* @package TorrentPier
*/
class Log
{
/**
* @var Logger
*/
private static $logger;
/**
* @param LoggerInterface $logger
*/
public static function setLogger(LoggerInterface $logger)
{
static::$logger = $logger;
}
/**
* @return Logger
*/
public static function getLogger()
{
if (!static::$logger) {
static::setLogger(Di::getInstance()->log);
}
return static::$logger;
}
/**
* Adds a log record at an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return bool
*/
public static function log($level, $message, array $context = array())
{
return static::getLogger()->log($level, $message, $context);
}
/**
* Adds a log record at the DEBUG level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function debug($message, array $context = array())
{
return static::getLogger()->debug($message, $context);
}
/**
* Adds a log record at the INFO level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function info($message, array $context = array())
{
return static::getLogger()->info($message, $context);
}
/**
* Adds a log record at the NOTICE level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function notice($message, array $context = array())
{
return static::getLogger()->notice($message, $context);
}
/**
* Adds a log record at the WARNING level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function warning($message, array $context = array())
{
return static::getLogger()->warning($message, $context);
}
/**
* Adds a log record at the ERROR level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function error($message, array $context = array())
{
return static::getLogger()->error($message, $context);
}
/**
* Adds a log record at the CRITICAL level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function critical($message, array $context = array())
{
return static::getLogger()->critical($message, $context);
}
/**
* Adds a log record at the ALERT level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function alert($message, array $context = array())
{
return static::getLogger()->alert($message, $context);
}
/**
* Adds a log record at the EMERGENCY level.
*
* @param string $message
* @param array $context
* @return bool
*/
public static function emergency($message, array $context = array())
{
return static::getLogger()->emergency($message, $context);
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace TorrentPier\ServiceProviders;
use Monolog\Logger;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* Class LogServiceProvider
* @package TorrentPier\ServiceProviders
*/
class LogServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
*/
public function register(Container $container)
{
$container['logger'] = function ($container) {
return function ($name) {
return new Logger(strtoupper($name));
};
};
$container['log'] = function ($container) {
/** @var Logger $logger */
$logger = $container['logger']('app');
foreach ($container['config.log.handlers'] as $logHandler) {
$logger->pushHandler(call_user_func($logHandler));
}
return $logger;
};
}
}

View file

@ -35,6 +35,12 @@ $db = $di->db;
// });
//});
///** @var \Monolog\Logger $log */
//$log = $di->log;
//$log->debug('test debug information');
\TorrentPier\Log::info('You will see the style guide');
$query = function(\Zend\Db\Sql\Select $select) {
$select->columns(['id' => 'user_id', 'name' => 'username']);
$select->join(['t' => BB_TOPICS], 't.topic_poster = u.user_id', ['title' => 'topic_title']);