1. Specification

1.1 Basics

  • The LoggerInterface exposes eight methods to write logs to the eight RFC 5424levels (debug, info, notice, warning, error, critical, alert, emergency).

  • A ninth method, log, accepts a log level as first argument. Calling this method with one of the log level constants MUST have the same result as calling the level-specific method. Calling this method with a level not defined by this specification MUST throw a Psr\Log\InvalidArgumentException if the implementation does not know about the level. Users SHOULD NOT use a custom level without knowing for sure the current implementation supports it.

1.2 Message

  • Every method accepts a string as the message, or an object with a __toString()method. Implementors MAY have special handling for the passed objects. If that is not the case, implementors MUST cast it to a string.

  • The message MAY contain placeholders which implementors MAY replace with values from the context array.

2 Context

  • every method accepts an array as context data. This is meant to hold any extraneous information that does not fit well in a string. The array can contain anything. Implementors MUST ensure they treat context data with as much lenience as possible. A given value in the context MUST NOT throw an exception nor raise any php error, warning or notice.

  • If an Exception object is passed in the context data, it MUST be in the 'exception'key. Logging exceptions is a common pattern and this allows implementors to extract a stack trace from the exception when the log backend supports it. Implementors MUST still verify that the 'exception' key is actually an Exceptionbefore using it as such, as it MAY contain anything.

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,下划线_和句号.组成。其它的字符作为以后占位符规范的保留字。

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

2 上下文

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

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

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

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

第14篇 PSR-3规范(日志)的更多相关文章

  1. 第十一篇 Integration Services:日志记录

    本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...

  2. PHP PSR 代码规范基本介绍

    PSR 是 PHP Standard Recommendation 的简写,即PHP推荐标准. 目前通过的规范有 PSR-0(Autoloading Standard).PSR-1(Basic Cod ...

  3. 【译】第十一篇 Integration Services:日志记录

    本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...

  4. PSR标准规范

    PSR标准规范 基本代码规范 PHP代码文件 必须 以 不带 BOM 的 UTF-8 编码: 类的命名 必须 遵循 StudlyCaps 大写开头的驼峰命名规范: 类中的常量所有字母都 必须 大写,单 ...

  5. 使用Common.Logging+log4net规范日志管理【转载】

    使用Common.Logging+log4net规范日志管理   Common.Logging+(log4net/NLog/) common logging是一个通用日志接口,log4net是一个强大 ...

  6. Spring Boot 揭秘与实战(三) 日志框架篇 - 如何快速集成日志系统

    文章目录 1. 默认的日志框架 logback2. 常用的日志框架 log4j 1.1. 日志级别 1.2. 日志文件 3. 源代码 Java 有很多日志系统,例如,Java Util Logging ...

  7. Mysql高手系列 - 第14篇:详解事务

    这是Mysql系列第14篇. 环境:mysql5.7.25,cmd命令中进行演示. 开发过程中,会经常用到数据库事务,所以本章非常重要. 本篇内容 什么是事务,它有什么用? 事务的几个特性 事务常见操 ...

  8. PHP PSR标准规范

    PHP PSR标准规范,PHP开发者都需要遵循规范. 官网(英文版本): https://www.php-fig.org 官网(中文版本): https://psr.phphub.org

  9. python 14篇 写日志

    import sys from loguru import logger # 日志级别: debug 调试信息打印日志比较详细,级别最低 # info 正常的提示信息,级别较低 # waring 警告 ...

随机推荐

  1. HUE中Oozie执行Hive脚本

    Oozie执行hive,传入参数1. 新建一个workflow 2. 拖入一个hive2 3. hive脚本如下 CREATE TABLE IF NOT EXISTS spider_tmp.org_i ...

  2. SQL中去掉换行符 与空格符

    SELECT B.TradeOrderID AS '二段交易号',B.ZipCode AS '邮编', B.Province AS '省',B.City AS '市',B.District AS '区 ...

  3. 使用VirtualBox安装Android 4.2.2 x86 .

    http://blog.csdn.net/kunoy/article/details/8768205 virtual box 安装 android x86 不显示鼠标, --> 控制 --> ...

  4. UOJ34 多项式乘法(非递归版)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. python之websocket

    一.websocket WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实现了浏览器与服务器全双工 ...

  6. JNIjw02

    1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw02.h" JNIEXPORT void JNICALL ...

  7. selenium学习笔记(selenium下载安装)

    博主自己捣鼓的接口框架先到这里 等工作上正式开始使用再后续完善需求 还是继续学习python.学编程就直接动手写 就想看看python+selenium的组合 什么都不多说.先下载安装 博主这里已经安 ...

  8. 20165332实验三 敏捷开发与XP实践

    20165332实验三 敏捷开发与XP实践 实验内容 1:XP基础 2:XP核心实践 3:相关工具 实验1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化, ...

  9. echarts的散点图

    目前页面中有一个故障数据,做成散点图的效果,打算用echarts 来实现,国内的图表类其实选择挺多的,个人觉得echarts是比较好用的,来看看它有什么优点,一时中文的,联合百度地图,很多都对国内很友 ...

  10. WPF中的事件列表 .

    以下是WPF中的常见事件汇总表(按字母排序),翻译不见得准确,但希望对你有用. 事件 描述 Annotation.AnchorChanged 新增.移除或修改 Anchor 元素时发生. Annota ...