Skip to content

Commit 603c537

Browse files
committed
Merge pull request #3500 from Joshua-Anderson/limit-logs
feat(controller): add a option to limit the number of log lines
2 parents c32b775 + c27fc9c commit 603c537

6 files changed

Lines changed: 33 additions & 5 deletions

File tree

client/deis.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,20 @@ def apps_logs(self, args):
633633
Options:
634634
-a --app=<app>
635635
the uniquely identifiable name for the application.
636+
-n --lines=<lines>
637+
the number of lines to display
636638
"""
637639
app = args.get('--app')
638640
if not app:
639641
app = self._session.app
640-
response = self._dispatch('get',
641-
"/v1/apps/{}/logs".format(app))
642+
643+
url = "/v1/apps/{}/logs".format(app)
644+
645+
log_lines = args.get('--lines')
646+
if log_lines:
647+
url += "?log_lines={}".format(log_lines)
648+
649+
response = self._dispatch('get', url)
642650
if response.status_code == requests.codes.ok:
643651
# strip the last newline character
644652
for line in response.json().split('\n')[:-1]:

controller/api/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,12 @@ def _default_scale(self, user, release):
337337

338338
self.scale(user, structure)
339339

340-
def logs(self):
340+
def logs(self, log_lines):
341341
"""Return aggregated log data for this application."""
342342
path = os.path.join(settings.DEIS_LOG_DIR, self.id + '.log')
343343
if not os.path.exists(path):
344344
raise EnvironmentError('Could not locate logs')
345-
data = subprocess.check_output(['tail', '-n', str(settings.LOG_LINES), path])
345+
data = subprocess.check_output(['tail', '-n', log_lines, path])
346346
return data
347347

348348
def run(self, user, command):

controller/api/tests/test_app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ def test_app_actions(self):
121121
HTTP_AUTHORIZATION='token {}'.format(self.token))
122122
self.assertEqual(response.status_code, 200)
123123
self.assertEqual(response.data, FAKE_LOG_DATA)
124+
125+
# test with log_lines
126+
response = self.client.get(url + "?log_lines=1",
127+
HTTP_AUTHORIZATION='token {}'.format(self.token))
128+
self.assertEqual(response.status_code, 200)
129+
self.assertEqual(response.data, FAKE_LOG_DATA.splitlines(True)[4])
130+
124131
os.remove(path)
125132
# TODO: test run needs an initial build
126133

controller/api/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def scale(self, request, **kwargs):
150150
def logs(self, request, **kwargs):
151151
app = self.get_object()
152152
try:
153-
return Response(app.logs(), status=status.HTTP_200_OK, content_type='text/plain')
153+
return Response(app.logs(request.query_params.get('log_lines',
154+
str(settings.LOG_LINES))),
155+
status=status.HTTP_200_OK, content_type='text/plain')
154156
except EnvironmentError:
155157
return Response("No logs for {}".format(app.id),
156158
status=status.HTTP_204_NO_CONTENT,

docs/reference/api-v1.3.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ What's New
1313
----------
1414

1515
**New!** ``/users`` endpoint for listing users
16+
**New!** ``/logs`` endpoint now has a option for limiting the number of log lines returned
1617

1718

1819
Authentication
@@ -258,6 +259,12 @@ Example Request:
258259
Host: deis.example.com
259260
Authorization: token abc123
260261
262+
Optional URL Query Parameters:
263+
264+
.. code-block:: console
265+
266+
?log_lines=
267+
261268
Example Response:
262269

263270
.. code-block:: console

tests/apps_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
appsRunCmd = "apps:run echo Hello, 世界"
1919
appsOpenCmd = "apps:open --app={{.AppName}}"
2020
appsLogsCmd = "apps:logs --app={{.AppName}}"
21+
appsLogsLimitCmd = "apps:logs --app={{.AppName}} -n 1"
2122
appsInfoCmd = "apps:info --app={{.AppName}}"
2223
appsDestroyCmd = "apps:destroy --app={{.AppName}} --confirm={{.AppName}}"
2324
appsDestroyCmdNoApp = "apps:destroy --confirm={{.AppName}}"
@@ -97,6 +98,9 @@ func appsLogsTest(t *testing.T, params *utils.DeisTestConfig) {
9798
utils.CurlApp(t, *params)
9899
utils.Execute(t, cmd, params, false, "created initial release")
99100
utils.Execute(t, cmd, params, false, "listening on 5000...")
101+
102+
utils.Execute(t, appsLogsLimitCmd, params, false, "")
103+
100104
if err := utils.Chdir(".."); err != nil {
101105
t.Fatal(err)
102106
}

0 commit comments

Comments
 (0)