stange/slog

一个简单的PHP日志类

1.0.1 2017-03-11 04:03 UTC

This package is auto-updated.

Last update: 2024-09-20 03:14:37 UTC


README

一个简单的PHP日志类

前言

Slog是一个简单的PHP日志类,您可以轻松将其包含在任何项目中。

安装Slog

您可以从GIT克隆最新版本

git clone https://github.com/pthreat/slog.git

或者通过composer将Slog添加到您的项目中

composer require stange/slog

特性

  • ANSI输出着色
  • 日志到文件
  • 日志标签(见下文)
  • 添加或附加日志字符串
  • 使用特定日期格式作为日志消息的前缀

示例

基本示例

use \stange\logging\Slog;

$log	=	new Slog();
$log->log('Scotty, beam me up!');

使用特定日志类型和ANSI着色功能

Slog包含不同的方法来通知不同类型的日志消息。默认情况下,SLog会着色您的日志输出

use \stange\logging\Slog;

$log	=	new Slog();

$log->log('test message');
$log->debug('Debug');
$log->info('Info');
$log->warning('Warning');
$log->error('Error');
$log->emergency('Emergency');
$log->success('Success');

结果

alt tag

当然,您可以禁用输出着色

use \stange\logging\Slog;

$log	=	new Slog([
                   'colors' => FALSE
]);

//You can disable colors at runtime too

//$log->useColors(FALSE);

$log->debug('Debug');

日志到文件

use \stange\logging\Slog;

$log	=	new Slog([
                   'file'=>'out.log'
]);

$log->log('Log to a file (and to stdout)');

注意: ANSI颜色将不会记录到文件中

仅文件输出示例

如果您不想输出到stdout,可以在构造函数中传递 'echo'=>FALSE,或者使用 Slog::setEcho(FALSE) 禁用stdout输出。

use \stange\logging\Slog;

$log	=	new Slog([
                   'file'   => 'out.log',
                   'echo'   => FALSE

]);

$log->log("No stdout log, file only");

日志标记

日志是件好事,但有时日志信息的数量可能会很繁琐。

** Slog的一个酷特性是您可以标记您的日志。 **

标记日志?这是什么意思?

我的意思是您可以在日志消息中使用某些“标签”来记录特定类型的日志消息,而不是记录其他类型的日志消息。

以下是一个两个标记日志消息的简要示例

use \stange\logging\Slog;

$log	=	new Slog([
                   'tagId'=>'@@@'
]);

$log->log('tagOne@@@Hello! this is a tagged message with the tag tagOne');
$log->log('tagTwo@@@This is another tagged message with the tagTwo');

在上面的示例中,两个消息都将被记录并通过stdout显示。

然而,现在,如果我们给slog添加一个日志标签,情况会有所不同。

只有与标签“tagOne”匹配的消息将被记录。包含标签“tagTwo”的消息将被丢弃,即:不会被记录。

$log	=	new Slog([
                       'tagId' => '@@@',   //Set the tag identifier to @@@
                       'tags'  => 'tagOne' //Log messages only from tagOne 
]);

$log->log('tagOne@@@Hello! this is a tagged message with the tag tagOne');

//The next message will not be logged since we specified that only messages
//containing the tag "tagOne" will be logged.

$log->log('tagTwo@@@This is another tagged message with the tagTwo');

如果您需要,您可以记录多个标签,这当您想增加日志详细程度时是一个好主意

$log	=	new Slog([
                       'tagId' => '@@@',              //Set the tag identifier to @@@
                       'tags'  => ['tagOne','tagTwo'] //Log messages from tagOne AND tagTwo
]);

$log->log('tagOne@@@Hello! this is a tagged message with the tag tagOne');
$log->log('tagTwo@@@This is another tagged message with the tagTwo');

在运行时添加或删除日志标签

您可以通过以下方法在任何给定时间添加或删除标签

  • Slog::removeTag($tag) 删除一个标签
  • Slog::addTag($tag) 指定应记录包含标签的消息
  • Slog::unsetTags() 记录所有内容

可记录特性和可记录接口

作为额外功能,我包含了一个简单的日志特性,您可以使用此特性在您自己的类中使用,使该类成为“可记录的”,此特性包含三个主要方法

  • Loggable::setLog(LogInterface $log)
  • Loggable::getLog()
  • Loggable::log($message,$type=NULL)

注意: 特性还会将 CLASS 名称添加到日志中,这样,如果您有多个使用可记录特性的类,您可以识别哪些日志来自一个类,哪些日志来自另一个类

特性和可记录接口实现示例

namespace myProject{

	/**
	 * Add the loggable interface to indicate to other class methods 
	 * that this class has logging capabilities.
	 */

	use \stange\logging\slog\iface\Loggable	as	LoggableInterface;

	class MyClass implements LoggableInterface{

		//Make the class "Loggable" by using the loggable trait
		use \stange\logging\slog\traits\Loggable;

		public function doStuff(){

			$this->log('stuff@@@Doing stuff!');

		}

		public function doOtherStuff(){

			$this->log('otherStuff@@@Doing other stuff!','info');

		}

	}

	class MyOtherClass implements LoggableInterface{

		use \stange\logging\slog\traits\Loggable;

		public function doSomething(){

			$this->log('Doing something!','success');

		}

	}

}

使用前面的代码,我们可以轻松实现以下功能

	namespace myProject;

	use \stange\logging\Slog();

	$log     =  new Slog([
                              'tagId'	=>	'@@@',
										'tags'	=>	'stuff'
	]);

	$myClass      = new MyClass();
	$myOtherClass = new MyOtherClass();

	$myClass->setLog($log);
	$myOtherClass->setLog($log);

	$myClass->doStuff();
	$myClass->doingOtherStuff();
	$myOtherClass->doSomething();

待办事项(下一版本)

  • 创建用于在不同位置记录日志消息的适配器模式
  • 添加日志适配器(Socket、文件、内存等)