第14篇 PSR-3规范(日志)
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 aPsr\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 anException
before 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-Z
,a-z
,0-9
,下划线_
和句号.
组成。其它的字符作为以后占位符规范的保留字。实现者
可以
使用占位符来实现不同的转义和翻译日志成文。因为用户
并不知道上下文数据会是什么,所以不推荐
提前转义占位符。
2 上下文
每个方法接受一个数组作为
context
参数,用来存储不适合在字符串中填充的信息。数组可以包括任何东西。实现者
必须
确保他们尽可能包容的对context
参数进行处理。一个context
参数的给定值不可
导致抛出异常,也不可
产生任何PHP错误,警告或者提醒。如果在
context
参数中传入了一个异常
对象,它必须以exception
作为键名。记录异常轨迹是通用的模式,并且可以在日志系统支持的情况下从异常中提取出整个调用栈。实现者
在将exception
当做异常
对象来使用之前必须
去验证它是不是一个异常
对象,因为它可能
包含着任何东西。
本文档描述了日志类库的通用接口。
主要目标是让类库获得一个Psr\Log\LoggerInterface
对象并能通过简单通用的方式来写日志。有自定义需求的框架和CMS可以
根据情况扩展这个接口,但推荐
保持和该文档的兼容性,以确保应用中使用到的第三方库能将日志集中写到应用日志里。
第14篇 PSR-3规范(日志)的更多相关文章
- 第十一篇 Integration Services:日志记录
本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...
- PHP PSR 代码规范基本介绍
PSR 是 PHP Standard Recommendation 的简写,即PHP推荐标准. 目前通过的规范有 PSR-0(Autoloading Standard).PSR-1(Basic Cod ...
- 【译】第十一篇 Integration Services:日志记录
本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...
- PSR标准规范
PSR标准规范 基本代码规范 PHP代码文件 必须 以 不带 BOM 的 UTF-8 编码: 类的命名 必须 遵循 StudlyCaps 大写开头的驼峰命名规范: 类中的常量所有字母都 必须 大写,单 ...
- 使用Common.Logging+log4net规范日志管理【转载】
使用Common.Logging+log4net规范日志管理 Common.Logging+(log4net/NLog/) common logging是一个通用日志接口,log4net是一个强大 ...
- Spring Boot 揭秘与实战(三) 日志框架篇 - 如何快速集成日志系统
文章目录 1. 默认的日志框架 logback2. 常用的日志框架 log4j 1.1. 日志级别 1.2. 日志文件 3. 源代码 Java 有很多日志系统,例如,Java Util Logging ...
- Mysql高手系列 - 第14篇:详解事务
这是Mysql系列第14篇. 环境:mysql5.7.25,cmd命令中进行演示. 开发过程中,会经常用到数据库事务,所以本章非常重要. 本篇内容 什么是事务,它有什么用? 事务的几个特性 事务常见操 ...
- PHP PSR标准规范
PHP PSR标准规范,PHP开发者都需要遵循规范. 官网(英文版本): https://www.php-fig.org 官网(中文版本): https://psr.phphub.org
- python 14篇 写日志
import sys from loguru import logger # 日志级别: debug 调试信息打印日志比较详细,级别最低 # info 正常的提示信息,级别较低 # waring 警告 ...
随机推荐
- 嵌套的SQL另外一种写法
SELECT a.TradeOrderID 二段交易号 , c.TradeOrderID 一段订单号 , a.BaggingDate AS 出库时间 , a.TransportOrderCode AS ...
- NumPy在数组上的迭代
NumPy - 数组上的迭代 NumPy 包包含一个迭代器对象numpy.nditer. 它是一个有效的多维迭代器对象,可以用于在数组上进行迭代. 数组的每个元素可使用 Python 的标准Itera ...
- xp_sp3_pro_简中_x86_cd_vl_x14-74070
1.镜像文件: zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-74070.iso 来自 msdn itellyou ...
- scala学习手记5 - 元组与多重赋值
假定要在一个方法中返回多个值.比如需要返回一个人的名.姓和邮箱地址.在Java中最常用的方法是定义一个Person类,其中包括相对应的字段:还有些不常用的方法就是返回一个集合或数组,拿到结果后再进行循 ...
- TCP异步IO_服务端_测试
1.测试代码来自于 JDK7 AIO初体验 http://www.iteye.com/topic/1113611 1.1. package aio; import java.net.InetSocke ...
- SQL优化之列裁剪和投影消除
列裁剪 对于没用到的列,则没有必要读取它们的数据去浪费无谓的IO 比如我们有一张表table1,它含有四列数据(a,b,c,d).当我们执行查询select a from table1 where c ...
- TCP粘包问题及解决方案
① TCP是个流协议,它存在粘包问题 TCP是一个基于字节流的传输服务,"流"意味着TCP所传输的数据是没有边界的.这不同于UDP提供基于消息的传输服务,其传输的数据是有边界的.T ...
- 解决:TypeError: object() takes no parameters
运行测试用例时发现以下报错 Ran 1 test in 22.505s FAILED (errors=1) Error Traceback (most recent call last): File ...
- 关于Java类
一个.java文件中可以有很多类.不过注意以下几点: 1.public 权限的类只能有一个(也可以一个都没有,但最多只有1个) 2.这个.java文件的文件名必须是public类的类名(一般的情况下, ...
- neutron之SDN简单测试
title: Neutron SDN 手动实现手册 date: 2017-04-13 23:37 tags: Network 本文旨在通过自己搭建类似neutron (openvswitch + gr ...