tintonic/php-elog

简单的PHP类,用于增强错误记录

v1.0.4 2022-01-21 18:28 UTC

This package is auto-updated.

Last update: 2024-09-27 15:40:55 UTC


README

简单的PHP增强日志解决方案。

安装

composer require tintonic/php-elog

使用

简单使用 – 创建实例并通过辅助函数进行日志记录

此示例创建一个将日志记录到当前执行脚本同一目录下的elog.log文件的Elog实例。辅助函数elog()将日志记录到第一个创建的Elog实例。

create_elog();

/*
Logs to the first instance that was created.
Logs to "__DIR__/elog.log"
*/
elog("I am elog.log");

记录不同数据类型

  • null 显示为 [null]
  • 空字符串 显示为 [empty string]
  • true 显示为 [true]
  • false 显示为 [false]
  • 对象数组 使用 print_r() 描述
create_elog();

elog(null);  // [null]

elog('');    // [empty string]

elog(true);  // [true]

elog(false); // [false]


elog((object) [
    'id' => 123,
    'foo' => 'bar'
]);

/*
stdClass Object
(
    [id] => 123
    [foo] => bar
)
*/


elog([
    'id' => 123,
    'foo' => 'bar'
]);

/*
Array
(
    [id] => 123
    [foo] => bar
)
*/

包含标签和/或数据类型

elog(123, 'Current value', true);

/*
--- Current value {integer} ---
123
*/

命名实例

此示例创建了两个命名 Elog 实例 – 一个将日志记录到 _DIR__/first.log,另一个将日志记录到 /path/to/log/second_log_file(不带文件扩展名)。

create_elog(__DIR__, 'first');
create_elog('/path/to/log', 'second', 'second_log_file', null);

// Log to named instances using elogn().
elogn('first', "I am first.log");         //  ———>  __DIR__/first.log
elogn('second', "I am second_log_file");  //  ———>  /path/to/log/second_log_file

使用 Elog 类

当然,您可以直接使用 Elog 类。

此示例还演示了如何为 $include_type 配置默认值,以始终在输出中包含数据类型。

use Tintonic/PhpElog/Elog;

/*
Create an instance named "notes" that logs to "/path/to/log/elog.txt" with data type by default.
*/
$logger = new Elog('/path/to/log', 'notes', 'elog', 'txt');
$logger->set_default_include_type(true);

/*
Log to named instance somewhere else in the application.
*/
Elog::logn('notes', 123);

/*
{integer} 
123
*/


Elog::logn('notes' 'bar', 'Foo?');

/*
--- Foo? {string} ---
bar
*/