gì bạn cố gắng làm không nên, bởi vì handler sẽ phụ thuộc từ yêu cầu http thay vì bản ghi nhật ký, sẽ không nằm trong ngữ cảnh, tuy nhiên bạn có thể đăng ký trình xử lý riêng của mình trong Symfony dễ dàng:
Hãy tạo e lớp trình xử lý tùy chỉnh:
namespace AppBundle\Monolog\Handler;
use Monolog\Handler\AbstractHandler;
class StopBotLogHandler extends AbstractHandler
{
public function isBotRequestDetected()
{
// here your code to detect Bot requests, return true or false
// something like this:
// return isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']);
}
/**
* Checks whether the given record will be handled by this handler.
*
* This is mostly done for performance reasons, to avoid calling processors for nothing.
*
* Handlers should still check the record levels within handle(), returning false in isHandling()
* is no guarantee that handle() will not be called, and isHandling() might not be called
* for a given record.
*
* @param array $record Partial log record containing only a level key (e.g: array('level' => 100) for DEBUG level)
*
* @return bool
*/
public function isHandling(array $record)
{
return $this->isBotRequestDetected();
}
/**
* Handles a record.
*
* All records may be passed to this method, and the handler should discard
* those that it does not want to handle.
*
* The return value of this function controls the bubbling process of the handler stack.
* Unless the bubbling is interrupted (by returning true), the Logger class will keep on
* calling further handlers in the stack with a given log record.
*
* @param array $record The record to handle
*
* @return bool true means that this handler handled the record, and that bubbling is not permitted.
* false means the record was either not processed or that this handler allows bubbling.
*/
public function handle(array $record)
{
// do nothing, just returns true whether the request is detected as "bot", this will break the handlers loop.
// else returns false and other handler will handle the record.
return $this->isBotRequestDetected();
}
}
Bất cứ khi nào bạn thêm bản ghi vào nhật ký, nó sẽ vượt qua ngăn xếp trình xử lý. Mỗi trình xử lý quyết định xem nó có xử lý hoàn toàn bản ghi hay không và nếu có, việc sao chép bản ghi sẽ kết thúc ở đó.
Quan trọng: Đọc phpdoc từ isHandling()
và handle()
phương pháp để biết thêm chi tiết.
Tiếp theo, hãy đăng ký lớp như dịch vụ "không có thẻ":
# app/config/services.yml
services:
monolog.handler.stop_bot_log:
class: AppBundle\Monolog\Handler\StopBotLogHandler
public: false
Sau đó, thêm xử lý của nó-handlers
danh sách:
# app/config/config_prod.yml
monolog:
handlers:
# ...
stopbotlog:
type: service
id: monolog.handler.stop_bot_log
priority: 1
Lưu ý type
tài sản phải bằng service
, id
phải là tên dịch vụ trước khi được xác định và priority
phải lớn hơn 0
để đảm bảo ure rằng bộ xử lý của nó sẽ được thực hiện trước bất kỳ trình xử lý nào khác.
Khi GoogleBot thực hiện một yêu cầu đến ứng dụng trang web xử lý stopbotlog
dừng tất cả xử lý sau anh ta và không đăng ký bất kỳ nội dung log.
Hãy nhớ rằng đó không phải là cách được khuyến nghị để làm điều đó! Theo nhu cầu của bạn, việc thực hiện tùy chọn 1 hoặc 2 là đủ.
Bạn muốn di chuyển biểu thức sang cấu hình? Xác định trong cấu hình monolog không phải là vị trí chính xác, bạn có thể tạo một dịch vụ (monolog sử dụng) và chèn biểu thức thông qua các tham số: http://symfony.com/doc/current/components/expression_language.html – Rvanlaak
I don ' nghĩ rằng bạn hiểu câu hỏi của tôi một cách chính xác. Những gì tôi về cơ bản muốn là có thể thiết lập một 'handler' sẽ được gọi bởi' FilterBotsHandler' của tôi. Ví dụ: hãy xem [DeduplcationHandler] (https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/DeduplicationHandler.php). Bạn có thể chỉ định một 'Handler'here khác, nó sẽ được gọi bởi' DeduplicationHandler'. Tôi cố gắng làm như vậy. –