Sentry Event Log combat in Linux

Source: Internet
Author: User
Tags dsn php error php error log php script


No matter what programming language you use, you will face the problem of how to handle the error log. Many programmers drift to the error log, until there is a failure to repent, if asked me how to do, I will recommend sentry!


Sentry is a platform for error recording and aggregation, just look at its beautiful interface and will like it:

Sentry
Sentry

on how to install Sentry, the official document has given detailed instructions, suggest that you read carefully, generally through virtualenv to install Sentry, specific reference: Learn to build a Python environment.

Reminder: When I install 7.5, the test has a cyclic redirection, if you can also install 7.4:

Pip Install sentry==7.4.3
When the installation is complete, if we want to monitor the PHP error log, it is usually embedded in our business code of the official PHP SDK, but this will certainly affect performance, but we can through the error log curve to save the nation, as long as the regular scan error log, the new generated data sent to Sentry is good.

BTW: If you find that the error counter in Sentry is always one, and even if a new error is not updated, it is mostly because you do not have permission to generate the Celerybeat-schedule file, which is generated by default in the CWD directory of the process.

How do you find the newly generated data? You need to record where the scan is, you can do this by using the Logtail or LOGTAIL2 provided in Logcheck, as for the difference between them, see the man document, and simply say that if the log is rotate, logtail may lose the number of the old log According to, and Logtail2 not.

<?php

Require_once __dir__. '/raven-php/lib/raven/autoloader.php ';

Raven_autoloader::register ();

$dsn = ' Http://your/dsn ';

$options = Array (
' Tags ' => array (
' Php_version ' => phpversion (),
),
' Trace ' => false,
);

$pattern _content = ' ^\[([^]]+)] PHP ([^:]+): \s+ (. +) ';

$parrern _level = implode (' | '), array (
' Debug ',
' Info ',
' Warning ',
' Error ',
' Fatal ',
));

$client = new Raven_client ($DSN, $options);

while ($line = fgets (STDIN))!== false) {
if (!preg_match ("/{$pattern _content}/", $line, $match)) {
Continue
}

List ($line, $timestamp, $level, $message) = $match;

$timestamp = gmdate (' y-m-d\th:i:s\z ', Strtotime ($timestamp));

Preg_match ("/{$parrern _level}/i", $level, $match);

$level = isset ($match [0])? $match [0]: ' ERROR ';

$client->capturemessage ($message, Array (), Array (
' Timestamp ' => $timestamp,
' Level ' => $level,
));
}

?>
Doing all of these preparations, the rest will be passed through the pipeline to the new data to the PHP script can:

shell> logtail2-f/path/to/php/error/log | Php/path/to/script
It is recommended that you run this command by crontab, but you need to pay attention to protect it with flock to prevent congestion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.