阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Sentry集中化日志管理

100次阅读
没有评论

共计 4484 个字符,预计需要花费 12 分钟才能阅读完成。

在调试程序中,通过日志分期来排查 BUG 是一个重要手段,它可以说是程序调试的利器。

关于日志管理

随着应用组件变多,那么各 coder 对输出日志五花八门,有写入 stdout,有写 stderr, 有写到 syslog,也有写到 xxx.log 的。那么这将导致平台应用日志分布在各个地方,无法统一管理。

为什么使用 Sentry

Sentry 是一个集中式日志管理系统。它具备以下优点:

  • 多项目,多用户
  • 界面友好
  • 可以配置异常出发规则,例如发送邮件
  • 支持主流语言接口

安装

参考 https://docs.getsentry.com/on-premise/server/installation/

Requirements

  • Linux Server
  • PostgreSQL
  • redis-server
  • Python 2/3
  • Nginx

步骤

安装依赖包

sudo apt-get install python-setuptools python-pip python-dev libxslt1-dev libxml2-dev libz-dev libffi-dev libssl-dev libpq-dev libyaml-dev

安装 Sentry

pip install -U sentry==8.0.0rc2

配置

vim sentry.conf.py

DATABASES = {
    ‘default’: {
        ‘ENGINE’: ‘sentry.db.postgres’,
        ‘NAME’: ‘sentry’,
        ‘USER’: ‘postgres’,
        ‘PASSWORD’: ‘123456’,
        ‘HOST’: ‘localhost’,
        ‘PORT’: ‘5432’,
    }
}

EMAIL_HOST = ‘smtp.exmail.qq.com’
EMAIL_HOST_PASSWORD = ‘HE*******’
EMAIL_HOST_USER = ‘hewx@doordu.com’
EMAIL_PORT = 25
EMAIL_USE_TLS = False

设置配置文件路径到环境变量

export SENTRY_CONF=/home/hewx/workbench/sentry

初始化数据库

sentry update

配置 Web Server

sudo apt-get install nginx-full
sudo vim /etc/nginx/sites-enabled/default

location / {
  proxy_pass        http://localhost:9000;
  proxy_redirect    off;

  proxy_set_header  Host              $host;
  proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
  proxy_set_header  X-Forwarded-Proto $scheme;
}

启动

启动 Wokers

sentry celery worker -B

启动 Web 服务

sentry start

实例

PHP

使用浏览器访问 sentry,并创建一个项目

参考 PHP 使用指令 http://10.0.0.180/sentry/45183e54fa36/settings/install/php/

<?php

require_once “vendor/autoload.php”;

$client = new Raven_Client(‘http://5d68caa5e36c4eaa8f7c0601a521fab6:b5917e01d45f4656ab2d943264b90377@10.0.0.180/3’);

$error_handler = new Raven_ErrorHandler($client);
$error_handler->registerExceptionHandler();
$error_handler->registerErrorHandler();
$error_handler->registerShutdownFunction();

$client->captureMessage(“ 这里发生了一个错误 ”);

$i = 1 / 0;

?>

Python

pip install raven

from raven import Client

client = Client(‘http://f1ba62aa87cb4fc1a36d9e4f96017e99:96f5feef13dc4d45825e73548cd5b784@10.0.0.180/4’)

try:
    1 / 0
except ZeroDivisionError:
    client.captureException()

JavaScript

<!DOCTYPE html>
<html>
  <head>
    <meta charset=”utf-8″>
    <title>Sentry Test</title>
  </head>
  <body>
    <script src=”https://cdn.ravenjs.com/2.0.1/raven.min.js”></script>
    <script>
      Raven.config(‘http://e896b37840444a41adc9a80aa1292dcb@10.0.0.180/5’).install()

      try {
          doSomething(a[0])
      } catch(e) {
          Raven.captureException(e)
      }

    </script>
  </body>
</html>

Android

https://github.com/joshdholtz/Sentry-Android/tree/android-studio-ify

import com.joshdholtz.sentry.Sentry;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Sentry.init(this, “http://10.0.0.180”, “http://340137fc1dc6443e99d9d02b8f1638d0:cfd6587578e742948b60d72442fb4ac8@10.0.0.180/6”);
        Sentry.captureMessage(“OMG this works woooo”);

        int a = 1 / 0;
    }
……

RuntimeException: Unable to start activity ComponentInfo{com.joshdholtz.sentrytesting/com.joshdholtz.sentryapp.MainActivity}: java.lang.ArithmeticException: divide by zero
  Module “android.app.ActivityThread”, line 2189, in performLaunchActivity
  Module “android.app.ActivityThread”, line 2238, in handleLaunchActivity
  Module “android.app.ActivityThread”, line 138, in access$800
  Module “android.app.ActivityThread$H”, line 1201, in handleMessage
  Module “android.os.Handler”, line 102, in dispatchMessage
  Module “android.os.Looper”, line 136, in loop
  Module “android.app.ActivityThread”, line 5016, in main
  Module “java.lang.reflect.Method”, in invokeNative
  Module “java.lang.reflect.Method”, line 515, in invoke
  Module “com.android.internal.os.ZygoteInit$MethodAndArgsCaller”, line 792, in run
  Module “com.android.internal.os.ZygoteInit”, line 608, in main
  Module “dalvik.system.NativeStart”, in main

ArithmeticException: divide by zero
  Module “com.joshdholtz.sentryapp.MainActivity”, line 20, in onCreate
  Module “android.app.Activity”, line 5251, in performCreate
  Module “android.app.Instrumentation”, line 1087, in callActivityOnCreate
  Module “android.app.ActivityThread”, line 2153, in performLaunchActivity
  Module “android.app.ActivityThread”, line 2238, in handleLaunchActivity
  Module “android.app.ActivityThread”, line 138, in access$800
  Module “android.app.ActivityThread$H”, line 1201, in handleMessage
  Module “android.os.Handler”, line 102, in dispatchMessage
  Module “android.os.Looper”, line 136, in loop
  Module “android.app.ActivityThread”, line 5016, in main
  Module “java.lang.reflect.Method”, in invokeNative
  Module “java.lang.reflect.Method”, line 515, in invoke
  Module “com.android.internal.os.ZygoteInit$MethodAndArgsCaller”, line 792, in run
  Module “com.android.internal.os.ZygoteInit”, line 608, in main
  Module “dalvik.system.NativeStart”, in main

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4484字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中