日志接口

本文档描述了日志类库的通用接口。

主要目标是让类库获得一个Psr\Log\LoggerInterface对象并能通过简单通用的方式来写日志。有自定义需求的框架和CMS可以根据情况扩展这个接口,但推荐保持和该文档的兼容性,以确保应用中使用到的第三方库能将日志集中写到应用日志里。

RFC 2119中的必须(MUST)不可(MUST NOT)建议(SHOULD)不建议(SHOULD NOT)可以/可能(MAY)等关键词将在本节用来做一些解释性的描述。

关键词实现者在这个文档被解释为:在日志相关的库或框架实现LoggerInterface接口的开发人员。用这些实现者开发出来的类库的人都被称作用户

1. 规范

1.1 基础

  • LoggerInterface暴露八个接口用来记录八个等级(debug, info, notice, warning, error, critical, alert, emergency)的日志。

  • 第九个方法是log,接受日志等级作为第一个参数。用一个日志等级常量来调用这个方法必须和直接调用指定等级方法的结果一致。用一个本规范中未定义且不为具体实现所知的日志等级来调用该方法必须抛出一个Psr\Log\InvalidArgumentException不推荐使用自定义的日志等级,除非你非常确定当前类库对其有所支持。

1.2 消息

  • 每个方法都接受一个字符串,或者一个有__toString方法的对象作为message参数。实现者 可以对传入的对象有特殊的处理。如果没有,实现者 必须将它转换成字符串。

  • message参数中可能包含一些可以context参数的数值所替换的占位符。

    占位符名字必须context数组类型参数的键名对应。

    占位符名字必须使用一对花括号来作为分隔符。在占位符和分隔符之间不能有任何空格。

    占位符名字应该只能由A-Za-z0-9,下划线_和句号.组成。其它的字符作为以后占位符规范的保留字。

    实现者 可以使用占位符来实现不同的转义和翻译日志成文。因为用户并不知道上下文数据会是什么,所以不推荐提前转义占位符。

    下面提供一个占位符替换的例子,仅作为参考:

      /*** Interpolates context values into the message placeholders.*/function interpolate($message, array $context = array()){// build a replacement array with braces around the context keys$replace = array();foreach ($context as $key => $val) {$replace['{' . $key . '}'] = $val;}// interpolate replacement values into the message and returnreturn strtr($message, $replace);}// a message with brace-delimited placeholder names$message = "User {username} created";// a context array of placeholder names => replacement values$context = array('username' => 'bolivar');// echoes "Username bolivar created"echo interpolate($message, $context);

1.3 上下文

  • 每个方法接受一个数组作为context参数,用来存储不适合在字符串中填充的信息。数组可以包括任何东西。实现者必须确保他们尽可能包容的对context参数进行处理。一个context参数的给定值不可导致抛出异常,也不可产生任何PHP错误,警告或者提醒。

  • 如果在context参数中传入了一个异常对象,它必须以exception作为键名。记录异常轨迹是通用的模式,并且可以在日志系统支持的情况下从异常中提取出整个调用栈。实现者在将exception当做异常对象来使用之前必须去验证它是不是一个异常对象,因为它可能包含着任何东西。

1.4 助手类和接口

  • Psr\Log\AbstractLogger类可以让你通过继承它并实现通用的log方法来方便的实现LoggerInterface接口。而其他八个方法将会把消息和上下文转发给log方法。

  • 类似的,使用Psr\Log\LoggerTrait只需要你实现通用的log方法。注意特性是不能用来实现接口的,所以你依然需要在你的类中implement LoggerInterface

  • Psr\Log\NullLogger是和接口一起提供的。它在没有可用的日志记录器时,可以为使用日志接口的用户们提供一个后备的“黑洞”。但是,当context参数的构建非常耗时的时候,直接判断是否需要记录日志可能是个更好的选择。

  • Psr\Log\LoggerAwareInterface只有一个setLogger(LoggerInterface $logger)方法,它可以在框架中用来随意设置一个日志记录器。

  • Psr\Log\LoggerAwareTrait特性可以被用来在各个类中轻松实现相同的接口。通过它可以访问到$this->logger

  • Psr\Log\LogLevel类拥有八个日志等级的常量。

2. 包

psr/log中提供了上文描述过的接口和类,以及相关的异常类,还有一组用来验证你的实现的单元测试。

3. Psr\Log\LoggerInterface

namespace Psr\Log;/*** Describes a logger instance** The message MUST be a string or object implementing __toString().** The message MAY contain placeholders in the form: {foo} where foo* will be replaced by the context data in key "foo".** The context array can contain arbitrary data, the only assumption that* can be made by implementors is that if an Exception instance is given* to produce a stack trace, it MUST be in a key named "exception".** See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md* for the full interface specification.*/
interface LoggerInterface
{/*** System is unusable.** @param string $message* @param array $context* @return null*/public function emergency($message, array $context = array());/*** Action must be taken immediately.** Example: Entire website down, database unavailable, etc. This should* trigger the SMS alerts and wake you up.** @param string $message* @param array $context* @return null*/public function alert($message, array $context = array());/*** Critical conditions.** Example: Application component unavailable, unexpected exception.** @param string $message* @param array $context* @return null*/public function critical($message, array $context = array());/*** Runtime errors that do not require immediate action but should typically* be logged and monitored.** @param string $message* @param array $context* @return null*/public function error($message, array $context = array());/*** Exceptional occurrences that are not errors.** Example: Use of deprecated APIs, poor use of an API, undesirable things* that are not necessarily wrong.** @param string $message* @param array $context* @return null*/public function warning($message, array $context = array());/*** Normal but significant events.** @param string $message* @param array $context* @return null*/public function notice($message, array $context = array());/*** Interesting events.** Example: User logs in, SQL logs.** @param string $message* @param array $context* @return null*/public function info($message, array $context = array());/*** Detailed debug information.** @param string $message* @param array $context* @return null*/public function debug($message, array $context = array());/*** Logs with an arbitrary level.** @param mixed $level* @param string $message* @param array $context* @return null*/public function log($level, $message, array $context = array());
}

4. Psr\Log\LoggerAwareInterface

namespace Psr\Log;/*** Describes a logger-aware instance*/
interface LoggerAwareInterface
{/*** Sets a logger instance on the object** @param LoggerInterface $logger* @return null*/public function setLogger(LoggerInterface $logger);
}

5. Psr\Log\LogLevel

namespace Psr\Log;/*** Describes log levels*/
class LogLevel
{const EMERGENCY = 'emergency';const ALERT     = 'alert';const CRITICAL  = 'critical';const ERROR     = 'error';const WARNING   = 'warning';const NOTICE    = 'notice';const INFO      = 'info';const DEBUG     = 'debug';
}

原文链接:https://github.com/hfcorriez/fig-standards/blob/zh_CN/%E6%8E%A5%E5%8F%97/PSR-3-logger-interface.md

PHP规范PSR-1(基本代码规范)

PHP规范PSR-2(代码风格指南)

php规范PSR-3(日志接口)

php规范PSR-4

转载于:https://blog.51cto.com/shadow610/1659192

php规范PSR-3(日志接口)相关推荐

  1. 阻塞队列实现日志接口开发

    前言 近日开发一个日志接口,供其他系统通过 webservice 进行调用.考虑到并发,多线程决定使用阻塞队列实现日志接口.记录实现的过程,供大家参考,对阻塞队列不了解的,可以参照上一篇博文. 实现阻 ...

  2. 【编程规范】 后端API接口设计编写与文档编写参考

    文章目录 0 统一规范 0.1 理清业务流程 0.2 定义前后端开发的接口规范 0.3 定义接口文档 1 后端接口编写 1.0 后端接口介绍 1.0.1 接口交互 1.0.2 返回格式 1.0.3 C ...

  3. JavaScript零基础入门 13:DOM规范中的MutationObserver接口

    目录 一.MutationObserver接口 二.MutationObserver基本用法 1.observe()方法 2.回调与MutationRecord 3.disconnect()方法 4. ...

  4. Linux 服务器程序规范、服务器日志、用户、进程间的关系

    文章目录 服务器程序规范 日志 rsyslogd 守护进程 syslog函数 openlog函数 setlogmask函数 closelog函数 用户 进程间的关系 进程组 会话 系统资源限制 改变工 ...

  5. PHP程序员需要注意的代码规范PSR有哪些?

    前言 再次仔细的看了一下关于PHP代码的书写规范,我发现自己确实有很多不足的地方,需要改进,PHP代码遵循PSR(PHP Standard Recommendation)规范,之前忘了看哪本书看到ps ...

  6. 一个编程小白的Java SE学习日志 Ⅷ——接口、异常【极客BOY-米奇】

    文章目录 前言(转载请说明作者!)4.9~4.16编写 接口 概念 定义 实现 使用场合 设计模式 适配器设计模式 简单工厂设计模式 异常 Java如何处理异常 使用try..catch结构捕获异常 ...

  7. jetty 切换log4j日志接口

    jetty 版本7.6.9.v20130131 jetty 默认使用slf4j 作为日志实现.因此首先需要加入slf4j相关jar包. slf4j-api-1.7.5.jar          //此 ...

  8. slf4j 日志接口 统一

    引用:http://www.blogjava.net/dreamstone/archive/2007/07/09/128993.html 一.介绍: 简单日记门面(simple logging Fac ...

  9. php 接口日志,PHP 开发 APP 接口--错误日志接口

    APP 上线以后可能遇到的问题: ① APP 强退 ② 数据加载失败 ③ APP 潜在问题 错误日志需要记录的内容 数据表 error_log 字段: id app_id:app 类别 id did: ...

  10. 如何编写一个好的规范中投证券L2接口文档?

    如果你的案例可以直接依靠复制拿来使用,那这个文档就是好文档既然要简单,那就抓住核心:怎么简单怎么来,怎么省时间怎么来如果不知道怎么写,就把案例写的越详细越好.开发时间是非常宝贵的,而接口对接通常都是一 ...

最新文章

  1. VB中DateDiff 函数解释
  2. Java NIO系列教程(一) Java NIO 概述
  3. 使软件可二次开发_基于C++的?UG二次开发
  4. USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers
  5. Nacos(八)之Docker
  6. React开发(225):render中返回的值可以定义为一个方法
  7. 微服务 松耦合_超值干货:微服务架构下如何解耦,对于已经紧耦合下如何重构?...
  8. java对时间使用des加密_Java如何使用DES加密对象?
  9. iview兼容ie8_如何解决iview在安卓4.4.4的webview中的兼容性
  10. mysql的条件语句_mysql条件语句
  11. 基于JAVA+Servlet+JSP+MYSQL的二手房交易系统
  12. Bokeh 添加注释
  13. python编程( 第一份Windows平台运行的python代码)
  14. 更改Wamp下网站地址栏图标的显示
  15. vue使用contenteditable 实现光标处插入自定义图片
  16. 革命性的超级WiFi - 电视白空间解释说
  17. 业务层战略制定的思路和方法_如何确保公司年度战略目标落地—打造战略执行的方法论...
  18. c# wifi串口通信_C#串口通信 SerialPort类
  19. vue的watch监听的用法
  20. 10分钟教你生成超高逼格微信朋友圈

热门文章

  1. linux c 密码 星号,Linux C : 登录密码星号 * 显示,包含能回退 backspace
  2. python求职网站_Python 求职 Top10 城市,来看看是否有你所在的城市
  3. mysql设置主键可视化_mysql怎么设置主键自
  4. mysql文章列表_MySQL-分享文章列表 - Su的博客
  5. 【Dart学习】--Dart之正则表达式相关方法总结
  6. Eclipse中快速使代码对齐?1张图搞定!
  7. rk3399_android7.1查看当前的ddr频率
  8. Java中try与catch的使用
  9. 锋利的jQuery-4--图片切换的一个例子(自己理解后写的,以备忘记时看看)
  10. python重定向作用_Python重定向不起作用