sentry是一个手机错误日志的系统,用django写的。之前我的一个django网站使用的logging来记录错误,然后将ERROR级别的错误通过邮件发送到管理员邮箱。后来发现这么搞不太好,不知道错误发生的次数,信息也比较少,用sentry能收集到ip,浏览器等更多的信息,于是决定改用sentry。

sentry是独立于你的项目的,先安装好sentry,跟你项目无关。sentry正常运行之后,登录,然后设置一些跟你的项目有关的东西。

直接使用pip安装就行了。当然,推荐使用虚拟机安装,这里就不多说了。

1

pip install sentry

安装的过程中,我报了这个错误。我报了一个和postgres有关的错误,我决定使用MySQL,就不理这个,然后安装pyhton-mysql.

1

pip install mysql-python

到这里,sentry的安装就完成。和wordpress一样,下面进入系统进行设置。

使用sentry init命令,会初始化一个~/.sentry,在这里会保存必要的设置。

然后使用sentry start命令开始设置并启动web服务(以后就不要使用这个命令启动了,使用sentry run web)。

接下来就可以进入sentry了,用浏览器打开~/.sentry/sentry.conf.py里面写的地址,就可以进行相关设置了。

如果在界面的最上面看到“Background workers haven’t checked in recently…..”的提示,说明是sentry的celery没有正常启动,使用”sentry run cron”和”sentry run worker”启动一下。

如果使用root账户运行的,会遇到以下提示(不建议使用root账户):

1

2

3

4

5

6

7

Running a worker with superuser privileges when the

worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT

environment variable (but please think about this before you do).

User information: uid=0 euid=0 gid=0 egid=0

非要使用root的话,export C_FORCE_ROOT=”true” 就可以了。

如果django之前设置了logging,sentry是捕捉不到的,将logging相关的设置替换成以下(官方模板的,也可以自己修改):

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

LOGGING = {

    'version': 1,

    'disable_existing_loggers': True,

    'root': {

        'level': 'WARNING',

        'handlers': ['sentry'],

    },

    'formatters': {

        'verbose': {

            'format': '%(levelname)s %(asctime)s %(module)s '

                      '%(process)d %(thread)d %(message)s'

        },

    },

    'handlers': {

        'sentry': {

            'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.

            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',

            'tags': {'custom-tag': 'x'},

        },

        'console': {

            'level': 'DEBUG',

            'class': 'logging.StreamHandler',

            'formatter': 'verbose'

        }

    },

    'loggers': {

        'django.db.backends': {

            'level': 'ERROR',

            'handlers': ['console'],

            'propagate': False,

        },

        'raven': {

            'level': 'DEBUG',

            'handlers': ['console'],

            'propagate': False,

        },

        'sentry.errors': {

            'level': 'DEBUG',

            'handlers': ['console'],

            'propagate': False,

        },

    },

}