github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/website/source/docs/operating-a-job/accessing-logs.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Accessing Logs - Operating a Job"
     4  sidebar_current: "docs-operating-a-job-accessing-logs"
     5  description: |-
     6    Nomad provides a top-level mechanism for viewing application logs and data
     7    files via the command line interface. This section discusses the nomad logs
     8    command and API interface.
     9  ---
    10  
    11  # Accessing Logs
    12  
    13  Viewing application logs is critical for debugging issues, examining performance
    14  problems, or even just verifying the application started correctly. To make this
    15  as simple as possible, Nomad provides:
    16  
    17  - Job specification for [log rotation](/docs/job-specification/logs.html)
    18  - CLI command for [log viewing](/docs/commands/logs.html)
    19  - API for programatic [log access](/docs/http/client-fs.html#logs)
    20  
    21  This section will utilize the job named "docs" from the [previous
    22  sections](/docs/operating-a-job/submitting-jobs.html), but these operations
    23  and command largely apply to all jobs in Nomad.
    24  
    25  As a reminder, here is the output of the run command from the previous example:
    26  
    27  ```text
    28  $ nomad run docs.nomad
    29  ==> Monitoring evaluation "42d788a3"
    30      Evaluation triggered by job "docs"
    31      Allocation "04d9627d" created: node "a1f934c9", group "example"
    32      Allocation "e7b8d4f5" created: node "012ea79b", group "example"
    33      Allocation "5cbf23a1" modified: node "1e1aa1e0", group "example"
    34      Evaluation status changed: "pending" -> "complete"
    35  ==> Evaluation "42d788a3" finished with status "complete"
    36  ```
    37  
    38  The provided allocation ID (which is also available via the `nomad status`
    39  command) is required to access the application's logs. To access the logs of our
    40  application, we issue the following command:
    41  
    42  ```shell
    43  $ nomad logs 04d9627d
    44  ```
    45  
    46  The output will look something like this:
    47  
    48  ```text
    49  <timestamp> 10.1.1.196:5678 10.1.1.196:33407 "GET / HTTP/1.1" 200 12 "curl/7.35.0" 21.809µs
    50  <timestamp> 10.1.1.196:5678 10.1.1.196:33408 "GET / HTTP/1.1" 200 12 "curl/7.35.0" 20.241µs
    51  <timestamp> 10.1.1.196:5678 10.1.1.196:33409 "GET / HTTP/1.1" 200 12 "curl/7.35.0" 13.629µs
    52  ```
    53  
    54  By default, this will return the logs of the task. If more than one task is
    55  defined in the job file, the name of the task is a required argument:
    56  
    57  ```shell
    58  $ nomad logs 04d9627d server
    59  ```
    60  
    61  The logs command supports both displaying the logs as well as following logs,
    62  blocking for more output, similar to `tail -f`. To follow the logs, use the
    63  `-tail` flag:
    64  
    65  ```shell
    66  $ nomad logs -tail 04d9627d
    67  ```
    68  
    69  This will stream logs to our console.
    70  
    71  By default, only the logs on stdout are displayed. To show the log output from
    72  stderr, use the `-stderr` flag:
    73  
    74  ```shell
    75  $ nomad logs -stderr 04d9627d
    76  ```
    77  
    78  ## Log Shipper Pattern
    79  
    80  While the logs command works well for quickly accessing application logs, it
    81  generally does not scale to large systems or systems that produce a lot of log
    82  output, especially for the long-term storage of logs. Nomad's retention of log
    83  files is best effort, so chatty applications should use a better log retention
    84  strategy.
    85  
    86  Since applications log to the `alloc/` directory, all tasks within the same task
    87  group have access to each others logs. Thus it is possible to have a task group
    88  as follows:
    89  
    90  ```hcl
    91  group "my-group" {
    92    task "server" {
    93      # ...
    94  
    95      # Setting the server task as the leader of the task group allows us to
    96      # signal the log shipper task to gracefully shutdown when the server exits.
    97      leader = true
    98    }
    99  
   100    task "log-shipper" {
   101      # ...
   102    }
   103  }
   104  ```
   105  
   106  In the above example, the `server` task is the application that should be run
   107  and will be producing the logs. The `log-shipper` reads those logs from the
   108  `alloc/logs/` directory and sends them to a longer-term storage solution such as
   109  Amazon S3 or an internal log aggregation system.
   110  
   111  When using the log shipper pattern, especially for batch jobs, the main task
   112  should be marked as the [leader task](/docs/job-specification/task.html#leader).
   113  By marking the main task as a leader, when the task completes all other tasks
   114  within the group will be gracefully shutdown. This allows the log shipper to
   115  finish sending any logs and then exiting itself. The log shipper should set a
   116  high enough [`kill_timeout`](/docs/job-specification/task.html#kill_timeout)
   117  such that it can ship any remaining logs before exiting.