Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #167 from alphagov/logstash_json
Browse files Browse the repository at this point in the history
Add json log formatter
  • Loading branch information
jabley committed Oct 25, 2013
2 parents 25ced9b + 777b865 commit 5589bdf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assets/*
.coverage
build
coverage.xml
*.log
*.log*
nosetests.xml
pep8.out
tmp/*
Expand Down
11 changes: 11 additions & 0 deletions backdrop/core/log_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from logging import FileHandler
from logstash_formatter import LogstashFormatter
import logging
from flask import request

Expand All @@ -11,11 +12,21 @@ def get_log_file_handler(path, log_level=logging.DEBUG):
return handler


def get_json_log_handler(path):
handler = FileHandler(path)
formatter = LogstashFormatter()
handler.setFormatter(formatter)
return handler


def set_up_logging(app, name, env):
log_level = logging._levelNames[app.config['LOG_LEVEL']]
app.logger.addHandler(
get_log_file_handler("log/%s.log" % env, log_level)
)
app.logger.addHandler(
get_json_log_handler("log/%s.log.json" % env)
)
app.logger.setLevel(log_level)
app.logger.info("Backdrop %s API logging started" % name)

Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ argh==0.23.3
Flask==0.10.1
Flask-FeatureFlags==0.3
gunicorn==18.0
invoke
logstash-formatter==0.5.5
pip==1.4.1
pymongo==2.6.1
python-dateutil==2.1
pytz==2013b
statsd==2.0.3
rauth==0.5.4
statsd==2.0.3
xlrd==0.9.2
invoke
30 changes: 30 additions & 0 deletions tests/core/test_json_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from hamcrest import assert_that, has_entries
import os
import logging
import json
from backdrop.core import log_handler
import unittest
from flask import Flask


class TestJsonLogging(unittest.TestCase):

def setUp(self):
self.app = Flask('json_test_app')
self.logger = self.app.logger
self.app.config['LOG_LEVEL'] = logging.DEBUG

def test_json_log_written_when_logger_called(self):

log_handler.set_up_logging(self.app, 'json_test', 'json_test')
self.logger.info('Writing out JSON formatted logs m8')

with open('log/json_test.log.json') as log_file:
data = json.loads(log_file.readlines()[-1])

assert_that(data, has_entries({
'@message': 'Writing out JSON formatted logs m8'
}))

# Only remove file if assertion passes
os.remove('log/json_test.log.json')

0 comments on commit 5589bdf

Please sign in to comment.