Merge lp:~thomir-deactivatedaccount/core-image-publisher/trunk-configure-logging into lp:core-image-publisher

Proposed by Thomi Richards
Status: Merged
Approved by: Celso Providelo
Approved revision: 7
Merged at revision: 7
Proposed branch: lp:~thomir-deactivatedaccount/core-image-publisher/trunk-configure-logging
Merge into: lp:core-image-publisher
Prerequisite: lp:~thomir-deactivatedaccount/core-image-publisher/trunk-run-service
Diff against target: 108 lines (+48/-7)
4 files modified
README.rst (+9/-3)
core_image_publisher/__init__.py (+36/-2)
core_image_publisher/queue.py (+1/-1)
requirements.txt (+2/-1)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/core-image-publisher/trunk-configure-logging
Reviewer Review Type Date Requested Status
Celso Providelo (community) Approve
Review via email: mp+254177@code.launchpad.net

Commit message

Configure logging for this service, including logstash handling.

Description of the change

Configure logging for this service, including logstash handling.

To post a comment you must log in.
7. By Thomi Richards

Document logstash logging options in README.

Revision history for this message
Celso Providelo (cprov) wrote :

Very good, thanks for working on this.

This pattern works fine with LS+ELK and since it will be available soon in production we can figure out ways to make the logs look sane across services.

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-25 23:55:03 +0000
3+++ README.rst 2015-03-25 23:55:03 +0000
4@@ -54,6 +54,12 @@
5
6 The sample configuration file in 'core-service.conf'::
7
8- [amqp]
9- uris = amqp://guest:guest@localhost:5672//
10-
11+ [amqp]
12+ uris = amqp://guest:guest@localhost:5672//
13+
14+...optionally, add a logstash section, which will turn on the logstash handler::
15+
16+ [logstash]
17+ host = 127.0.0.1
18+ port = 1234
19+ version = 1
20\ No newline at end of file
21
22=== modified file 'core_image_publisher/__init__.py'
23--- core_image_publisher/__init__.py 2015-03-25 23:55:03 +0000
24+++ core_image_publisher/__init__.py 2015-03-25 23:55:03 +0000
25@@ -15,10 +15,12 @@
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #
28
29-import logging
30-
31 import argparse
32 import configparser
33+import logging
34+import os
35+
36+import logstash
37
38 from core_image_publisher.queue import (
39 create_connection_from_config,
40@@ -44,6 +46,37 @@
41 return config
42
43
44+def configure_logging(config):
45+ root_logger = logging.getLogger()
46+ root_logger.setLevel(logging.INFO)
47+
48+ # If there is no ./logs directory, fallback to stderr.
49+ log_path = os.path.abspath(
50+ os.path.join(__file__, '../../logs/core-image-publisher.log'))
51+ log_dir = os.path.dirname(log_path)
52+ if os.path.exists(log_dir):
53+ handler = logging.FileHandler(log_path)
54+ else:
55+ print("'logs' directory '{}' does not exist, using stderr "
56+ "for app log.".format(log_dir))
57+ handler = logging.StreamHandler()
58+
59+ handler.setFormatter(
60+ logging.Formatter(
61+ '%(asctime)s %(name)s %(levelname)s: %(message)s'
62+ )
63+ )
64+ root_logger.addHandler(handler)
65+ if 'logstash' in config:
66+ root_logger.addHandler(
67+ logstash.LogstashHandler(
68+ config['logstash']['host'],
69+ int(config['logstash']['port']),
70+ int(config['logstash']['version'])
71+ )
72+ )
73+
74+
75 def logging_worker(message):
76 logger.info("Got %r", message.payload, extra=message.payload)
77 message.ack()
78@@ -51,6 +84,7 @@
79
80 def main():
81 config = read_config()
82+ configure_logging(config)
83 connection = create_connection_from_config(config)
84 try:
85 with connection:
86
87=== modified file 'core_image_publisher/queue.py'
88--- core_image_publisher/queue.py 2015-03-25 23:55:03 +0000
89+++ core_image_publisher/queue.py 2015-03-25 23:55:03 +0000
90@@ -67,7 +67,7 @@
91 Run requested tests and posts results to the 'adt_results' queue
92 for later checking.
93 """
94- logger.info('Got: {}'.format(body), extra=body)
95+ logger.info('Got: %r', body, extra=body)
96 try:
97 self.on_message_cb(message)
98 except Exception as e:
99
100=== modified file 'requirements.txt'
101--- requirements.txt 2015-03-25 21:36:10 +0000
102+++ requirements.txt 2015-03-25 23:55:03 +0000
103@@ -1,1 +1,2 @@
104-kombu==3.0.24
105\ No newline at end of file
106+kombu==3.0.24
107+python-logstash==0.4.2
108\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: