Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit c6c8a7e

Browse files
gets rid of loud logging and sets requirement for Twisted to 15.2.1 which is needed for crosstown_traffic testing
1 parent c1995ac commit c6c8a7e

18 files changed

+179
-143
lines changed

hendrix/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.0'
1+
__version__ = '1.0.1'

hendrix/contrib/async/crosstown_traffic.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
from twisted.internet.threads import deferToThread, deferToThreadPool
22
from twisted.internet import reactor
3+
from twisted.logger import Logger
34

45
import threading
56

6-
import logging
7-
8-
logger = logging.getLogger(__name__)
9-
logger.setLevel(logging.INFO)
107

118
def get_response_for_thread(thread=None):
129

@@ -24,6 +21,8 @@ def get_tasks_to_follow_current_response(thread=None):
2421

2522
class ThroughToYou(object):
2623

24+
log = Logger()
25+
2726
def __init__(self,
2827
same_thread=False,
2928
no_go_status_codes=['5xx', '4xx'],
@@ -40,7 +39,11 @@ def __call__(self, crosstown_task):
4039
self.response = get_response_for_thread()
4140

4241
if not self.no_go:
43-
logger.info("Adding '%s' to crosstown_traffic for %s" % (crosstown_task.__name__, self.response))
42+
self.log.info(
43+
"Adding '{task!r}' to crosstown_traffic for "
44+
"{log_source.response}",
45+
task=crosstown_task
46+
)
4447
self.response.crosstown_tasks.append(self)
4548

4649
def run(self, threadpool):
@@ -71,9 +74,11 @@ def populate_no_go_status_code_list(self):
7174
self.no_go_status_code_list.extend(range(begin, end))
7275

7376
else:
74-
self.no_go_status_code_list.append(no_go_code)
77+
self.no_go_status_code_list.append(no_go_code)
7578

76-
logger.debug("no_go_status_codes are %s" % self.no_go_status_code_list)
79+
self.log.debug(
80+
"no_go_status_codes are {data!r}", data=self.no_go_status_code_list
81+
)
7782

7883
def check_status_code_against_no_go_list(self):
7984
if self.no_go_status_codes:
@@ -83,7 +88,10 @@ def check_status_code_against_no_go_list(self):
8388
self.populate_no_go_status_code_list()
8489

8590
if self.status_code in self.no_go_status_code_list:
86-
logger.info("Flagging no-go becasue status code is %s" % self.status_code)
91+
self.log.info(
92+
"Flagging no-go becasue status code is "
93+
"{log_source.status_code}"
94+
)
8795
self.no_go = True
8896

8997

hendrix/defaults.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import os
2+
3+
14
CACHE_PORT = 8080
25
HTTP_PORT = 8000
36
HTTPS_PORT = 4430
47

58

69
DEFAULT_MAX_AGE = 3600
10+
11+
DEFAULT_LOG_PATH = os.path.dirname(__file__)
12+
DEFAULT_LOG_FILE = os.path.join(DEFAULT_LOG_PATH, 'default-hendrix.log')

hendrix/deploy/base.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@ def __init__(self, action='start', options={}, reactor=reactor):
3535
# because running the management command overrides self.options['wsgi']
3636
if self.options['wsgi']:
3737
if hasattr(self.options['wsgi'], '__call__'):
38-
# If it has a __call__, we assume that it is the application object itself.
38+
# If it has a __call__, we assume that it is the application
39+
# object itself.
3940
self.application = self.options['wsgi']
4041
try:
41-
self.options['wsgi_app_name'] = "%s.%s" % (self.application.__module__, self.application.__name__)
42+
self.options['wsgi'] = "%s.%s" % (
43+
self.application.__module__, self.application.__name__
44+
)
4245
except AttributeError:
43-
self.options['wsgi_app_name'] = self.application.__class__.__name__
46+
self.options['wsgi'] = self.application.__class__.__name__
4447
else:
45-
# Otherwise, we'll try to discern an application in the belief that this is a dot path.
48+
# Otherwise, we'll try to discern an application in the belief
49+
# that this is a dot path.
4650
wsgi_dot_path = self.options['wsgi']
47-
self.application = HendrixDeploy.importWSGI(wsgi_dot_path) # will raise AttributeError if we can't import it.
48-
self.options['wsgi_app_name'] = wsgi_dot_path
51+
# will raise AttributeError if we can't import it.
52+
self.application = HendrixDeploy.importWSGI(wsgi_dot_path)
4953
self.use_settings = False
5054
else:
5155
os.environ['DJANGO_SETTINGS_MODULE'] = self.options['settings']

hendrix/logger.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from .defaults import DEFAULT_LOG_FILE
2+
from twisted.logger import (
3+
ILogObserver, jsonFileLogObserver, FilteringLogObserver,
4+
LogLevelFilterPredicate, LogLevel, globalLogPublisher
5+
)
6+
from zope.interface import provider
7+
8+
import io
9+
10+
11+
@provider(ILogObserver)
12+
def hendrixObserver(path=DEFAULT_LOG_FILE, log_level=LogLevel.warn):
13+
json_observer = jsonFileLogObserver(
14+
io.open(path, 'a')
15+
)
16+
return FilteringLogObserver(
17+
json_observer,
18+
[LogLevelFilterPredicate(log_level), ]
19+
)
20+
21+
22+
globalLogPublisher.addObserver(hendrixObserver(log_level=LogLevel.debug))

hendrix/options.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import os
2-
31
from hendrix import defaults
42
from optparse import make_option, OptionParser
3+
import os
54

65

76
def cleanOptions(options):
@@ -55,7 +54,7 @@ def cleanOptions(options):
5554
'--log',
5655
dest='log',
5756
type=str,
58-
default=os.path.join(os.path.dirname(__file__), 'hendrix.log'),
57+
default=os.path.join(defaults.DEFAULT_LOG_PATH, 'hendrix.log'),
5958
help=(
6059
'file path to where the log files should live '
6160
'[default: $PYTHON_PATH/lib/.../hendrix/hendrix.log]'

hendrix/resources.py

+24-27
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,34 @@
77
from twisted.web import resource, static
88
from twisted.web.server import NOT_DONE_YET
99
from twisted.web.wsgi import WSGIResource, _WSGIResponse
10+
from twisted.logger import Logger
1011

11-
import logging
1212
import chalk
13-
from twisted.internet.threads import deferToThread
1413
import threading
15-
import uuid
16-
17-
logger = logging.getLogger(__name__)
18-
19-
# thread_list = [] # Debug
14+
import uuid
2015

2116

2217
class HendrixWSGIResponse(_WSGIResponse):
2318

19+
log = Logger()
20+
2421
def __init__(self, *args, **kwargs):
2522
self.crosstown_tasks = []
2623
return super(HendrixWSGIResponse, self).__init__(*args, **kwargs)
2724

2825
def run(self, *args, **kwargs):
2926
self.thread = threading.current_thread()
30-
# thread_list.append(self.thread) # Debug
31-
# logger.debug("Assigning %s as the current response for thread %s" % (self, self.thread))
3227
self.thread.response_object = self
3328
self.request.setHeader('server', 'hendrix/Twisted')
3429
ran = super(HendrixWSGIResponse, self).run(*args, **kwargs)
3530
self.follow_response_tasks()
3631
return ran
37-
38-
def follow_response_tasks(self):
3932

33+
def follow_response_tasks(self):
4034
for task in self.crosstown_tasks:
41-
logger.info("Processing crosstown task: '%s'" % task)
4235
task.run(self.threadpool)
4336

37+
4438
class LoudWSGIResponse(HendrixWSGIResponse):
4539

4640
def startResponse(self, status, headers, excInfo=None):
@@ -55,19 +49,20 @@ def startResponse(self, status, headers, excInfo=None):
5549
responseInColor, self.request, status, headers
5650
)
5751
return self.write
58-
52+
5953

6054
class HendrixWSGIResource(WSGIResource):
61-
55+
6256
ResponseClass = HendrixWSGIResponse
63-
64-
def render(self, request):
57+
58+
def render(self, request):
6559
response = self.ResponseClass(
66-
self._reactor, self._threadpool, self._application, request)
60+
self._reactor, self._threadpool, self._application, request
61+
)
6762
response.start()
6863
return NOT_DONE_YET
6964

70-
65+
7166
class DevWSGIResource(HendrixWSGIResource):
7267

7368
ResponseClass = LoudWSGIResponse
@@ -86,12 +81,16 @@ class HendrixResource(resource.Resource):
8681
to ensure that django always gets the full path.
8782
"""
8883

84+
log = Logger()
85+
8986
def __init__(self, reactor, threads, application, loud=False):
9087
resource.Resource.__init__(self)
9188
if loud:
9289
self.wsgi_resource = DevWSGIResource(reactor, threads, application)
9390
else:
94-
self.wsgi_resource = HendrixWSGIResource(reactor, threads, application)
91+
self.wsgi_resource = HendrixWSGIResource(
92+
reactor, threads, application
93+
)
9594

9695
def getChild(self, name, request):
9796
"""
@@ -137,11 +136,12 @@ def putNamedChild(self, res):
137136

138137
name = parts[-1] # get the path part that we care about
139138
if children.get(name):
140-
logger.warning(
141-
'A resource already exists at this path. Check '
142-
'your resources list to ensure each path is '
143-
'unique. The previous resource will be overridden.'
144-
)
139+
# self.log.warn(
140+
# 'A resource already exists at this path. Check '
141+
# 'your resources list to ensure each path is '
142+
# 'unique. The previous resource will be overridden.'
143+
# )
144+
pass
145145
parent.putChild(name, res)
146146
except AttributeError:
147147
# raise an attribute error if the resource `res` doesn't contain
@@ -215,9 +215,6 @@ def DjangoStaticResource(path, rel_url='static'):
215215
rel_url = rel_url.strip('/')
216216
StaticFilesResource = MediaResource(path)
217217
StaticFilesResource.namespace = rel_url
218-
chalk.green(
219-
"Adding media resource for URL '%s' at path '%s'" % (rel_url, path)
220-
)
221218
return StaticFilesResource
222219

223220

hendrix/runtests.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, sys
1+
import os
2+
import sys
23

34
# begin chdir armor
45
sys.path[:] = map(os.path.abspath, sys.path)
@@ -8,4 +9,4 @@
89
sys.argv.append("test")
910

1011
from twisted.scripts.trial import run
11-
run()
12+
run()

hendrix/services.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
from .resources import HendrixResource
33
from twisted.application import internet, service
44
from twisted.internet import reactor
5+
from twisted.logger import Logger
56
from twisted.python.threadpool import ThreadPool
67
from twisted.web import server
78

8-
import logging
9-
10-
logger = logging.getLogger(__name__)
11-
129

1310
class HendrixService(service.MultiService):
1411
"""
@@ -19,6 +16,8 @@ class HendrixService(service.MultiService):
1916
'services' refers to a list of twisted Services to add to the collection.
2017
"""
2118

19+
log = Logger()
20+
2221
def __init__(
2322
self, application, port=80, resources=None, services=None,
2423
loud=False):
@@ -27,10 +26,10 @@ def __init__(
2726
# Create, start and add a thread pool service, which is made available
2827
# to our WSGIResource within HendrixResource
2928
threads = ThreadPool(name="Hendrix Service")
30-
29+
3130
# Testing threads 1-2-3
3231
threads.adjustPoolsize(3, 5)
33-
32+
3433
reactor.addSystemEventTrigger('after', 'shutdown', threads.stop)
3534
ThreadPoolService(threads).setServiceParent(self)
3635

0 commit comments

Comments
 (0)