Skip to content

Commit 672a9ee

Browse files
committed
Add service_logs command to APIClient and logs command to models.Service
Signed-off-by: Joffrey F <[email protected]>
1 parent 80046f1 commit 672a9ee

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

docker/api/service.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,55 @@ def services(self, filters=None):
166166
url = self._url('/services')
167167
return self._result(self._get(url, params=params), True)
168168

169+
@utils.minimum_version('1.25')
170+
def service_logs(self, service, details=False, follow=False, stdout=False,
171+
stderr=False, since=0, timestamps=False, tail='all',
172+
is_tty=None):
173+
"""
174+
Get log stream for a service.
175+
Note: This endpoint works only for services with the ``json-file``
176+
or ``journald`` logging drivers.
177+
178+
Args:
179+
service (str): ID or name of the container
180+
details (bool): Show extra details provided to logs.
181+
Default: ``False``
182+
follow (bool): Keep connection open to read logs as they are
183+
sent by the Engine. Default: ``False``
184+
stdout (bool): Return logs from ``stdout``. Default: ``False``
185+
stderr (bool): Return logs from ``stderr``. Default: ``False``
186+
since (int): UNIX timestamp for the logs staring point.
187+
Default: 0
188+
timestamps (bool): Add timestamps to every log line.
189+
tail (string or int): Number of log lines to be returned,
190+
counting from the current end of the logs. Specify an
191+
integer or ``'all'`` to output all log lines.
192+
Default: ``all``
193+
is_tty (bool): Whether the service's :py:class:`ContainerSpec`
194+
enables the TTY option. If omitted, the method will query
195+
the Engine for the information, causing an additional
196+
roundtrip.
197+
198+
Returns (generator): Logs for the service.
199+
"""
200+
params = {
201+
'details': details,
202+
'follow': follow,
203+
'stdout': stdout,
204+
'stderr': stderr,
205+
'since': since,
206+
'timestamps': timestamps,
207+
'tail': tail
208+
}
209+
210+
url = self._url('/services/{0}/logs', service)
211+
res = self._get(url, params=params, stream=True)
212+
if is_tty is None:
213+
is_tty = self.inspect_service(
214+
service
215+
)['Spec']['TaskTemplate']['ContainerSpec'].get('TTY', False)
216+
return self._get_result_tty(True, res, is_tty)
217+
169218
@utils.minimum_version('1.24')
170219
def tasks(self, filters=None):
171220
"""

docker/models/services.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ def update(self, **kwargs):
7777
**create_kwargs
7878
)
7979

80+
def logs(self, **kwargs):
81+
"""
82+
Get log stream for the service.
83+
Note: This method works only for services with the ``json-file``
84+
or ``journald`` logging drivers.
85+
86+
Args:
87+
details (bool): Show extra details provided to logs.
88+
Default: ``False``
89+
follow (bool): Keep connection open to read logs as they are
90+
sent by the Engine. Default: ``False``
91+
stdout (bool): Return logs from ``stdout``. Default: ``False``
92+
stderr (bool): Return logs from ``stderr``. Default: ``False``
93+
since (int): UNIX timestamp for the logs staring point.
94+
Default: 0
95+
timestamps (bool): Add timestamps to every log line.
96+
tail (string or int): Number of log lines to be returned,
97+
counting from the current end of the logs. Specify an
98+
integer or ``'all'`` to output all log lines.
99+
Default: ``all``
100+
101+
Returns (generator): Logs for the service.
102+
"""
103+
is_tty = self.attrs['Spec']['TaskTemplate']['ContainerSpec'].get(
104+
'TTY', False
105+
)
106+
return self.client.api.service_logs(self.id, is_tty=is_tty, **kwargs)
107+
80108

81109
class ServiceCollection(Collection):
82110
"""Services on the Docker server."""

0 commit comments

Comments
 (0)