02-monolog - PHP 日志神器 之 Handlers, Formatters and Processors

Handlers

记录日志到文件与系统日志(syslog)

  • StreamHandler:记录日志到任何 PHP stream,用它来记录到文件。
  • RotatingFileHandler: 每天一个文件,会自动删除比$maxFiles老的文件,这只是一个很随意的方案,You should use logrotate for high profile setups though。
  • SyslogHandler: 记录到系统日志
  • ErrorLogHandler: Logs records to PHP's error_log()
    function.

下面的就是扩展阅读啦


发送提醒与邮件

看看就能理解

  • NativeMailerHandler: Sends emails using PHP's mail()
    function.
  • SwiftMailerHandler: Sends emails using a Swift_Mailer
    instance.
  • PushoverHandler: Sends mobile notifications via the Pushover API.
  • HipChatHandler: Logs records to a HipChat chat room using its API.
  • FlowdockHandler: Logs records to a Flowdock account.
  • SlackHandler: Logs records to a Slack account.
  • MandrillHandler: Sends emails via the Mandrill API using a Swift_Message
    instance.
  • FleepHookHandler: Logs records to a Fleep conversation using Webhooks.
  • IFTTTHandler: Notifies an IFTTT trigger with the log channel, level name and message.

记录到指定server与网络日志

接着看看

SocketHandler: Logs records to sockets, use this for UNIX and TCP sockets. See an example.
AmqpHandler: Logs records to an amqp compatible server. Requires the php-amqp extension (1.0+).
GelfHandler: Logs records to a Graylog2 server.
CubeHandler: Logs records to a Cube server.
RavenHandler: Logs records to a Sentry server using raven.
ZendMonitorHandler: Logs records to the Zend Monitor present in Zend Server.
NewRelicHandler: Logs records to a NewRelic application.
LogglyHandler: Logs records to a Loggly account.
RollbarHandler: Logs records to a Rollbar account.
SyslogUdpHandler: Logs records to a remote Syslogd server.
LogEntriesHandler: Logs records to a LogEntries account.

开发环境中,利用浏览器扩展

装扩展

FirePHPHandler: Handler for FirePHP, providing inline console
messages within FireBug.
ChromePHPHandler: Handler for ChromePHP, providing inline console
messages within Chrome.
BrowserConsoleHandler: Handler to send logs to browser's Javascript console
with no browser extension required. Most browsers supporting console
API are supported.
PHPConsoleHandler: Handler for PHP Console, providing inline console
and notification popup messages within Chrome.

记录到数据库

顾名思义

RedisHandler: Logs records to a redis server.
MongoDBHandler: Handler to write records in MongoDB via a Mongo extension connection.
CouchDBHandler: Logs records to a CouchDB server.
DoctrineCouchDBHandler: Logs records to a CouchDB server via the Doctrine CouchDB ODM.
ElasticSearchHandler: Logs records to an Elastic Search server.
DynamoDbHandler: Logs records to a DynamoDB table with the AWS SDK.

特殊的Handler

慢慢看

FingersCrossedHandler: A very interesting wrapper. It takes a logger as parameter and will accumulate log records of all levels until a record exceeds the defined severity level. At which point it delivers all records, including those of lower severity, to the handler it wraps. This means that until an error actually happens you will not see anything in your logs, but when it happens you will have the full information, including debug and info records. This provides you with all the information you need, but only when you need it.

DeduplicationHandler: Useful if you are sending notifications or emails when critical errors occur. It takes a logger as parameter and will accumulate log records of all levels until the end of the request (or flush()
is called). At that point it delivers all records to the handler it wraps, but only if the records are unique over a given time period (60seconds by default). If the records are duplicates they are simply discarded. The main use of this is in case of critical failure like if your database is unreachable for example all your requests will fail and that can result in a lot of notifications being sent. Adding this handler reduces the amount of notifications to a manageable level.

WhatFailureGroupHandler: This handler extends the GroupHandler ignoring exceptions raised by each child handler. This allows you to ignore issues where a remote tcp connection may have died but you do not want your entire application to crash and may wish to continue to log to other handlers.

BufferHandler: This handler will buffer all the log records it receives until close()
is called at which point it will callhandleBatch()
on the handler it wraps with all the log messages at once. This is very useful to send an email with all records at once for example instead of having one mail for every log record.

GroupHandler: This handler groups other handlers. Every record received is sent to all the handlers it is configured with.

FilterHandler: This handler only lets records of the given levels through to the wrapped handler.

SamplingHandler: Wraps around another handler and lets you sample records if you only want to store some of them.

NullHandler: Any record it can handle will be thrown away. This can be used to put on top of an existing handler stack to disable it temporarily.

PsrHandler: Can be used to forward log records to an existing PSR-3 logger

TestHandler: Used for testing, it records everything that is sent to it and has accessors to read out the information.

HandlerWrapper: A simple handler wrapper you can inherit from to create your own wrappers easily.

Formatters

✪ 为常用

LineFormatter: Formats a log record into a one-line string. ✪
HtmlFormatter: Used to format log records into a human readable html table, mainly suitable for emails.✪
NormalizerFormatter: Normalizes objects/resources down to strings so a record can easily be serialized/encoded.
ScalarFormatter: Used to format log records into an associative array of scalar values.
JsonFormatter: Encodes a log record into json.✪
WildfireFormatter: Used to format log records into the Wildfire/FirePHP protocol, only useful for the FirePHPHandler.
ChromePHPFormatter: Used to format log records into the ChromePHP format, only useful for the ChromePHPHandler.
GelfMessageFormatter: Used to format log records into Gelf message instances, only useful for the GelfHandler.
LogstashFormatter: Used to format log records into logstash event json, useful for any handler listed under inputs here.
ElasticaFormatter: Used to format log records into an Elastica\Document object, only useful for the ElasticSearchHandler.
LogglyFormatter: Used to format log records into Loggly messages, only useful for the LogglyHandler.
FlowdockFormatter: Used to format log records into Flowdock messages, only useful for the FlowdockHandler.
MongoDBFormatter: Converts \DateTime instances to \MongoDate and objects recursively to arrays, only useful with the MongoDBHandler.

Processors

PsrLogMessageProcessor: Processes a log record's message according to PSR-3 rules, replacing {foo}
with the value from $context['foo'].

IntrospectionProcessor: Adds the line/file/class/method from which the log call originated.

WebProcessor: Adds the current request URI, request method and client IP to a log record.

MemoryUsageProcessor: Adds the current memory usage to a log record.

MemoryPeakUsageProcessor: Adds the peak memory usage to a log record.

ProcessIdProcessor: Adds the process id to a log record.

UidProcessor: Adds a unique identifier to a log record.

GitProcessor: Adds the current git branch and commit to a log record.

TagProcessor: Adds an array of predefined tags to a log record.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,773评论 0 0
  • 写在前面 几个月之前,偶然看到了老庄的一篇博客——《如何评价一个新技术 》,讨论了docker是一个什么级别的发明...
    李福就是李建业阅读 7,466评论 0 13
  • 【读经】 代上7-8章 【金句】 拿弗他利的儿子是雅薛、沽尼、耶色、沙龙。这都是辟拉的子孙。 (历代志上 7:13...
    chanor阅读 612评论 0 0
  • 五谷为之,天地藏之。集天地之光华,采日月之流芳。一杯浊酒,两袖清风,杯尽自狂,天地几人高?酒清青豆与食尽,同销一世...
    行森阅读 228评论 0 1
  • 恒星 恒星的定义:本身能够发生热核反应,能够发光发热,最终演化为致密残骸的星体。恒星的质量必须大,至少8%的太阳质...
    小浣熊UU阅读 1,920评论 1 3