Skip to content

Commit

Permalink
Add the minimum log level for sentry messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Apr 10, 2019
1 parent 0705372 commit 07dba3c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sentrylogs/bin/sentrylogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def get_command_line_args():
help='Which logs to follow, default ALL')
parser.add_argument('--nginxerrorpath', '-n', default=None,
help='Nginx error log path')
parser.add_argument('--loglevel', '-l', default=None,
help='Minimum log level to send to sentry')

return parser.parse_args()

Expand All @@ -50,6 +52,10 @@ def process_arguments(args):
print('Using the Nginx error log path %s' % args.nginxerrorpath)
os.environ['NGINX_ERROR_PATH'] = args.nginxerrorpath

if args.loglevel:
print('Using the sentry log level %s' % args.loglevel)
os.environ['SENTRY_LOG_LEVEL'] = args.loglevel

from ..conf import settings # noqa; pylint: disable=unused-variable

if args.daemonize:
Expand Down
15 changes: 15 additions & 0 deletions sentrylogs/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@

import sentry_sdk

SENTRY_LOG_LEVELS = (
"debug",
"info",
"warning",
"error",
"fatal",
)

# absolute path to nginx error .log file
NGINX_ERROR_PATH = os.environ.get('NGINX_ERROR_PATH',
'/var/log/nginx/error.log')

SENTRY_LOG_LEVEL = os.environ.get('SENTRY_LOG_LEVEL', 'error')
if SENTRY_LOG_LEVEL not in SENTRY_LOG_LEVELS:
message = "Log level '{}' is invalid. Valid options are {}".format(
SENTRY_LOG_LEVEL,
SENTRY_LOG_LEVELS,
)
raise SystemExit(message)

SENTRY_DSN = os.environ.get('SENTRY_DSN', None)
sentry_sdk.init(SENTRY_DSN)
5 changes: 5 additions & 0 deletions sentrylogs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
"""
from sentry_sdk import capture_message, configure_scope

from .conf.settings import SENTRY_LOG_LEVEL, SENTRY_LOG_LEVELS


def send_message(message, level, data):
"""Send a message to the Sentry server"""
# Only send messages for desired log level
if SENTRY_LOG_LEVELS.index(level) < SENTRY_LOG_LEVELS.index(SENTRY_LOG_LEVEL):
return
with configure_scope() as scope:
for key, value in data.items():
scope.set_extra(key, value)
Expand Down

0 comments on commit 07dba3c

Please sign in to comment.