2012-04-16 1 views
0

간략하게 사용자 지정 처리기 서비스에서 정의한 로거 서비스를 사용하려고합니다.Symfony2의 AuthenticationHandler 클래스에서 사용자 정의 로거 서비스 사용

내 config.yml

services: 
authentication_handler: 
    class: Panasonic\LoginBundle\Handler\AuthenticationHandler 
    arguments: [ @custom_logger ] 

custom_logger: 
    class: Monolog\Logger 
    arguments: [login] 
    calls: 
     - [ pushHandler, [ @custom_logger_stream ]] 

custom_logger_stream: 
    class: Monolog\Handler\RotatingFileHandler 
    arguments: [ %kernel.logs_dir%/test.log, 30, 200 ] 

은 "서비스"내 코드, DNT 걱정 잘 들여이다.

내 번들/핸들러/AuthenticationHandler를

namespace Panasonic\LoginBundle\Handler; 

use Monolog\Logger; 
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface 
{ 

private $custom_logger; 

public function __constructor(Logger $custom_logger) 
{ 
    $this->custom_logger = $custom_logger; 
} 

public function onAuthenticationSuccess(Request $request, 
             TokenInterface $token) 
{ 
    $log = $this->custom_logger; 

    $log->addWarning('Foo'); 
    $log->addError('Bar'); 
    die; 

    //  var_dump($token); die; 
    //  var_dump($request->getSession()); die; 
} 
} 

나는 점점 오전 :

Fatal error: Call to a member function addWarning() on a non-object in ... Panasonic\LoginBundle\Handler\AuthenticationHandler.php on line 22 

참고 : 나는, 내가 onAuthenticationSuccess에서 응답을 반환해야합니다 알고는 사실 uncomplete에,하지만 작동하는지, var_dumps에서 결과를 얻었습니다.

주사가 잘 안된다고 생각합니다. 어떻게해야합니까? 나는 여기서 무엇을해야 할지를 알 수 없다.

참조 내가 선택 도와주세요 : Access services inside a regular class

http://martinsikora.com/symfony2-and-dependency-injection

+0

나는 또는 custom_logger : class : Monolog \ LoggerSymfony \ Bridge \ Monolog \ Logger에 있으며 내 처리기에 LoggerInterface를 사용해야합니다. 내 코드를 다시 읽음으로써 PéCé가 준 링크로 내 문제를 해결했습니다. – Rishi

답변

1

이 생성자의 이름을 확인 ... 나는 그것이 호출되지 않습니다 추측 ...

public function __construct() 
{ 
    // ... 
} 

내가 작성했습니다 정확하게 달성하는 방법을 설명하는 기사 : http://blog.alterphp.com/2012/04/set-up-specific-log-file-for.html

+0

감사합니다. 내 블로그 게시물은 실수를 찾아 내는데 도움이되었습니다. 깨끗한 코드로 좋은 예. – Rishi

관련 문제