Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Logs not Readable

When I dump the docker log of the container using the below command

docker logs containername > containername.log

The output generated for the file is not legible to read and comprehend - No Date for log is mentioned - Unknown escape characters are added

I am not sure if I need to dump the logs with some extra option added to the above command or there is some bigger gap in my understanding.

Example of the output :

Hosting environment: Production Content root path: /app Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down. [39;49m[30m[[39;49m[39;49m[37m11:21:25.208 +00:00[39;49m[39;49m[30m [39;49m[39;49m[37mINF[39;49m[39;49m[30m] [39;49m[39;49m[36mRequest starting HTTP/1.1 GET http://localhost:31201/swagger [39;49m

[39;49m[30m[[39;49m[39;49m[37m11:21:25.943 +00:00[39;49m[39;49m[30m [39;49m[39;49m[37m[39;49m[37m[41mERR[39;49m[39;49m[30m] [39;49m[39;49m[37mLevel = ErrorMessage = Request Execution not successful for the following details : URL = http://localhost:31201/swagger

I am able to see the output clearly using docker logs command. I am not sure why these special characters are added to the output.

like image 313
Shubhanshu Rastogi Avatar asked Sep 25 '19 12:09

Shubhanshu Rastogi


People also ask

How do I view docker container logs?

You find these JSON log files in the /var/lib/docker/containers/ directory on a Linux Docker host. The <container_id> here is the id of the running container. If you're not sure which id is related to which container, you can run the docker ps command to list all running containers.

How do I persist docker logs?

You can persist all container log files by creating a volume mount point to the Docker host machine or central log server. Since every container has its own unique log folder ( containerType _ containerId ), you can simply mount all container log directories (*/logs/) to the same path on your host machine.

What happens if we do not specify a logging driver?

By default, no log-rotation is performed. As a result, log-files stored by the default json-file logging driver logging driver can cause a significant amount of disk space to be used for containers that generate much output, which can lead to disk space exhaustion.


2 Answers

That output is from an application that expects to write to a tty. This may be a configuration of the application that you can change. And it may also be detecting when their input is a tty which you can toggle with docker (the -t flag or tty in the compose file which defaults to off).

If you cannot modify the container or application to avoid printing the control characters, then you can attempt to strip them out with a sed command:

docker logs containername | sed $'s/[^[:print:]\t]//g' > containername.log
like image 146
BMitch Avatar answered Sep 17 '22 23:09

BMitch


Thanks for the above answers that helped me find the final solution.

One of the answer above tells to use sed utility that is only available on Unix and I had a special requirement for Windows system. For the windows system I used the Replace command available as a part of Powershell 3.0.The powershell makes use of regex expression that helps to replace the ANSI Color codes.

Using Regex

Below is the standard Regex for removing ANSI color codes (can be used in Linux and windows both)

'\x1b\[[0-9;]*m'
  • \x1b (or \x1B) is the escape special character
    (sed does not support alternatives \e and \033)
  • \[ is the second character of the escape sequence
  • [0-9;]* is the color value(s) regex
  • m is the last character of the escape sequence

Final Command

docker logs container | ForEach-Object { $_ -replace '\x1b\[[0-9;]*m','' }| Out-File -FilePath .\docker-logs.log

  • ForEach-Object refers to each object from the pipped stream and $_ refers to current object.

The above command will remove the special characters like [1;35m , [0m[1;3 and ^[[37mABC from the output stream.

like image 44
Shubhanshu Rastogi Avatar answered Sep 19 '22 23:09

Shubhanshu Rastogi