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

    1.2 消息

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

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

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

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

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

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

    2 上下文

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

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

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

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

    本博客的所有博文,大都来自自己的工作实践。希望对大家有用,欢迎大家交流和学习。 我的新站:www.huishougo.com
  • 相关阅读:
    ubuntu 16.04 tmux
    ubuntu 16.04 samba 文件共享
    ubuntu 16.04 有道词典
    ubuntu bless 16字节每行
    Win7任务栏图标大小调整为等宽
    ubuntu 16.04 vnc server
    ubuntu 16.04 U盘多媒体不自动弹出
    Linux录屏软件
    通过apt-get安装nvidia驱动
    调试X Server
  • 原文地址:https://www.cnblogs.com/zhouqingda/p/5175269.html
Copyright © 2020-2023  润新知