silpion/logger-extra-bundle

在 Symfony2 包中为 Monolog 提供额外的日志功能。

安装次数: 3,317

依赖者: 0

建议者: 0

安全性: 0

星标: 4

关注者: 5

分支: 0

公开问题: 4

类型:symfony-bundle

0.1.0-beta1 2014-10-02 08:38 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:18:44 UTC


README

Build Status Latest Stable Version License Scrutinizer Quality Score Code Coverage HHVM Status Dependency Status Project Status

Symfony2 的日志相关功能包。此包可以提供以下功能:

  • 为当前请求的每条消息添加一个唯一的 RequestId。
  • 根据当前会话 ID 为每条消息添加一个唯一的 SessionId。
  • 为当前请求的每条消息添加任意的 "键:值" 对。
  • 在主请求上创建日志条目。
  • 在响应上创建日志条目。

还有一个用于日志请求和响应的 Stack Middleware:silpion/stack-logger

安装

使用 Composer

php composer.phar require silpion/logger-extra-bundle

还将 SilpionLoggerExtraBundle 添加到您的 AppKernel 中

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Silpion\LoggerExtraBundle\SilpionLoggerExtraBundle(),
        );
        ...
    }
}

配置

默认情况下,此包不会执行任何操作!每个功能都必须单独启用。

示例配置

silpion_logger_extra:

    # If a random request_id should be added to the [extra] section of each log message.
    request_id:           true

    # Class of the used request_id provider.
    request_id_provider:  Silpion\LoggerExtraBundle\Logger\Provider\Request\UniqRequestIdProvider

    # If a salted SHA1 of the session_id should be added to the [extra] section of each log message.
    session_id:           true

    # Class of the used session_id provider.
    session_id_provider:  Silpion\LoggerExtraBundle\Logger\Provider\Session\SymfonySessionIdProvider

    # If the current PID of the PHP Interpreter should be added to the [extra] section of each log message.
    process_id:           true

    # If the session should be started, so the session_id will always be available.
    session_start:        false

    # A list of "key: value" entries that will be set in the [extra] section of each log message (Overwrites existing keys!).
    additions:
        server_id: 42

    logger:

        # Will create a log entry on each incoming request.
        on_request:           true

        # Will create a log entry on each outgoing response.
        on_response:          true

可用提供者

RequestIdProvider

除了将生成简单 sha1 哈希的 UniqRequestIdProvider 之外,还有一个 EnrichedRequestIdProvider,它将生成类似于 MongoDb ObjectId 的哈希,包含当前时间戳、machineId 和 processId。该 EnrichedRequestIdProvider 将生成 request_id,这将按创建时间排序。

使用方法

如果您想在应用程序的某个地方使用当前 RequestId 或 SessionId,请参阅此代码

$requestId = $this->get('silpion_logger_extra')->getRequestId();
$sessionId = $this->get('silpion_logger_extra')->getSessionId();

参考

有趣的是,在 Symfony2 烹饪书 中有一个添加 RequestId 的示例,但直到现在还没有在包中提供。