Logging added

This commit is contained in:
Alexander Kislitsky 2014-09-18 17:31:32 +04:00
parent 77817b497c
commit 0bd854f0ca
12 changed files with 79 additions and 75 deletions

View File

@ -7,7 +7,6 @@ app = flask.Flask(__name__)
app.config['JSONSCHEMA_DIR'] = os.path.join(app.root_path, 'schemas')
flask_jsonschema.JsonSchema(app)
# Application errors handling
from collector.api import error_handling

View File

@ -18,11 +18,13 @@ def handle_response(http_code, *path):
@wraps(fn)
def decorated(*args, **kwargs):
response = fn(*args, **kwargs)
print "### handle_response", response
current_app.logger.debug("Processing response: {}".format(response))
if current_app.config.get('VALIDATE_RESPONSE', False) and path:
print "### validating response"
jschema = current_app.extensions.get('jsonschema')
jsonschema.validate(response, jschema.get_schema(path))
current_app.logger.debug("Validating response: {}".format(response))
jsonschema_ext = current_app.extensions.get('jsonschema')
jsonschema.validate(response, jsonschema_ext.get_schema(path))
current_app.logger.debug("Response validated: {}".format(response))
current_app.logger.debug("Response processed: {}".format(response))
return jsonify(response), http_code
return decorated
return wrapper

View File

@ -1,11 +1,24 @@
import logging
import os
class Production(object):
DEBUG = False
PORT = 5000
HOST = 'localhost'
VALIDATE_RESPONSE = False
LOG_FILE = '/var/log/fuel-stat/collector.log'
LOG_LEVEL = logging.ERROR
LOG_ROTATION = False
class Testing(Production):
DEBUG = True
HOST = '0.0.0.0'
VALIDATE_RESPONSE = True
LOG_FILE = os.path.realpath(os.path.join(
os.path.dirname(__file__), '..', 'test', 'logs', 'collector.log'))
LOG_LEVEL = logging.DEBUG
LOG_ROTATION = True
LOG_FILE_SIZE = 2048000
LOG_FILES_COUNT = 5

View File

@ -7,25 +7,25 @@ from collector.api.app import app
@app.errorhandler(400)
def bad_request(error):
print "### bad request"
app.logger.error("Bad request: {}".format(error))
return make_response(jsonify({'status': 'error', 'message': '{}'.format(error)}), 400)
@app.errorhandler(404)
def not_found(error):
print "### not_found"
app.logger.error("Not found: {}".format(error))
return make_response(jsonify({'status': 'error', 'message': '{}'.format(error)}), 404)
@app.errorhandler(flask_jsonschema.ValidationError)
def validation_error(error):
print "### validation_error"
app.logger.error("Validation error: {}".format(error))
return make_response(jsonify({'status': 'error', 'message': '{}'.format(error)}), 400)
@app.errorhandler(500)
def server_error(error):
print "### server_error"
app.logger.error("Server error: {}".format(error))
return make_response(jsonify({'status': 'error', 'message': '{0}: {1}'.format(error.__class__.__name__, error)}), 500)

View File

@ -0,0 +1,29 @@
from logging import FileHandler
from logging import Formatter
from logging.handlers import RotatingFileHandler
from collector.api.app import app
def get_file_handler():
if app.config.get('LOG_ROTATION'):
file_handler = RotatingFileHandler(app.config.get('LOG_FILE'),
maxBytes=app.config.get('LOG_FILE_SIZE'),
backupCount='LOG_FILES_COUNT')
else:
file_handler = FileHandler(app.config.get('LOG_FILE'))
file_handler.setLevel(app.config.get('LOG_LEVEL'))
formatter = get_formatter()
file_handler.setFormatter(formatter)
return file_handler
def get_formatter():
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
LOG_FORMAT = "%(asctime)s.%(msecs)03d %(levelname)s " \
"[%(thread)x] (%(module)s) %(message)s"
return Formatter(fmt=LOG_FORMAT, datefmt=DATE_FORMAT)
def init_logger():
app.logger.addHandler(get_file_handler())

View File

@ -10,9 +10,7 @@ from collector.api.common.util import handle_response
@validate_request('action_logs', 'post_request')
@handle_response(201, 'action_logs', 'post_response')
def post():
print "### request.data", request.data
print "### request.headers", request.headers
print "### app.config.VALIDATE_RESPONSE", app.config.get('VALIDATE_RESPONSE', False)
app.logger.debug("action_logs post request: {}".format(request.json))
return {'status': 'ok'}

View File

@ -1,46 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"post_req": {
"definitions": {
"positive_number": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
},
"type": "object",
"properties": {
"id": { "$ref": "#/definitions/positive_number" },
"title": {},
"author": {}
},
"required": ["id"]
},
"type": "object",
"def": {
"status_resp": {
"type": "object",
"properties": {
"status": {"enum": ["ok", "error"]},
"message": {"type": "string"}
},
"required": ["status"]
}
},
"post_resp": {
"allOf": [
{
"$ref": "#/def/status_resp"
},
{
"properties": {
"message": {"type": "string"}
}
}
]
}
}

View File

@ -2,6 +2,7 @@ from flask import json
from unittest2.case import TestCase
from collector.api import app
from collector.api.log import init_logger
class BaseTest(TestCase):
@ -9,6 +10,7 @@ class BaseTest(TestCase):
@classmethod
def setUpClass(cls):
app.app.config.from_object('collector.api.config.Testing')
init_logger()
def setUp(self):
self.client = app.app.test_client()
@ -17,7 +19,17 @@ class BaseTest(TestCase):
return self.client.post(url, data=json.dumps(data),
content_type='application/json')
def check_response_ok(self, resp, code=200):
self.assertEquals(code, resp.status_code)
d = json.loads(resp.data)
self.assertEquals('ok', d['status'])
def check_response_error(self, resp, code):
self.assertEquals(code, resp.status_code)
d = json.loads(resp.data)
self.assertEquals('error', d['status'])
def test_unknown_resource(self):
resp = self.client.get('/xxx')
print "### resp", resp
self.check_response_error(resp, 404)

View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

View File

@ -4,14 +4,4 @@ from collector.test.base import BaseTest
class TestActionLogs(BaseTest):
def test_post(self):
resp = self.post('/api/v1/action_logs', {'id': 1})
print "### resp.status_code", resp.status_code
print "### resp.headers", resp.headers
print "### resp.data", resp.data
print "### resp", resp
# resp = self.client.get('/api/v1/action_logs')
# # from collector.api.app import api
# # print "### resources", api.resources
# print "### resp.status_code", resp.status_code
# print "### resp.headers", resp.headers
# print "### resp.data", resp.data
self.check_response_ok(resp, code=201)

View File

@ -1,5 +0,0 @@
#!/usr/bin/env python
from collector.api.app import app
app.run(debug=True)

8
collector/run_test_app.py Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env python
from collector.api.app import app
from collector.api.log import init_logger
app.config.from_object('collector.api.config.Testing')
init_logger()
app.run()