Symfony 2 - 登录服务
Symfony 2 - logging in a service
symfony 新手,所以请指出任何显而易见的地方:)
我有一个发送推送通知的服务。我正在尝试将日志记录对象传递给该服务,以便我可以写入主日志处理程序。
底线是这不会写在任何地方,我也不知道我哪里出错了。
我从我的代码中删除了一些东西,但这通常是我的想法。
blah\CoreBundle\Service\PushTask.php
public function __construct(
\Doctrine\ORM\EntityManager $entityManager,
$logger
) {
$this->entityManager = $entityManager;
$this->logger = $logger;
}
...
public function pushSomething()
{
$this->logger->addInfo('test'); // not writing
}
config_dev.yml
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: info
blah\CoreBundle\Resources\config\services.xml
<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="logger" />
</service>
试试这个:
$this->logger->info('test');
而不是这个:
$this->logger->addInfo('test');
希望这对您有所帮助(尽快获取更多信息)。
您可以按照您的示例使用的完整堆栈:
blah\CoreBundle\Service\PushTask.php
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
\Doctrine\ORM\EntityManager $entityManager,
LoggerInterface $logger
) {
$this->entityManager = $entityManager;
$this->logger = $logger;
}
...
public function pushSomething()
{
$this->logger->info('test');
}
config.yml
monolog:
channels:
- your_new_channel
handlers:
// just keep it or add a new handler like:
your_handler:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_new_channel.log"
level: info
channels: ["your_new_channel"]
blah\CoreBundle\Resources\config\services.xml
<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="monolog.logger.your_new_channel" />
</service>
去查看文件:dev_new_channel.log
您也可以在执行此操作后清除缓存,以确保所有 yml/xml 更改都已到位!
为什么这是我的建议?
使用通道和处理程序将帮助您保持日志井井有条并且易于更改!
public function getCustomerAction(Request $request){
Common::Init($this->get('logger'),$request->headers->get('Request-id'));}
class Common {
public static $logger;
public static $request_id;
public static function Init($logger, $request_id) {
self::$logger = $logger;
// self::$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
if (is_null($request_id)) {
self::$request_id = Common::generate_uuid();
}
else{
self::$request_id =$request_id;
}
self::$logger->pushProcessor(function ( $record) {
$record['context']['request-id'] = isset($_SERVER['HTTP_X_REQUEST_ID']) ? $_SERVER['HTTP_X_REQUEST_ID'] : self::$request_id;
dump($record);
return $record;
});
Common::$logger->debug('hey it works');
}
public static function generate_uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}}
symfony 新手,所以请指出任何显而易见的地方:)
我有一个发送推送通知的服务。我正在尝试将日志记录对象传递给该服务,以便我可以写入主日志处理程序。 底线是这不会写在任何地方,我也不知道我哪里出错了。
我从我的代码中删除了一些东西,但这通常是我的想法。
blah\CoreBundle\Service\PushTask.php
public function __construct(
\Doctrine\ORM\EntityManager $entityManager,
$logger
) {
$this->entityManager = $entityManager;
$this->logger = $logger;
}
...
public function pushSomething()
{
$this->logger->addInfo('test'); // not writing
}
config_dev.yml
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: info
blah\CoreBundle\Resources\config\services.xml
<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="logger" />
</service>
试试这个:
$this->logger->info('test');
而不是这个:
$this->logger->addInfo('test');
希望这对您有所帮助(尽快获取更多信息)。
您可以按照您的示例使用的完整堆栈:
blah\CoreBundle\Service\PushTask.php
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
\Doctrine\ORM\EntityManager $entityManager,
LoggerInterface $logger
) {
$this->entityManager = $entityManager;
$this->logger = $logger;
}
...
public function pushSomething()
{
$this->logger->info('test');
}
config.yml
monolog:
channels:
- your_new_channel
handlers:
// just keep it or add a new handler like:
your_handler:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_new_channel.log"
level: info
channels: ["your_new_channel"]
blah\CoreBundle\Resources\config\services.xml
<service id="civix_core.push" class="blah\CoreBundle\Service\PushTask">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="monolog.logger.your_new_channel" />
</service>
去查看文件:dev_new_channel.log
您也可以在执行此操作后清除缓存,以确保所有 yml/xml 更改都已到位!
为什么这是我的建议? 使用通道和处理程序将帮助您保持日志井井有条并且易于更改!
public function getCustomerAction(Request $request){
Common::Init($this->get('logger'),$request->headers->get('Request-id'));}
class Common {
public static $logger;
public static $request_id;
public static function Init($logger, $request_id) {
self::$logger = $logger;
// self::$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));
if (is_null($request_id)) {
self::$request_id = Common::generate_uuid();
}
else{
self::$request_id =$request_id;
}
self::$logger->pushProcessor(function ( $record) {
$record['context']['request-id'] = isset($_SERVER['HTTP_X_REQUEST_ID']) ? $_SERVER['HTTP_X_REQUEST_ID'] : self::$request_id;
dump($record);
return $record;
});
Common::$logger->debug('hey it works');
}
public static function generate_uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}}