Merge lp:~thomir-deactivatedaccount/adt-cloud-service/trunk-fix-logging into lp:adt-cloud-service

Proposed by Thomi Richards
Status: Merged
Approved by: Thomi Richards
Approved revision: 25
Merged at revision: 21
Proposed branch: lp:~thomir-deactivatedaccount/adt-cloud-service/trunk-fix-logging
Merge into: lp:adt-cloud-service
Diff against target: 161 lines (+57/-7)
6 files modified
README.rst (+8/-0)
adt-service.conf (+5/-7)
adt_cloud_service/__init__.py (+34/-0)
adt_cloud_service/queue.py (+4/-0)
adt_cloud_service/v1.py (+5/-0)
requirements.txt (+1/-0)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/adt-cloud-service/trunk-fix-logging
Reviewer Review Type Date Requested Status
Celso Providelo (community) Approve
Paul Larson Approve
Review via email: mp+253591@code.launchpad.net

Commit message

Sort out logging.

Description of the change

Sort out logging for the a-c-w:

 - we keep a separate application log.
  - we try and write to 'logs/app.log'
  - we fall back to stderr if the logs directory does not exist.
 - we configure the logstash handler if our config file contains the 'logstash' section.
 - we log every request that contains missing parameters, and every time we enqueue something to rabbit.

To post a comment you must log in.
Revision history for this message
Joe Talbott (joetalbott) wrote :

One question in-line.

Revision history for this message
Paul Larson (pwlars) wrote :

Neat, I didn't know you could send directly to logstash like that. Looks good.

review: Approve
Revision history for this message
Celso Providelo (cprov) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'README.rst'
2--- README.rst 2015-03-16 20:56:36 +0000
3+++ README.rst 2015-03-19 22:03:02 +0000
4@@ -30,6 +30,14 @@
5 [image_mapper]
6 url = http://127.0.0.1:1234
7
8+Optionally, add a `[logstash]` section, which will enable the logstash logging handler::
9+
10+ [logstash]
11+ host = localhost
12+ port = 5959
13+ version = 1
14+
15+
16 Running::
17
18 $ gunicorn adt_cloud_service:app
19
20=== modified file 'adt-service.conf'
21--- adt-service.conf 2015-03-16 20:56:36 +0000
22+++ adt-service.conf 2015-03-19 22:03:02 +0000
23@@ -1,6 +1,6 @@
24 # `adt-cloud-worker` configuration file.
25 [image_mapper]
26-url = http://127.0.0.1:1234
27+url = http://127.0.0.1:5000
28
29 [amqp]
30 uris = amqp://guest:guest@localhost:5672//
31@@ -8,9 +8,7 @@
32 [swift]
33 public_url = http://some.swift.server.com
34
35-[nova]
36-os_username = foo
37-os_tenant_name = foo_project
38-os_password = xxx
39-os_auth_url = http://172.20.161.138:5000/v2.0/
40-os_region_name = bot_prototype
41+[logstash]
42+host = localhost
43+port = 5959
44+version = 1
45
46=== modified file 'adt_cloud_service/__init__.py'
47--- adt_cloud_service/__init__.py 2015-03-18 18:59:21 +0000
48+++ adt_cloud_service/__init__.py 2015-03-19 22:03:02 +0000
49@@ -16,8 +16,11 @@
50 #
51
52 import configparser
53+import logging
54+import os.path
55
56 import flask
57+import logstash
58
59 from adt_cloud_service import (
60 errors,
61@@ -33,6 +36,8 @@
62 config = configparser.ConfigParser()
63 config.read('adt-service.conf')
64
65+ configure_logging(config)
66+
67 app = flask.Flask('adt_cloud_service')
68
69 app.add_url_rule(
70@@ -70,4 +75,33 @@
71 return app
72
73
74+def configure_logging(config):
75+ root_logger = logging.getLogger()
76+ root_logger.setLevel(logging.INFO)
77+
78+ log_path = os.path.abspath(os.path.join(__file__, '../../logs/app.log'))
79+ log_dir = os.path.dirname(log_path)
80+ handler = None
81+ if os.path.exists(log_dir):
82+ handler = logging.FileHandler(log_path)
83+ else:
84+ print("'logs' directory '{}' does not exist, using stderr for app log.".format(log_dir))
85+ handler = logging.StreamHandler()
86+ handler.setFormatter(
87+ logging.Formatter(
88+ '%(asctime)s %(name)s %(levelname)s: %(message)s'
89+ )
90+ )
91+ root_logger.addHandler(handler)
92+
93+ if 'logstash' in config:
94+ root_logger.addHandler(
95+ logstash.LogstashHandler(
96+ config['logstash']['host'],
97+ int(config['logstash']['port']),
98+ int(config['logstash']['version'])
99+ )
100+ )
101+
102+
103 app = create_flask_app()
104
105=== modified file 'adt_cloud_service/queue.py'
106--- adt_cloud_service/queue.py 2015-03-17 02:47:19 +0000
107+++ adt_cloud_service/queue.py 2015-03-19 22:03:02 +0000
108@@ -21,6 +21,9 @@
109 from kombu.pools import producers
110 from kombu.common import uuid
111
112+import logging
113+logger = logging.getLogger(__name__)
114+
115
116 class RabbitQueuer(object):
117 """A class that can send things to the rabbit queue. An instance of this
118@@ -78,4 +81,5 @@
119 exchange=self.adt_exchange,
120 routing_key=routing_key,
121 declare=declare)
122+ logger.info("Submitted payload %r", payload_copy, extra=payload_copy)
123 return payload_copy['request_id']
124
125=== modified file 'adt_cloud_service/v1.py'
126--- adt_cloud_service/v1.py 2015-03-18 20:20:47 +0000
127+++ adt_cloud_service/v1.py 2015-03-19 22:03:02 +0000
128@@ -17,12 +17,16 @@
129
130 """Version 1 restful service web code."""
131
132+import logging
133 import urllib.parse
134
135 from flask import current_app, request, url_for, jsonify
136
137 from adt_cloud_service.errors import MissingRequestParameters
138
139+logger = logging.getLogger(__name__)
140+
141+
142 def test_new():
143 required_keys = {
144 'package-name',
145@@ -32,6 +36,7 @@
146 }
147 missing = required_keys.difference(set(request.form.keys()))
148 if missing:
149+ logger.error("Got request %r with missing keys %r", dict(request.form), missing)
150 raise MissingRequestParameters(missing)
151
152 rabbit_request = dict()
153
154=== modified file 'requirements.txt'
155--- requirements.txt 2015-03-12 18:56:59 +0000
156+++ requirements.txt 2015-03-19 22:03:02 +0000
157@@ -1,3 +1,4 @@
158 flask==0.10.1
159 gunicorn==19.3.0
160 kombu==3.0.24
161+python-logstash==0.4.2

Subscribers

People subscribed via source and target branches

to all changes: