productsupcom / flexilog
灵活的PSR-3兼容性记录器
v0.1.2
2017-10-30 09:50 UTC
Requires
- php: >=5.6.0
- psr/log: ^1.0
Requires (Dev)
- escapestudios/symfony2-coding-standard: ~2.0
- phpunit/phpunit: ^5.4
- raulfraile/ladybug: ^1.0
- victorjonsson/markdowndocs: dev-master
Suggests
- graylog2/gelf-php: Required for GelfHandler, if you want to write to Graylog
- league/climate: Required for ShellHandler, if you want to write to the Shell using multi-color
- dev-master
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-processor
- dev-v2_future
- dev-untangle2
- dev-gelf_untangle
- dev-contextKeyFix
- dev-autoremove
- dev-saver_filehandler
- dev-slackhandler
- dev-extendable_redishandler2
- dev-extendable_redishandler
- dev-DEV-1380
- dev-patch-1
- dev-ACC-973
- dev-symfony_notice_fix
- dev-symfonyhandler
- dev-synfonyhandler
- dev-phpcs
- dev-loginfo
- dev-loginfo_sol
- dev-werner
- dev-namespace
This package is auto-updated.
Last update: 2024-09-23 21:50:53 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);
为处理器静音消息
有时您想初始化多个处理器但不向每个处理器发送消息。这可以通过使用静音选项来完成。
// 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对象的保留关键字列表。