我正在symfony 4.0下构建一个电子商务应用程序。 该应用程序变得越来越复杂,我想记录该过程。 我想将其记录在特定的日志文件中。
为此,我创建了一个新的独白处理程序: monolog.yaml:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
youshoes:
level: info
type: stream
path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
channels: [youshoes]
channels: ["main", "youshoes"]
我现在如何使用此日志。例如:
$logger->info('Client has validated is cart');
$logger->info('Payment is successful');
$logger->info('Client is requesting for deliveries');
etc ....
在正确的日志文件中。
非常感谢您的帮助。
皮埃尔。
答案 0 :(得分:0)
我找到了解决方法...
monolog.yaml
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event", "!youshoes"]
youshoes:
level: info
type: stream
path: "%kernel.logs_dir%/%kernel.environment%YOUSHOES.log"
channels: ["youshoes"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
channels: ["youshoes"]
services.yaml
...
# saw in https://symfony.com/doc/current/service_container.html#services-wire-specific-service
App\Service\YoushoesLog:
arguments:
# the '@' symbol is important: that's what tells the container
# you want to pass the *service* whose id is 'monolog.logger.request',
# and not just the *string* 'monolog.logger.request'
$logger: '@monolog.logger.youshoes'
Service / YoushoesLog.php
<?php
namespace App\Service;
use Psr\Log\LoggerInterface;
class YoushoesLog
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function info($message)
{
if ($this->logger->info($message)) return true;
return false;
}
}
任何控制器:
use App\Service\YoushoesLog;
class TestController extends Controller{
public function test(YoushoesLog $logger){
$logger->info("Client has sent order");
}
}