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 齺魺鯝魃鰖龖齟齞鯟鷇龴龱鷞麸麴齺鵛骠龖齈鵊鵈骞骛鱧鴨龖骞骛龇龖鼢骙黳鵛骖齺骓骑齺麸麴黵鸗魆魅鰾鵊鵈龴龱髚鷜髯鱂魃鰖鹶龖麦魙赘鷁鹺

本书共71小节:

评论 (写第一个评论)