2016-03-30 76 views
1

記錄與獨白錯誤時,包括一個完整的堆棧跟蹤我有一個應用程序的調試啓用,轉化爲異常和獨白記錄器服務錯誤:在Silex的

ErrorHandler::register(); 
ExceptionHandler::register(); 

$app = new Silex\Application(); 

$logfile = dirname(__DIR__) . '/var/log/application.log'; 
$app->register(new Silex\Provider\MonologServiceProvider(), array(
    'monolog.logfile' => $logfile, 
    'monolog.level' => Logger::DEBUG,  
)); 

我想錯誤記錄到詳細的文件如在瀏覽器上。

例子:我訪問一個不存在的URL,我得到瀏覽器接下來的兩個錯誤:

2/2 
Not NotFoundHttpException in RouterListener.php line 159: 
No route found for "GET /xxx" 

1. in RouterListener.php line 159 
2. at RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) 
3. at call_user_func(array(object(RouterListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) in EventDispatcher.php line 164 
... 

1/2 
ResourceNotFoundException in UrlMatcher.php line 102: 
No routes found for "/xxx". 

1. in UrlMatcher.php line 102 
2. at UrlMatcher->match('/xxx') in RedirectableUrlMatcher.php line 30 
3. at RedirectableUrlMatcher->match('/xxx') in LazyUrlMatcher.php line 51 
... 

但是,登錄唯一的文字是:

[2016-03-30 21:32:10] myapp.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /xxx" (uncaught exception) at ...route.../vendor/symfony/http-kernel/EventListener/RouterListener.php line 159 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /xxx\" at ...route.../vendor/symfony/http-kernel/EventListener/RouterListener.php:159, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): No routes found for \"/xxx\". at ...route.../vendor/symfony/routing/Matcher/UrlMatcher.php:102)"} [] 
[2016-03-30 21:32:10] myapp.INFO: < 404 [] [] 

有什麼辦法記錄拋出的所有錯誤,幷包括完整的堆棧跟蹤?

+0

不包含在日誌中的完整的堆棧跟蹤?有一個跟蹤權? – Tieme

回答

1

你可以寫日誌格式,將格式化錯誤

配置獨白:

$logfile = dirname(__DIR__) . '/var/log/application.log'; 
$app->register(new Silex\Provider\MonologServiceProvider(), array(
    'monolog.logfile' => $logfile, 
    'monolog.level' => Logger::DEBUG,  
)); 

$app->extend('monolog', function($monolog, $app) { 
    static $initialized = false; 
    if ($initialized) return $monolog; 
    $initialized = true; 

    foreach ($monolog->getHandlers() as $handler) 
     if ($handler->getFormatter() instanceof \Monolog\Formatter\LineFormatter) 
      $handler->setFormatter(new \My\Formatter()); 

    return $monolog; 
}); 

格式化:

class \My\Formatter extends \Monolog\Formatter\NormalizerFormatter 
{ 
    public function format(array $record) 
    { 
     $output = ...; 
     return $output 
    } 

    public function formatBatch(array $records) 
    { 
     $output = ...; 
     return $output 
    } 
}