Docker Logs not Readable
Asked Answered
B

5

8

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.

Basilius answered 25/9, 2019 at 12:8 Comment(0)
B
3

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.

Basilius answered 9/10, 2019 at 4:49 Comment(0)
K
6

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
Kindrakindred answered 25/9, 2019 at 12:28 Comment(2)
Thanks for the information and I surely believe the command would work on linux machine but I am running docker on windows machine where there is no sed command available. Do you have any other alternative for the windows machineBasilius
Install Git. Start Git Bash. Use sed and most of the other standard linux commands.Discuss
B
3

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.

Basilius answered 9/10, 2019 at 4:49 Comment(0)
R
1

For anyone who comes across this, my scenario was due to the run command having -tty, so removing this removed the special characters, with TTY ...

2021-01-13T02:32:19.4743079Z [01/13/2021 15:32:19] docker run --tty --volume C:\Users\agent:C:/solution/home microlith_master_push:294 deploy.bat Test

2021-01-13T02:32:20.9345011Z [2J[?25l[m[H
2021-01-13T02:32:21.0359039Z [H]0;C:\Windows\system32\cmd.exe[?25h[?25l[deploy.bat] ExecutionPolicy ByPass ... 
2021-01-13T02:32:21.0360194Z [?25h[?25l

without (note the subtle difference that the command shell being used isn't logged without TTY)

2021-01-13T03:40:51.1535448Z [01/13/2021 16:40:51] docker run --volume C:\Users\agent:C:/solution/home microlith_master_push:298 deploy.bat Test

2021-01-13T03:40:52.4624483Z [deploy.bat] ExecutionPolicy ByPass ...

Note: if the application itself uses ANSI codes, those will still come through, e.g. MuleSoft AnyPoint CLI

2021-01-13T03:41:16.4235891Z [01/13/2021 16:41:16] anypoint-cli runtime-mgr cloudhub-application modify --runtime 4.3.0 api-c-0.1.3.jar --output table

2021-01-13T03:41:20.8002477Z Updating api-c-Test ...
2021-01-13T03:41:49.9849592Z [90m┌──────────────────────────────[39m[90m┬────────────────────────────────────────────────────────────────┐[39m
2021-01-13T03:41:49.9850811Z [90m│[39m Domain                       [90m│[39m api-c-test.us-e1.cloudhub.io                                   [90m│[39m
2021-01-13T03:41:49.9851873Z [90m├──────────────────────────────[39m[90m┼────────────────────────────────────────────────────────────────┤[39m
2
Rile answered 13/1, 2021 at 3:55 Comment(0)
C
0

Non intuitively docker logs just outputs the standard output of the container entrypoint/command.

What you are getting in the output are the special ANSI characters to set text style (font color and foreground) on the shell interpreter. Usually, that is not friendly for log files or showing it as plain text.

Some applications have the option to output in plain text (no special characters) (e.g: docker-compose logs --no-colors).

Chemmy answered 25/9, 2019 at 12:20 Comment(2)
Thanks for the reply but there is no similar tag for the docker command that would prevent these escape characters.Basilius
It seems the correct flag is --no-color (singular), at least on docker-compose version 1.29.2Campagna
C
0

docker build --progress plain works for docker build.

Craquelure answered 3/1, 2022 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.