Michelle
Michelle

Reputation: 1097

Is there a way to customize Docker's log?

We are collecting the logs of our applications. Since we containerize our applications, the way to collect logs needs a little bit changes.

We log via the Docker Logging Driver:

But the logs from Docker has additional information which unnecessary and make the forward step complicated because we need to remove those additional information before forward.

For example, the log from Docker is as below, but all we want is the value of log field. Is there a way to customize log format and only output the information wanted by override some Docker's configurations?

{
  “log”: "{“level”: “info”,“message”: “data is correct”,“timestamp”: “2017-08-01T11:35:30.375Z”}\r\n",
  “stream”: “stdout”,
  “time”: “2017-08-03T07: 58: 02.387253289Z”
}

Upvotes: 1

Views: 1021

Answers (1)

nivox
nivox

Reputation: 2130

I don't know of any way to customize the output of the json-file docker log plugin. However docker supports the gelf plugin which allows you to send logs to logstash. Using logstash you can output logs in many different ways (by using output plugins) and at the same time customize the format.

For instance to output logs to a file (without any other metadata) you can use something like the following:

output {
 file {
   path => "/path/to/logfile"
   codec => line { format => "%{message}"}
 }
}

If you don't want to add complexity to your logging logic, you can keep using the json-file driver and use an utility such as jq to parse the file and extract only the relevant information. For instance with jq you can do: jq -r .log </path/to/logfile>

This will read each line of the specified file as a json object and output only the log field.

Upvotes: 1

Related Questions