github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/docs/extend/plugins_logging.md (about)

     1  ---
     2  title: "Docker log driver plugins"
     3  description: "Log driver plugins."
     4  keywords: "Examples, Usage, plugins, docker, documentation, user guide, logging"
     5  ---
     6  
     7  <!-- This file is maintained within the docker/docker Github
     8       repository at https://github.com/docker/docker/. Make all
     9       pull requests against that repo. If you see this file in
    10       another repository, consider it read-only there, as it will
    11       periodically be overwritten by the definitive file. Pull
    12       requests which include edits to this file in other repositories
    13       will be rejected.
    14  -->
    15  
    16  # Logging driver plugins
    17  
    18  This document describes logging driver plugins for Docker.
    19  
    20  Logging drivers enables users to forward container logs to another service for
    21  processing. Docker includes several logging drivers as built-ins, however can
    22  never hope to support all use-cases with built-in drivers. Plugins allow Docker
    23  to support a wide range of logging services without requiring to embed client
    24  libraries for these services in the main Docker codebase. See the
    25  [plugin documentation](legacy_plugins.md) for more information.
    26  
    27  ## Create a logging plugin
    28  
    29  The main interface for logging plugins uses the same JSON+HTTP RPC protocol used
    30  by other plugin types. See the
    31  [example](https://github.com/cpuguy83/docker-log-driver-test) plugin for a
    32  reference implementation of a logging plugin. The example wraps the built-in
    33  `jsonfilelog` log driver.
    34  
    35  ## LogDriver protocol
    36  
    37  Logging plugins must register as a `LogDriver` during plugin activation. Once
    38  activated users can specify the plugin as a log driver.
    39  
    40  There are two HTTP endpoints that logging plugins must implement:
    41  
    42  ### `/LogDriver.StartLogging`
    43  
    44  Signals to the plugin that a container is starting that the plugin should start
    45  receiving logs for.
    46  
    47  Logs will be streamed over the defined file in the request. On Linux this file
    48  is a FIFO. Logging plugins are not currently supported on Windows.
    49  
    50  **Request**:
    51  ```json
    52  {
    53  		"File": "/path/to/file/stream",
    54  		"Info": {
    55  			"ContainerID": "123456"
    56  		}
    57  }
    58  ```
    59  
    60  `File` is the path to the log stream that needs to be consumed. Each call to
    61  `StartLogging` should provide a different file path, even if it's a container
    62  that the plugin has already received logs for prior. The file is created by
    63  docker with a randomly generated name.
    64  
    65  `Info` is details about the container that's being logged. This is fairly
    66  free-form, but is defined by the following struct definition:
    67  
    68  ```go
    69  type Info struct {
    70  	Config              map[string]string
    71  	ContainerID         string
    72  	ContainerName       string
    73  	ContainerEntrypoint string
    74  	ContainerArgs       []string
    75  	ContainerImageID    string
    76  	ContainerImageName  string
    77  	ContainerCreated    time.Time
    78  	ContainerEnv        []string
    79  	ContainerLabels     map[string]string
    80  	LogPath             string
    81  	DaemonName          string
    82  }
    83  ```
    84  
    85  
    86  `ContainerID` will always be supplied with this struct, but other fields may be
    87  empty or missing.
    88  
    89  **Response**
    90  ```json
    91  {
    92  	"Err": ""
    93  }
    94  ```
    95  
    96  If an error occurred during this request, add an error message to the `Err` field
    97  in the response. If no error then you can either send an empty response (`{}`)
    98  or an empty value for the `Err` field.
    99  
   100  The driver should at this point be consuming log messages from the passed in file.
   101  If messages are unconsumed, it may cause the container to block while trying to
   102  write to its stdio streams.
   103  
   104  Log stream messages are encoded as protocol buffers. The protobuf definitions are
   105  in the
   106  [docker repository](https://github.com/docker/docker/blob/master/api/types/plugins/logdriver/entry.proto).
   107  
   108  Since protocol buffers are not self-delimited you must decode them from the stream
   109  using the following stream format:
   110  
   111  ```
   112  [size][message]
   113  ```
   114  
   115  Where `size` is a 4-byte big endian binary encoded uint32. `size` in this case
   116  defines the size of the next message. `message` is the actual log entry.
   117  
   118  A reference golang implementation of a stream encoder/decoder can be found
   119  [here](https://github.com/docker/docker/blob/master/api/types/plugins/logdriver/io.go)
   120  
   121  ### `/LogDriver.StopLogging`
   122  
   123  Signals to the plugin to stop collecting logs from the defined file.
   124  Once a response is received, the file will be removed by Docker. You must make
   125  sure to collect all logs on the stream before responding to this request or risk
   126  losing log data.
   127  
   128  Requests on this endpoint does not mean that the container has been removed
   129  only that it has stopped.
   130  
   131  **Request**:
   132  ```json
   133  {
   134  		"File": "/path/to/file/stream"
   135  }
   136  ```
   137  
   138  **Response**:
   139  ```json
   140  {
   141  	"Err": ""
   142  }
   143  ```
   144  
   145  If an error occurred during this request, add an error message to the `Err` field
   146  in the response. If no error then you can either send an empty response (`{}`)
   147  or an empty value for the `Err` field.
   148  
   149  ## Optional endpoints
   150  
   151  Logging plugins can implement two extra logging endpoints:
   152  
   153  ### `/LogDriver.Capabilities`
   154  
   155  Defines the capabilities of the log driver. You must implement this endpoint for
   156  Docker to be able to take advantage of any of the defined capabilities.
   157  
   158  **Request**:
   159  ```json
   160  {}
   161  ```
   162  
   163  **Response**:
   164  ```json
   165  {
   166  	"ReadLogs": true
   167  }
   168  ```
   169  
   170  Supported capabilities:
   171  
   172  - `ReadLogs` - this tells Docker that the plugin is capable of reading back logs
   173  to clients. Plugins that report that they support `ReadLogs` must implement the
   174  `/LogDriver.ReadLogs` endpoint
   175  
   176  ### `/LogDriver.ReadLogs`
   177  
   178  Reads back logs to the client. This is used when `docker logs <container>` is
   179  called.
   180  
   181  In order for Docker to use this endpoint, the plugin must specify as much when
   182  `/LogDriver.Capabilities` is called.
   183  
   184  
   185  **Request**:
   186  ```json
   187  {
   188  	"ReadConfig": {},
   189  	"Info": {
   190  		"ContainerID": "123456"
   191  	}
   192  }
   193  ```
   194  
   195  `ReadConfig` is the list of options for reading, it is defined with the following
   196  golang struct:
   197  
   198  ```go
   199  type ReadConfig struct {
   200  	Since  time.Time
   201  	Tail   int
   202  	Follow bool
   203  }
   204  ```
   205  
   206  - `Since` defines the oldest log that should be sent.
   207  - `Tail` defines the number of lines to read (e.g. like the command `tail -n 10`)
   208  - `Follow` signals that the client wants to stay attached to receive new log messages
   209  as they come in once the existing logs have been read.
   210  
   211  `Info` is the same type defined in `/LogDriver.StartLogging`. It should be used
   212  to determine what set of logs to read.
   213  
   214  **Response**:
   215  ```
   216  {{ log stream }}
   217  ```
   218  
   219  The response should be the encoded log message using the same format as the
   220  messages that the plugin consumed from Docker.