bigpoint/monolog-creator

这些类提供了创建预配置的monolog日志对象工厂。

2.0.2 2023-12-18 10:04 UTC

This package is auto-updated.

Last update: 2024-09-19 04:21:17 UTC


README

这些类提供了一个简单的工厂来创建预配置的monolog日志对象。

Monolog-Creator目前仅支持少数几个monolog处理器、格式化和处理器。因此,请随意扩展库。

查看变更日志了解主版本之间的更新。

安装

composer require bigpoint/monolog-creator

示例

最小化

您至少需要配置_default日志和一个处理器。

index.php

<?php
require 'vendor/autoload.php';

$config = [
    "handler" => [
        "stream" => [
            "path" => "php://stdout",
        ],
    ],
    "logger" => [
        "_default" => [
            "handler" => ["stream"],
            "level"   => "WARNING",
        ],
    ],
];

$loggerFactory = new \MonologCreator\Factory($config);

$logger = $loggerFactory->createLogger('name');
$logger->addWarning('I am a warning');
?>

不同的日志

您还可以创建不同的预配置日志。例如,使用另一个日志级别或处理器。

index.php

<?php
require 'vendor/autoload.php';

$config = [
    "handler" => [
        "stream" => [
            "path" => "php://stdout",
        ],
    ],
    "logger" => [
        "_default" => [
            "handler" => ["stream"],
            "level"   => "WARNING",
        ],
        "test" => [
            "handler" => ["stream"],
            "level"   => "DEBUG",
        ],
    ],
];

$loggerFactory = new \MonologCreator\Factory($config);

$logger = $loggerFactory->createLogger('test');
$logger->addDebug('I am a debug message');
?>

不同的格式化器

您可以使用格式化器配置日志输出。请注意,格式化器在formatter键下有一个通用配置,并且必须分配到handler部分中的特定处理器。

$config = [
    "handler" => [
        "stream" => [
            "path"      => "php://stdout",
            "formatter" => "json",
        ],
    ],
    'formatter' => [
        'json'     => [],
    ],
    "logger" => [
        "_default" => [
            "handler" => ["stream"],
            "level"   => "WARNING",
        ],
        "test" => [
            "handler" => ["stream"],
            "level"   => "DEBUG",
        ],
    ],
];

可选的处理器

您可以选择性地将处理器添加到您的日志器中

$config = [
    "logger" => [
        "test" => [
            "handler" => ["stream"],
            "processors" : ["web"],
            "level"   => "DEBUG",
        ],
    ],
];

支持的处理器

StreamHandler

$config = [
    "handler" => [
        "stream" => [
            "path"      => "php://stdout",
        ],
    ],
];

UdpHandler(自定义处理器)

$config = [
    "handler" => [
        'udp' => [
            'host'      => 'localhost',
            'port'      => '9999',
        ],
    ],
];

RedisHandler(与predis/predis

要使用Redis处理器,您必须自己创建Predis客户端对象并将其设置为Factory,然后才能创建任何日志。

$config = [
    "handler" => [
        'redis' => [
            "key" => "php_logs",
        ]
    ],
];

$predisClient = new \Predis\Client('tcp://192.168.42.43:6379');
$loggerFactory->setPredisClient($predisClient);

支持的格式化器

JsonFormatter

目前此处不支持任何选项。

$config = [
    'formatter' => [
        'json'     => [],
    ],
];

LineFormatter

所有值都是可选的。布尔值includeStacktracesallowInlineLineBreaksignoreEmptyContextAndExtra可以是"true""false"

$config = [
    'formatter' => [
        'line' => [
            "format"                     => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
            "dateFormat"                 => "Y-m-d H:i:s",
            "includeStacktraces"         => "true",
            "allowInlineLineBreaks"      => "true",
            "ignoreEmptyContextAndExtra" => "true",
        ],
    ],
];

LogstashFormatter

$config = [
    'formatter' => [
        'logstash'     => [
            "type" => "test-app"
        ],
    ],
];

支持的处理器

WebProcessors

$config = [
    'logger' => [
        '_default' => [
            'handler'   => [
                'stream',
            ],
            "processors"  => ["web"],
            'level' => 'DEBUG',
        ],
    ]
];

RequestID处理器

为每个请求注入一个随机UUID,以便于跟踪来自同一请求的多个日志消息。

$config = [
    'logger' => [
        '_default' => [
            'handler'   => [
                'stream',
            ],
            "processors"  => ["requestId"],
            'level' => 'DEBUG',
        ],
    ]
];

ExtraFieldProcessor

允许您在除context列表之外的日志数据中添加高级或特定字段。这些附加字段将出现在输出中的extra列表中。

$config = [
    'logger' => [
        '_default' => [
            'handler'   => [
                'stream',
            ],
            "processors"  => ["extraFields"],
            "extraFields" : [
                "extra_key1" : "extra_value1",
                "extra_key2" : "extra_value2"
            ],
            'level' => 'DEBUG',
        ],
    ]
];

许可 & 作者

Copyright:: 2015-2021 Bigpoint GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.