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