Skip to content

Instantly share code, notes, and snippets.

@pilgrim2go
Forked from nagyv/0 - odoo logs to sentry
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pilgrim2go/3c5b6ea0615159ac5a46 to your computer and use it in GitHub Desktop.
Save pilgrim2go/3c5b6ea0615159ac5a46 to your computer and use it in GitHub Desktop.
This gist provides a simple setup to add Sentry logging to OpenERP/Odoo.
As Odoo's logging setup is rather limited, I'm using syslog to collect and forward logs to Sentry.
These scripts should run without any modification on an Ubuntu based server, assuming the paths and user names for openerp match with the ones in the `supervisord.conf` file.
Kudos go to:
* https://gist.github.com/avoine/2912777
* http://hashbang.fr/openerp-log.html
# This file should be copied under /etc/rsyslog.d/
# A 1-liner to improve the OpenERP syslog
$template OpenErpTemplate,"\n%timereported% %HOSTNAME% [%syslogseverity-text%] %msg%"
# if we are dealing with messages coming from Openerp
# then write them to a log file /var/log/openerp/openerp-syslog.log
:programname,isequal,"OpenERP" /var/log/openerp/openerp-syslog.log;OpenErpTemplate
# if we are dealing with messages coming from Openerp
# and the message is and error or worse
# then write them to a log file /var/log/openerp/openerp-syslog.err
if $programname == 'OpenERP' and $syslogseverity <= '3' then /var/log/openerp/openerp-syslog.err;OpenErpTemplate
#$ActionQueueType LinkedList # use asynchronous processing
#$ActionQueueFileName test # set file name, also enables disk mode
#$ActionResumeRetryCount -1 # infinite retries on insert failure
#$ActionQueueSaveOnShutdown on # save in-memory data if rsyslog shuts down
# if we are dealing with messages coming from Openerp
# then forward the message to our rsyslog2sentry server
:programname,isequal,"OpenERP" @@127.0.0.1:10514;RSYSLOG_SyslogProtocol23Format
# if we are dealing with messages coming from Openerp
# then don't add them to /var/log/syslog
:programname,isequal,"OpenERP" ~
raven
loggerglue
#!/usr/bin/env python
#
# Copyright 2012 Patrick Hetu <patrick.hetu@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You need to install:
#
# * http://raven.readthedocs.org/en/latest/index.html
# * http://pypi.python.org/pypi/loggerglue/
####################
# Configuration #
####################
SENTRY_LEVEL = 8 # Sets the minimum PRIVAL_SEVERITY that is forwarded to Sentry. 8 = all
SENTRY_DSN = ''
SERVER_IP = ''
SERVER_PORT = 10514
####################
# Code starts here #
####################
import logging
from raven import Client
from loggerglue.server import SyslogServer, SyslogHandler
client = Client(dsn=SENTRY_DSN)
PRIVAL_SEVERITY = {
0 : logging.CRITICAL,
1 : logging.CRITICAL,
2 : logging.CRITICAL,
3 : logging.ERROR,
4 : logging.WARNING,
5 : logging.INFO,
6 : logging.INFO,
7 : logging.DEBUG,
}
PRIVAL_FACILITY = {
0 : "LOG_KERN",
1 : "LOG_USER",
2 : "LOG_MAIL",
3 : "LOG_DAEMON",
4 : "LOG_AUTH",
5 : "LOG_SYSLOG",
6 : "LOG_LPR",
7 : "LOG_NEWS",
8 : "LOG_UUCP",
9 : "LOG_CRON",
10 : "LOG_AUTHPRIV",
16 : "LOG_LOCAL0",
17 : "LOG_LOCAL1",
18 : "LOG_LOCAL2",
19 : "LOG_LOCAL3",
20 : "LOG_LOCAL4",
21 : "LOG_LOCAL5",
22 : "LOG_LOCAL6",
23 : "LOG_LOCAL7",
}
def prival(prival):
sev_num = prival % 8
fac_num = (prival - sev_num) / 8
return (PRIVAL_SEVERITY[sev_num], PRIVAL_FACILITY[fac_num])
class SimpleHandler(SyslogHandler):
def handle_entry(self, entry):
if entry.prival % 8 > SENTRY_LEVEL:
return
level, fac = prival(entry.prival)
client.name = entry.hostname
msg = entry.msg.split(':')
data = {'level': level, "culprit" : ".".join([fac, entry.app_name]),
'logger' : msg[3]}
client.capture('Message', message=':'.join(msg[4:]),
date=entry.timestamp, data=data, extra={
'Server': msg[0],
'Database': msg[1],
})
s = SyslogServer((SERVER_IP, SERVER_PORT), SimpleHandler)
s.serve_forever()
; copy this file under /etc/supervisor
[program:rslog2senry]
user=openerp
command=python rsyslog2sentry.py
directory=/home/openerp/bin
environment=PATH="/home/openerp/.virtualenvs/v7_openerp/bin"
autostart=true
autorestart=true
stdout_logfile=/var/log/rsyslog2sentry.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment