Skip to content

Commit 6349cd3

Browse files
authored
Merge pull request #1466 from hashicorp/d-logs
Document logs command and API
2 parents 4c2aacd + fbf1d2f commit 6349cd3

File tree

9 files changed

+215
-30
lines changed

9 files changed

+215
-30
lines changed

command/logs.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ General Options:
2828
2929
Logs Specific Options:
3030
31+
-stderr:
32+
Display stderr logs.
33+
3134
-verbose
3235
Show full information.
3336
@@ -39,15 +42,15 @@ Logs Specific Options:
3942
rather to wait for additional output.
4043
4144
-tail
42-
Show the files contents with offsets relative to the end of the file. If no
45+
Show the logs contents with offsets relative to the end of the logs. If no
4346
offset is given, -n is defaulted to 10.
4447
4548
-n
4649
Sets the tail location in best-efforted number of lines relative to the end
47-
of the file.
50+
of the logs.
4851
4952
-c
50-
Sets the tail location in number of bytes relative to the end of the file.
53+
Sets the tail location in number of bytes relative to the end of the logs.
5154
`
5255
return strings.TrimSpace(helpText)
5356
}

website/source/docs/commands/fs.html.md.erb

+8-8
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ drwxrwxr-x 4096 28 Jan 16 05:39 UTC redis/
6868
-rw-rw-r-- 0 28 Jan 16 05:39 UTC redis_exit_status
6969

7070

71-
$ nomad fs redis/local
71+
$ nomad fs eb17e557 redis/local
7272
Mode Size Modified Time Name
7373
-rw-rw-rw- 0 28 Jan 16 05:39 UTC redis.stderr
7474
-rw-rw-rw- 17 28 Jan 16 05:39 UTC redis.stdout
7575

7676

77-
$ nomad fs -stat redis/local/redis.stdout
77+
$ nomad fs -stat eb17e557 redis/local/redis.stdout
7878
Mode Size Modified Time Name
7979
-rw-rw-rw- 17 28 Jan 16 05:39 UTC redis.stdout
8080

8181

82-
$ nomad fs redis/local/redis.stdout
82+
$ nomad fs eb17e557 redis/local/redis.stdout
8383
foobar
8484
baz
8585

86-
$ nomad fs -tail -f -n 3 redis/local/redis.stdout
86+
$ nomad fs -tail -f -n 3 eb17e557 redis/local/redis.stdout
8787
foobar
8888
baz
8989
bam
@@ -92,15 +92,15 @@ bam
9292

9393
## Using Job ID instead of Allocation ID
9494

95-
Passing `-job` into one of the `fs` commands will allow the `fs` command to
96-
randomly select an allocation ID from the specified job.
95+
Setting the `-job` flag causes a random allocation of the specified job to be
96+
selected. Nomad will prefer to select a running allocation ID for the job, but
97+
if no running allocations for the job are found, Nomad will use a dead
98+
allocation.
9799

98100
```
99101
nomad fs -job <job-id> <path>
100102
```
101103

102-
Nomad will prefer to select a running allocation ID for the job, but if no
103-
running allocations for the job are found, Nomad will use a dead allocation.
104104

105105
This can be useful for debugging a job that has multiple allocations, and it's
106106
not really required to use a specific allocation ID.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
layout: "docs"
3+
page_title: "Commands: logs"
4+
sidebar_current: "docs-commands-logs"
5+
description: >
6+
Stream the logs of a task.
7+
---
8+
9+
# Command: logs
10+
11+
The `logs` command displays the log of a given task.
12+
13+
## Usage
14+
15+
```
16+
nomad logs [options] <alloc-id> <task>
17+
```
18+
19+
This commands streams the logs of the given task in the allocation. If the
20+
allocation is only running a single task, the task name can be omitted.
21+
Optionally, the `-job` option may be used in which case a random allocation from
22+
the given job will be chosen.
23+
#
24+
## General Options
25+
26+
<%= general_options_usage %>
27+
28+
## Logs Options
29+
30+
* `-stderr`: Display stderr logs.
31+
32+
* `-verbose`: Display verbose output.
33+
34+
* `-job`: Use a random allocation from the specified job, prefering a running
35+
allocation.
36+
37+
* `-f`: Causes the output to not stop when the end of the logs are reached, but
38+
rather to wait for additional output.
39+
40+
* `-tail`: Show the logs contents with offsets relative to the end of the logs.
41+
If no offset is given, -n is defaulted to 10.
42+
43+
* `-n`: Sets the tail location in best-efforted number of lines relative to the
44+
end of the logs.
45+
46+
* `-c`: Sets the tail location in number of bytes relative to the end of the logs.
47+
48+
## Examples
49+
50+
```
51+
$ nomad logs eb17e557 redis
52+
foobar
53+
baz
54+
bam
55+
56+
$ nomad logs -stderr eb17e557 redis
57+
[ERR]: foo
58+
[ERR]: bar
59+
60+
$ nomad logs -job example
61+
[ERR]: foo
62+
[ERR]: bar
63+
64+
$ nomad logs -tail -n 2 eb17e557 redis
65+
foobar
66+
baz
67+
68+
$ nomad logs -tail -f -n 3 eb17e557 redis
69+
foobar
70+
baz
71+
bam
72+
<blocking>
73+
```
74+
75+
## Using Job ID instead of Allocation ID
76+
77+
Setting the `-job` flag causes a random allocation of the specified job to be
78+
selected. Nomad will prefer to select a running allocation ID for the job, but
79+
if no running allocations for the job are found, Nomad will use a dead
80+
allocation.
81+
82+
```
83+
nomad logs -job <job-id> <task>
84+
```
85+
86+
87+
This can be useful for debugging a job that has multiple allocations, and it's
88+
not really required to use a specific allocation ID.

website/source/docs/http/client-fs.html.md

+89
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,95 @@ allocation was placed.
133133
</dd>
134134
</dl>
135135

136+
<a id="logs"></a>
137+
138+
<dl>
139+
<dt>Description</dt>
140+
<dd>
141+
Stream a tasks stdout/stderr logs.
142+
</dd>
143+
144+
<dt>Method</dt>
145+
<dd>GET</dd>
146+
147+
<dt>URL</dt>
148+
<dd>`/v1/client/fs/logs/<Allocation-ID>`</dd>
149+
150+
<dt>Parameters</dt>
151+
<dd>
152+
<ul>
153+
<li>
154+
<span class="param">task</span>
155+
<span class="param-flags">required</span>
156+
The name of the task inside the allocation to stream logs from.
157+
</li>
158+
<li>
159+
<span class="param">follow</span>
160+
<span class="param-flags">required</span>
161+
A boolean of whether to follow logs.
162+
</li>
163+
<li>
164+
<span class="param">type</span>
165+
Either, "stdout" or "stderr", defaults to "stdout" if omitted.
166+
</li>
167+
<li>
168+
<span class="param">offset</span>
169+
The offset to start streaming from. Defaults to 0.
170+
</li>
171+
<li>
172+
<span class="param">origin</span>
173+
Origin can be either "start" or "end" and applies the offset relative to
174+
either the start or end of the logs respectively. Defaults to "start".
175+
</li>
176+
</ul>
177+
</dd>
178+
179+
<dt>Returns</dt>
180+
<dd>
181+
182+
```
183+
...
184+
{
185+
"File":"alloc/logs/redis.stdout.0",
186+
"Offset":3604480
187+
"Data": "NTMxOTMyCjUzMTkzMwo1MzE5MzQKNTMx..."
188+
}
189+
{
190+
"File":"alloc/logs/redis.stdout.0",
191+
"FileEvent": "file deleted"
192+
}
193+
```
194+
195+
</dd>
196+
197+
198+
<dt>Field Reference</dt>
199+
<dd>
200+
The return value is a stream of frames. These frames contain the following
201+
fields:
202+
203+
<ul>
204+
<li>
205+
<span class="param">Data</span>
206+
A base64 encoding of the bytes being streamed.
207+
</li>
208+
<li>
209+
<span class="param">FileEvent</span>
210+
An event that could cause a change in the streams position. The possible
211+
values are "file deleted" and "file truncated".
212+
</li>
213+
<li>
214+
<span class="param">Offset</span>
215+
Offset is the offset into the stream.
216+
</li>
217+
<li>
218+
<span class="param">File</span>
219+
The name of the file being streamed.
220+
</li>
221+
</ul>
222+
</dd>
223+
</dl>
224+
136225
<dl>
137226
<dt>Description</dt>
138227
<dd>

website/source/docs/jobops/logs.html.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ description: |-
1010

1111
Accessing applications logs is critical when debugging issues, performance
1212
problems or even for verifying the application is starting correctly. To make
13-
this as simple as possible, Nomad provides both a CLI tool and an API for
14-
accessing application logs and data files.
13+
this as simple as possible, Nomad provides [log
14+
rotation](/docs/jobspec/index.html#log_rotation) in the jobspec, provides a [CLI
15+
command](/docs/commands/logs.html) and an [API](/docs/http/client-fs.html#logs)
16+
for accessing application logs and data files.
1517

1618
To see this in action we can just run the example job which created using `nomad
1719
init`:
@@ -32,18 +34,15 @@ $ nomad run example.nomad
3234
==> Evaluation "7a3b78c0" finished with status "complete"
3335
```
3436

35-
We can grab the allocation ID from above and use the [`nomad fs`
36-
command](/docs/commands/fs.html) to access the applications logs. Logs are
37-
stored under the following directory structure:
38-
`alloc/logs/<task-name>.<stdout/stderr>.<index>`. Nomad has built in log
39-
rotation, documented in the [Jobspec](/docs/jobspec/index.html#log_rotation).
40-
The index is a monotonically increasing number starting at zero and incremented
41-
each time the log is rotated.
37+
We can grab the allocation ID from above and use the [`nomad logs`
38+
command](/docs/commands/logs.html) to access the applications logs. The `logs`
39+
command supports both displaying the logs as well as following logs, blocking
40+
for more output.
4241

4342
Thus to access the `stdout` we can issue the below command:
4443

4544
```
46-
$ nomad fs c3c58508 alloc/logs/redis.stdout.0
45+
$ nomad logs c3c58508 redis
4746
_._
4847
_.-``__ ''-._
4948
_.-`` `. `_. ''-._ Redis 3.2.1 (00000000/0) 64 bit
@@ -69,7 +68,11 @@ $ nomad fs c3c58508 alloc/logs/redis.stdout.0
6968
1:M 28 Jun 19:49:30.505 * The server is now ready to accept connections on port 6379
7069
```
7170

72-
Replacing `stdout` for `stderr` would display the respective `stderr` output.
71+
To display the `stderr` for the task we would run the following:
72+
73+
```
74+
$ nomad logs -stderr c3c58508 redis
75+
```
7376

7477
While this works well for quickly accessing logs, we recommend running a
7578
log-shipper for long term storage of logs. In many cases this will not be needed

website/source/intro/getting-started/install.html.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Available commands are:
5555
fs Inspect the contents of an allocation directory
5656
init Create an example job file
5757
inspect Inspect a submitted job
58+
logs Streams the logs of a task.
5859
node-drain Toggle drain mode on a given node
5960
node-status Display status information about nodes
6061
plan Dry-run a job update to determine its effects

website/source/intro/getting-started/jobs.html.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,10 @@ We can see that Nomad reports the state of the allocation as well as its
105105
current resource usage. By supplying the `-stats` flag, more detailed resource
106106
usage statistics will be reported.
107107

108-
To inspect the file system of a running allocation, we can use the [`fs`
109-
command](/docs/commands/fs.html):
108+
To see the logs of a task, we can use the [logs command](/docs/commands/logs.html):
110109

111110
```
112-
$ nomad fs 8ba85cef alloc/logs
113-
Mode Size Modified Time Name
114-
-rw-rw-r-- 0 B 15/03/16 15:40:56 PDT redis.stderr.0
115-
-rw-rw-r-- 2.3 kB 15/03/16 15:40:57 PDT redis.stdout.0
116-
117-
$ nomad fs 8ba85cef alloc/logs/redis.stdout.0
111+
$ nomad logs 8ba85cef redis
118112
_._
119113
_.-``__ ''-._
120114
_.-`` `. `_. ''-._ Redis 3.2.1 (00000000/0) 64 bit

website/source/intro/getting-started/next-steps.html.md

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ We recommend reading the following as next steps.
2020

2121
* [Creating a Cluster](/docs/cluster/bootstrapping.html) - Additional details on
2222
creating a production worthy Nomad Cluster.
23+
24+
* [Operating a Job](/docs/jobops/index.html) - Additional details on how to
25+
run a job in production.
26+

website/source/layouts/docs.erb

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
<li<%= sidebar_current("docs-commands-inspect") %>>
158158
<a href="/docs/commands/inspect.html">inspect</a>
159159
</li>
160+
<li<%= sidebar_current("docs-commands-logs") %>>
161+
<a href="/docs/commands/logs.html">logs</a>
162+
</li>
160163
<li<%= sidebar_current("docs-commands-node-drain") %>>
161164
<a href="/docs/commands/node-drain.html">node-drain</a>
162165
</li>

0 commit comments

Comments
 (0)