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