productsupcom/flexilog

灵活的PSR-3兼容性记录器

v0.1.2 2017-10-30 09:50 UTC

README

<?php

require_once('vendor/autoload.php');

use Productsup\Flexilog\Logger;
use Productsup\Flexilog\Info;
use Productsup\Flexilog\Handler;

// this is optional
// a Info class allows you to set certain properties that you always
// want to include in your Log output (e.g. Gelf or Shell)
// this is an expanditure to the `$context`.
// You can define specific requiredData for the Info so you can enforce
// certain properties to be available.
$logInfo = new Info\GenericInfo();
$logInfo->setRequiredData(['foo']);
$logInfo->setProperty('foo', 'bar');

// pick a cool handler
$shellHandler = new Handler\ShellHandler('trace', 2);

// set the Handler and the optional $logInfo
$logger = new Logger([$shellHandler], $logInfo);


$logger->notice('Hello World');
$context = array(
    'fullMessage' => 'Blablablabla bla blaaaa blaaaa {foo} blaa',
    'foo' => 'bar',
    //'exception' => new \Exception('wut', 0, new \Exception('Previous')),
    'someArray' => array('yo, sup', 'nm nm', 'a' => array('foo', 'bar' => 'baz')),
    'date' => new \DateTime()
);
$logger->message('default message', $context);
$logger->message('critical message', $context, 'critical');

以上内容将输出到Shell

NOTICE: default message
Full Message: Blablablabla bla blaaaa blaaaa bar blaa
Extra Variables: 
	foo: bar
	someArray: {"0":"yo, sup","1":"nm nm","a":{"0":"foo","bar":"baz"}}
	site: 397
	process: somepid
	_date: 2015-07-07T16:39:55+02:00

CRITICAL: critical message
Full Message: Blablablabla bla blaaaa blaaaa bar blaa
Extra Variables: 
	foo: bar
	someArray: {"0":"yo, sup","1":"nm nm","a":{"0":"foo","bar":"baz"}}
	site: 397
	process: somepid
	_date: 2015-07-07T16:39:55+02:00

或到Graylog: http://yourgrayloginstance.com/messages/graylog2_312/23a5e2b0-24b6-11e5-b0b9-001e67b4d4d0(示例可能包含其他样本数据)。

或者PSR-3兼容

$logger->critical('critical message', $context);

查看生成的API文档以获取更多信息

为处理器静音消息

有时您想初始化多个处理器但不向每个处理器发送消息。这可以通过使用静音选项来完成。

// initialise a few Handlers, use the default verbosity set in the Handler
$shellHandler = new Handler\ShellHandler('trace');
// set the Verbosity to -1, which allows it to be muted
$arrayHandler = new Handler\ArrayHandler('debug', -1);

$logger = new Logger([$shellHandler, $arrayHandler]);

// now send a message where $muted is set to true
$logger->log('debug', 'foobar', ['baz'=>'bar'], true);

结果将是ShellHandler将输出消息"foobar",但是ArrayHandler将不会收到该消息。

如果始终要将消息记录到集中式日志系统,但是客户端可以看到的生产环境不需要看到该消息,这将非常有用。

跟踪级别

Flexilog添加了一个比debug更低的级别,即trace级别。为了与PSR\NullLogger保持兼容,您需要按照以下方式调用它

$logger->log('trace', $message, $context);

PSR\NullLogger将简单地忽略它。

Symfony控制台

public function execute(InputInterface $input, OutputInterface $output)
{
    $logger = new \Productsup\Logger(
        array(
            'Console' => new \Productsup\Handler\SymfonyConsoleHandler('debug', 2, $output)
        )
    );

    $logger->message('message');
    $logger->error('errrooorrr');
}

输出

[notice] message
[error] errrooorrr

注意事项

根据使用的Info对象,可能会有一些保留关键字。请检查您使用的Info对象的保留关键字列表。