Access application logs for troubleshooting
Viewing application logs is critical for debugging issues, examining performance problems, or even verifying the application started correctly. To make this as simple as possible, Nomad provides:
- Job specification for log rotation
- CLI command for log viewing
- API for programmatic log access
This section will use the job named "docs" from the previous sections, but these operations and command largely apply to all jobs in Nomad.
As a reminder, here is the output of the run command from the previous example:
The provided allocation ID (which is also available via the nomad status
command) is required to access the application's logs. To access the logs of our
application, issue the following command:
The output will look something like this:
By default, this will return the logs of the task. If more than one task is defined in the job file, the name of the task is a required argument:
The logs command supports both displaying the logs as well as following logs,
blocking for more output, similar to tail -f
. To follow the logs, use the
appropriately named -f
flag:
This will stream logs to your console.
If you are only interested in the "tail" of the log, use the -tail
and -n
flags:
This will show the last 25 lines. If you omit the -n
flag, -tail
will
default to 10 lines.
By default, only the logs on stdout are displayed. To show the log output from
stderr, use the -stderr
flag:
Consider the "log shipper" pattern
While the logs command works well for quickly accessing application logs, it generally does not scale to large systems or systems that produce a lot of log output, especially for the long-term storage of logs. Nomad's retention of log files is best effort, so chatty applications should use a better log retention strategy.
Since applications log to the alloc/
directory, all tasks within the same task
group have access to each others logs. Thus it is possible to have a task group
as follows:
In the above example, the server
task is the application that should be run
and will be producing the logs. The log-shipper
reads those logs from the
alloc/logs/
directory and sends them to a longer-term storage solution such as
Amazon S3 or an internal log aggregation system.
When using the log shipper pattern, especially for batch jobs, the main task
should be marked as the leader task. By marking the main task as a leader,
when the task completes all other tasks within the group will be gracefully
shutdown. This allows the log shipper to finish sending any logs and then
exiting itself. The log shipper should set a high enough kill_timeout
such
that it can ship any remaining logs before exiting.