69. 日志系统与监控

龳龰龭龬龪龧龤龣龳龰龟龞龛龗龤龓龒龐龎龌龋龉龈龇龄龃龀齾齻齺齷龭龬龐 drupal 龭龬龳龰齧齣齢齠齟齞齝齜齝齛齗龭龬龐齔齓齢齑 RFC 5424 PSR-3 鼰鼮齞齠齟齢鼪鼧鼥鼡鼟齜鼛 8 鼖鼔齝龐鼒鼐鼎鼊鼉鼈鼅鼁黿黻黹黷龧黳黲黮黭黬黨黦龋齺齧黥龳龰龛龗黣黡點黚龇默齾黗龇黕黓龐黐黍黉黇黅黄黁黀麾麺龳龰黀齧麸龇龛麷麳麯麮齠麭龧龐麩麧鼰麥麣鼒麡麠

龭龬麜鼔麘

麗黇龭龬黡麔麓麏麺黣黡鼥鼡麍鼰齠齟齜鼛黍麊 8 鼖鼔齝麘

emergency 麘龳龰黀齧麸龐鹹鹷黣黡

alert 麘黣黡鹵鹴龐鹱鹰鹮鹫鹩鹧

critical 麘鹣鹡鹞鹜龐鹚齾鹙鹕齻鹓鹐

error 麘鹍鹉龐鹆鹃鹂鹀鸼鸹

warning 麘鹵鹴龐黐黍鸳麸龤鸰鸯麸齻鼎鸫

notice 麘鸪鹷齠齟龐黲麔黕黓

info 麘鸪鹷齠齟龐黍麸鸤鸠齗麜

debug 麘鸘鸕齠齟龐鹹鹷鸓鸒

齣麾鼔齝麸黉鸏鸌鸋鼛鸇 0 7 麏黉鸏鷾鷻鷾黣黡麍龐鷷鷶鸰鷲鷰麺鷬鷨黥 RFC 5424 PSR-3 鷧龐鷥齣麊鷤鷣麘

http://tools.ietf.org/html/rfc5424

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md

drupal 龭龬龳龰鷔鷨麘

龳龰鷧齛齗齻龭龬齠齟鼮鷑鼮鼐龐黲麔鸰鷎齞龐黥 drupal 鷧鷌齞齝鷈鷅鼛鷌鷂鷁 channel 鷈龐鶾鼖鷂鷁鶼鹆鶻鼖鷂鷁鶼鶷龐鶶鷶齧齣鸇龭龬鶴鶲鷧黕鶰鷂鷁鶯黦鶬鶪鶻鼖鷂鷁鶼鶷龐鷂鷁鶯齧齣鶦鶥龛鶢鶠鷲鷰龐鷂鷁鶼鶷齷鸳麸龭龬龳龰齻鶜鶙黿黥鶘鶔龭龬齠齟齷黕鶰齛齗鶒 Logger 鹩鹧齻龐鶾鼖鷂鷁鶼鶷齧齣鶍鶊鼖齛齗鶒龐鶇鷶齢鶃龃鼔麗鶀鵼黭龐鷂鷁鶼鶷鵹鵷麭麸鸤龰鶻齻鸳麸黁鶙龐黁齾鸘麸鵶鼰黉黇龛麭鶾鶻鼖齛齗鶒龐龳龰鵲鵱黳黦龋龤鶻鼖齛齗鶒龐鶇鵮鵫鼰龭龬鵨鶜黉黇黅龐鶶鷶齧齣鷲鷰鵦黷齻齛齗鶒麸鵣鵡齝齻鵟鹞龐黐黍麗黇黡麔鼔齝龛麷鵛鵚麺麮齠麜麠

龭龬鶴鶲麘

鵮鵫黦鶬鶪鷂鷁鶼鶷

鵙鵗鶯麘 logger.factory

齞麘 \Drupal\Core\Logger\LoggerChannelFactory

默齾鶴鶲鶼鶷麘 \Drupal::service("logger.factory");

默齾鷂鷁鶼鶷麘 \Drupal::service("logger.factory")->get($channel);

默齾鷂鷁鶼鶷齻鹚鴾鼎鸫麘 \Drupal::logger($channel);

黥鴺鶒鴶鴲鴰鴯龳龰鴫鴧鴤鴠鶍鴞鴚鼛鷌 logger 鷈齻鵙鵗麏鴙齺齷齛齗鶒麍龐黥鶴鶲鶼鶷黦鶬鶪龇鶇鷶鴫鸰鹂鶜龐黥鴖黭鶰鼥鷧鴙齧齣鸘麸黍麊鴔鴑鴐鴏鴌鶊齛齗鶒麘

addLogger(LoggerInterface $logger, $priority = 0)

黥黦鶬鶪鷂鷁鶼鶷龇龐鶴鶲鴫鼰鴠鶍齻齛齗鶒鴂麭鶇龐鼒鴀黲麔鹂鹀麘鷂鷁鶼鶷鴙鶍鳼鶯鼎鸫麸齣鴐鴏齛齗鶒龐黥鶴鶲鶼鶷麾鴐鴏鼼黥鷂鷁鶼鶷麾鴐鴏齷鶍鳺齝齻龐鳸黥鶴鶲鶼鶷麾鴐鴏龐鳷龒鴠鶍鷂鷁鶼鶷鳴鴫鸰鴐鴏龐齓鳳鳯黦鶬鶪齻鴠鶍鷂鷁鶼鶷鳴鴫鸰鴐鴏鹃齛齗鶒龐鳫黍鳧黥鷂鷁鶼鶷麾鸘麸鴐鴏齛齗鶒龐鳷龒鳤鷷鷂鷁鶼鶷黀鴫鸰鴐鴏

鷂鷁鶼鶷麘

鼒齷鶶鷶鸳麸龭龬龳龰齻龰鶻黁鶙龐齞鷲鷰黍麊麘

\Drupal\Core\Logger\LoggerChannel

鶴鶲鴫鼛鶾鶻鼖鴂鶜齻鷂鷁鶯麏龭龬齞齝鴞龬麍黦鶬鶪鶻鼖黨鳢齻鷂鷁鶼鶷齔鹂鶜鴠鶍齛齗鶒龐鷂鷁鶼鶷麗黇龭龬齻 8 齞黡麔麓麜鼔鵹鵷黍麊 8 鼖鹚鴾鼎鸫麘

emergency($message, array $context = array());
alert($message, array $context = array());
critical($message, array $context = array());
error($message, array $context = array());
warning($message, array $context = array());
notice($message, array $context = array());
info($message, array $context = array());
debug($message, array $context = array());

鶇鷶黳齷鹚鴾鼎鸫龐黦鳝麾鳴黥黍麊鼎鸫鷧鵼黭麘

log($level, $message, array $context = []);

黍鸳麸鹃鼎鸫黲麔鶠黭鴂鳔麜鼔齠齟龐齧齣齷鸏鳓鳐麺黉鸏龐鷥鳏鳎麠

鳤鷧 $message 齷麔齛齗齻鳊齟龐齧齣鳆鳃鳀鲿龐鳀鲿龟鴺黥 $context 鷧鵹鵷龐鳀鲿鲽鲺鶍 drupal 鲸鲵鼼 PSR3 鲸鲵麘

drupal 鲸鲵黍麊麘

\Drupal::logger('theme')->warning('Theme hook %hook not found.', ['%hook' => $hook]);

$message 鷧鳀鲿鶯黲麔鴏鳏鲨龐鶍鲥鷑鳏鲨麘 @ % : 麏鲞鲛麍 龐鼒鵹鸋龳龰黮黭黀鳼齻鲘鲔鹩鹧龐鳃鷰黍麊麘

@ 麘鲒鷰 html 鲑鲐鸏鳓龐黍鳧齷 MarkupInterface 鶼鶷鼰黀黮黭鲍鲉鹩鹧

% 麘鲇 @ 鶻鼐鵡鲒鷰鹩鹧龐齔鼰鲃鳧鳆鲀齾 <em> 鱽鱹鷧

: 麏鲞鲛麍麘鱷鱶鹙鹕齻鱵鱲龐齔鵡鲒鷰鹩鹧龐鹷鹷麸鱰鹩鹧鱮黁齠齟麠

$context 鷧鱭鶯鴙黲麔鱬鳏鲨龐鱩鱰鲽鲺鳀鲿鸓鸒齠齟鷥麘

\Drupal\Component\Render\FormattableMarkup::placeholderFormat

PSR3 鲸鲵黍麊麘

鱦齧齣麸 PSR3 鲸鲵 齻鼐鱢龐鴙齺齷鳀鲿齣 {} 鳆鱙龐黍麘

\Drupal::logger('theme')->warning('Theme hook {hook} not found.', ['hook' => $hook]);

鳫鼒黲麔齛齗鶒鸳麸黍麊鵙鵗鼰鳤鲒鱖鼛 drupal 鲸鲵麘

鵙鵗 id logger.log_message_parser

齞麘 \Drupal\Core\Logger\LogMessageParser

鹃鵙鵗黥龟龞鼰 PSR3 鲸鲵鲽鲺鱐鱖鼛 drupal @ 鲽鲺龐麜鳼鱰黍麊麘

\Drupal::logger('theme')->warning('Theme hook @hook not found.', ['@hook' => $hook]);

黥鷂鷁鶼鶷龟龞麾麊鳎黉鱌鴫鸰鶠鱈鱆鴏齣麊鳀鲿龐鴠齣鹫鼊鱂鶍鴂鳔鴙齧齣鸳麸麘

$context += [
      'channel' => $channel, //频道名
      'link' => '',
      'user' =>$currentUser , //当前用户账号对象,注意该对象不能直接转化为字符串
      'uid' => $currentUser->id(), //用户id
      'request_uri' => $request->getUri(),
      'referer' => $request->headers->get('Referer', ''),
      'ip' =>$request->getClientIP(),
      'timestamp' => time(),
];

鹂鹀鼒麡鱆鴏齻鵲鵱鳀鲿鰰麸龤 PSR3 鲸鲵鴂鳔龐鴠齣鸳麸鶇鷶鴙麔麸 PSR3 鲸鲵齻 鼐鱢龐黍麘

\Drupal::logger('user')->warning('hill @name : your uid is {uid}.',[鷌@name鷈=>$name]);

黍龧鴠鷥 PSR3 鲸鲵齧齣鼼 drupal 鲸鲵鰭麸龐鶦鱰鵲鵱鳀鲿鰰麸 PSR3 鲸鲵 龐鴠齣鰬鱲鴠鶍齛齗鶒鰫鰰麸 log_message_parser 鵙鵗鶼麾麊鳎 $context 黮黭鹩鹧龐齣默齾龰鶻齻鲒鱖麏鷥麊鳎龐鱩鱰鼒鶻鰩鰨鰤鵱鼛 drupal 黲麔鰡黮龐鼰鹃鵙鵗鰝齾鷂鷁鶼鶷鷧鵼黭麍黿鹂鹀鳯鶰鹃鵙鵗鲒鱖齻鳀鲿鷧鱂鶍鸰 $message 麸齾齻 PSR3 鲸鲵鳀鲿黀鸰鰚鰘黿黥 PSR3 鲸鲵齻鼐鱢鷧鰕鰒鲛麔鼪鰎鰊鳀鲿鶯龐黀麔龌龋鰇鲵黿鶠鷲鷰鳀鲿齔黀鰅鰄龧麸 PSR3 鲸鲵龐鸳麸 drupal 鲸鲵齧鶍鴌鶊鸹鰂齓麓鰷鴌鰁麠

黻齣麾鵹齾齻鷂鷁鶼鶷鼎鸫黷龐鹃鶼鶷鱦鶍黍麊鼎鸫麘

setRequestStack 麘鯾鯻鯺鯷鯳鯱龐麸鱰麭鵲鵱麾麊鳎鯭黉鵹鵷鯬

setCurrentUser 麘鯾鯻龎鳏麸鸤龐麸鱰麭鵲鵱麾麊鳎鯭黉鵹鵷鯬

setLoggers 麘鯾鯻鲔龞齛齗鶒

addLogger 麘鯾鯻黨鼖齛齗鶒

龳龰鵲鵱齛齗鶒麘

龳龰鵹鵷龤鶻鼖鵲鵱齻齛齗鶒龐鴙齷鵲鵱鲘鲀鯪鶻齻鶻鼖龐麸鱰鼰龭龬齠齟齛齗齾黉黇黅龐黍麊麘

鵙鵗 id logger.dblog

齞麘 Drupal\dblog\Logger\DbLog

鶃龃鼔麘 0 麏齛齗鶒麸鯧黉鸌鸋鶃龃鼔龐齧齣鼛鵮黉龐黉鯬鷾鰕鶃龃鼔鷾鯤龐鷾龃鵼黭麍

鹃齛齗鶒黥黉黇黅鸌 watchdog 鷧齛齗龭龬龐鼒鴀鶼鹃鸌齻龞齜黡麔鸏鴯鯣鯢黍麊麘

uid 麸鸤 id

type 鷂鷁鶯龐鴙齺齷龭龬齞齝鴞龬龐鴫鸰鯡鯟齾 64 鸏鯘齣龟

message 鱂鶍黮黭鲽鲺鳀鲿鱐鱖齻龭龬鳊齟龐鳳鳯鼰 PSR3 鲸鲵鲒鱖鯗龤 drupal 鲸鲵

variables 鳯鶰 logger.log_message_parser 鹩鹧齻麾麊鳎鯭黉龐鳳鳯鼰 PSR3 鲸鲵 鲒鱖鯗龤 drupal 鲸鲵龐鱂鶍鸰麸齾齻鵲鵱鳀鲿鼼麸鸤鶠鷲鷰齻 PSR3 鲸鲵鳀鲿黀鸰鰚鰘

severity 黉鸏鯬龐鳏鳎鵹齾齻齠齟黣黡鼔齝龐鸇 0 7

link 麾麊鳎鷧齻 link 鳀鲿

location 鯺鯷 uri

referer 鵣鯕鯔鯓

hostname 鰤鸤鯒 ip

timestamp 龇鯏鯎

黥龳龰鵶鯊 /admin/reports/dblog 齧齣鼁齾鹃鸌齻齠齟龐黍鳧鶥龛鶢鶠鷲鷰龤鷂鷁鶯龐鹃鹡鯉鼰鯈鴏鹃鷂鷁鶯

鶍龇鯅黥黉黇黅鷧齛齗龭龬龇龐鹃鯄鯁麩鮽齧鰷鴫龛龗鮺鹷龐黹龇鮹鸫黮黭龭龬齛齗龐齧齣黕鶰鯾鯻鮶麸龭龬黉黇黅齻鼎鱢龣鮳龐齧齣黥鮱鯻鳎鵚鷧鯾鯻鶻鼖鮮鴞鶯鮪鵡鷌 dedicated_dblog 鷈齻黉黇黅龐鹃鶯鶦黍麊鹷鲿鷲鷰麘

\Drupal\dblog\Logger\DbLog::DEDICATED_DBLOG_CONNECTION_TARGET

鮱鯻黍麊麘

$databases['default']['default']=$dsn_1; // 鮖鵙鵗鶒

$databases['default']['replica']=$dsn_2;

$databases['default']['dedicated_dblog']=$dsn_3;

黉黇黅鮱鯻鸓鷥麩龳鮓黉黇黅鮖黚龐黍麾鹞鹜龛龗龇鼰鸳麸鮶麸黉黇黅黮黭龭龬齛齗龐黍鳧鱂鶍鯾鯻鮶麸龭龬黉黇黅龇龐鼰鰰麸鵲鵱黅鮒鸕鶻鶀龐鴠鶍鼒麡鮎鮍鳴鮹鸫齛齗龇鼰鮊龌鮺鹷麠

鶠鷲鷰齛齗鶒麘

龄龃鷲鷰鶻鼖齞龐黦龋黁鶙麘 \Psr\Log\LoggerInterface

鰅鰄鸳麸鲑鮆 \Drupal\Core\Logger\RfcLoggerTrait 龐鼒鼐黳黲麔鴶鵨 log 鼎鸫鹫齧

鼰鹃齞鷲鷰鯗鵙鵗龐麭龌 logger 鴞鴚鼼鶃龃鼔龐齣麊鮂鯾鮀魿鶯鼛鷌 yunke 鷈龐鶶鷶鵣黦龋鶻鼖齛齗鶒龐魼鰷齷魸齾魴鯾鷲黣黡麓龇魱魭鹧魪龛麷麮齠麺鵛鵚麘

魨鶻魤麘黥鮀魿鮮齗鰬鹮鳎鵚麘

yunke\src\Logger\Logger.php

龟鴺鼛麘

<?php
/**
 * @file
 * 日志记录器
 */

namespace Drupal\yunke\Logger;

use Psr\Log\LoggerInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use \Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Logger\LogMessageParserInterface;

class Logger implements LoggerInterface
{
    use RfcLoggerTrait;
    use DependencySerializationTrait;

    protected $parser;

    public function __construct(LogMessageParserInterface $parser)
    {
        $this->parser = $parser;
    }

    public function log($level, $message, array $context = [])
{
        //可在配置系统中指定级别,见本系列配置系统,记录器得到的级别为整数
        if ($level > 3) {
            return;
        }
        //引入log_message_parser服务以支持PSR3风格的变量占位
        $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context);
        //进行占位替换
        $msg = new FormattableMarkup($message, $message_placeholders);
        //调用第三方接口给管理员发送邮件或短信 这里采用如下代码供你测试 将在系统根目录产生文件
        file_put_contents("snsyunke.txt", $msg);
    }
}

魨魍魤麘黥 yunke.services.yml 鷧鷲鷰鵙鵗龐黍麊麘

  yunke.log:
    class: \Drupal\yunke\Logger\Logger
    arguments: ['@logger.log_message_parser']
    tags:
      - { name: logger , priority: 100 }
      - { name: backend_overridable }

黡魉鲘鲀鮀魿鹫齧魇麸鹃齛齗鶒龐黥鸹鰂鶒鷧鵼黭齣麊鴔鴑麘

\Drupal::logger('yunke')->alert(" hill @admin ,Website needs you now! \n ip:{ip} \n time: @time", ['@admin' => "yunke","@time"=>date("Y-m-d H:i:s")]);

龧鼰鴧齾齣麊鳊齟麘

 hill yunke ,Website needs you now! 
 ip:127.0.0.1 
 time: 2018-04-29 03:27:53

鳼龇鵲鵱齛齗鶒鴙鴫齛齗龭龬龐齧黥鯔鬵鵶鯊鼅鼁

齛齗鶒鶃龃鼔麘

龎龳龰鷲鷰龤鶊鼖齛齗鶒龇龐齧齣魞鷲鶃龃鼔龐麸鯧黉鸌鸋龐齧齣鼛鵮黉龐黉鯬鷾鰕鶃龃鼔鷾鯤龐鷾龃鵼黭黿龧齧鰷鴫龀齾龪龃鵼黭齻齛齗鶒齣魝麸鼎鱢鬴鰡鴂鶜齻 $level, $message, $context 龐齣鬳鬯鵶鯉齻齛齗鶒龐鳫鼒齷黀黭齻龐鼒鼐鵡鼼黁鶙鷲鷰黀鬫鴺龐鶃龃鼔鬧麸鱰魞鷲龃鵶鬣鬡麠

鬞鴲點黚麘

龭龬齠齟鵲鵱齣鴂鶜齻鬝鬙鬖鸋麏龳龰鵲鵱鬕鬝麍龐黍黲鬞鴲龐齧黥鸘麸鶇鳏龃鸘麸鬞鴲鬑黉龐黍麊鴠鸋麘

\Drupal::logger('yunke')->alert(t("my name is @name", ['@name' => $name]));

鳫鼒鼐齔黀鶃鬍龐鴙黀鬋鶘龐鬞鴲黀鰷鸳麸 PSR3 鲸鲵鳀鲿鲽鲺麏麾麊鳎黉鱌黀鰷鴂鳔 PSR3 鲸鲵 鲽鲺鳀鲿龐鳫鳊齟鷧齧齣鸳麸 PSR3 鲸鲵鵲鵱鳀鲿麍龐齓黳鰷麸鱰鶠鬇鶥龛齻鴔鴑龐鸪鬆齻鵡鸫齷鶼齛齗鶒龐鶴鶲麜鵙鵗鷲鷰黮黭鬂鵨龐黥齛齗鶒鷧黮黭鬞鴲麠

髿髽鯣鯢麘

  1. bug 麘黥 \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders 鷧鼰鲽鲺鳀鲿鷌 : 鷈鹍鴞齛鼛鷌髻鷈髹髵齠齟髱髯
  2. 黥鷂鷁鶼鶷鵼黭髮鯏齧鰷魝髪鳔鷎鸘麸龐鴠齣黥龳龰鯾鷲龤鸘麸髩鼡鼛 5 鶔龐齣髧髦鮹髥鳔鷎

3 鲟黥 8 鼖龭龬麜鼔鷧龐鮮鳏 drupal8.5 黥鵶鯊鵲鵱鱂鶍黦龋鮱鯻龓龒麜鼔齻龭龬髡鸰齛齗龐鵲鵱鲔龞齛齗

4 鲟龳龰鵹鵷龤 dblog 鮀魿龐鶇鵹鵷黍麊鶻麡魼鰷麘魭鹧髟鯉 /admin/config/development/logging 齻鮱鯻魼鰷龐齧齣鯾鷲龭龬鸌黉黇齻鶊髜龐齔鵹鵷髛髗鲍鵗龐髛髗龇龐鼰髕黻鶊髒齻髎髌齻黉黇黿鹃鮀魿鱦鵹鵷龭龬鬖鸋鬫鱩魼鰷麜龐黀鮒赘鷨麠

本书共78小节:

评论 (写第一个评论)