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>
您可以使用以下示例的完整堆栈:
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');
}
配置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更改都已到位!
为什么这是我的建议?使用通道和处理程序将帮助您保持日志的有序性,并易于更改!
试试这个:
$this->logger->info('test');
而不是这个:
$this->logger->addInfo('test');
希望得到帮助(尽快提供更多信息)。
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)
);
}}