定制 faslatam/log 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

faslatam/log

最新稳定版本:v2.0.0

Composer 安装命令:

composer require faslatam/log

包简介

Small log file writer with adjustable log level settings.

README 文档

README

Latest Version Software License Total Downloads

Small log file writer with adjustable log level threshold settings.

Install

Via Composer

$ composer require faslatam/log

Usage

Creating a logger

$logger = new Forestry\Log\Log('/tmp/dummy.log');
//Just log notices and above.
$errorLog = new Forestry\Log\Log('./logs/error.log', Psr\Log\LogLevel::NOTICE);

Using a factory

Another way to create an instance is the use of one of the factories. There is one for each threshold level.

Here is an example for a logger with an error threshold:

$factory = new ErrorLogger();
$logger = $factory->create('/tmp/error.log');

Log a message

Forestry\Log provides methods for the log levels defined by RFC 5424 (debug, info, notice, warning, error, critical, alert and emergency). There's a method for each of these levels:

$logger->emergency('This is an emergency message');
$logger->alert('This is an alert message');
$logger->critical('This is an critical message');
$logger->error('This is an error message');
$logger->warning('This is an warning message');
$logger->notice('This is a notice message');
$logger->info('This is an information');
$logger->debug('This is a debug message');

You can also use a generic log method:

$logger->log(Psr\Log\LogLevel::DEBUG, 'this is a debug message');

Using placeholders in log messages

You can use placeholders in the your message string and fill them using the associative context array. The array keys have to match the placeholders without the curly brackets:

$user = ['name' => 'John Doe', 'mail' => 'j.doe@example.org'];
$logger->info('Send mail to {name} ({mail})', $user); // Send mail to John Doe (j.doe@example.org)

Change the date format

The default date format is Y-m-d H:i:s. You can change it by using the setDateFormat method:

$logger->setDateFormat('r'); // e.g. Thu, 21 Dec 2000 16:01:07 +0200

This method accepts any string which is compatible with PHPs native date().

Change the message format

The default format for the log message is date level message. To change it, you can re-arrange the placeholders with setLogFormat:

$logger->setLogFormat('[{level}|{date}] {message}'); // [INF0|2013-04-25 13:37:42] This is an info message

There are the following placeholder available:

  • {date}
  • {level}
  • {message}

These placeholders will be replaced with ones for sprintf(), so you can also use the following:

  • %1$s = date
  • %2$s = level
  • %3$s = message

Change the threshold level of an existing instance

To change the threshold level of an existing instance, use the setLogThreshold method:

$logger->setLogThreshold(Psr\Log\LogLevel::DEBUG);

Get current logging level

To get the current threshold level of an existing instance, use the getLogThreshold method:

$level = $logger->getLogThreshold();
$logger->setLogThreshold(Psr\Log\LogLevel::INFO);
$logger->logInfo('my info');
$logger->setLogThreshold($level);

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-09