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 齼鮓鯭魡鰪龘齝齜鯮鷘龴龳鷮麱麭齼鵦髎龘齄鵜鵘髌髉鱪鴳龘髌髉龅龘鼟髇黦鵦髅齼髃髀齼麱麭黪鸣魦魤鱊鵜鵘龴龳鬄鷬鬖鱌魡鰪鹾龘麤魸赘鷔麀

本书共88小节:

评论 (写第一个评论)