Reputation: 2413
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.
Upvotes: 8
Views: 7188
Reputation: 488
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
Upvotes: 1
Reputation: 2413
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.
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 charactersed
does not support alternatives \e
and \033
)\[
is the second character of the escape sequence[0-9;]*
is the color value(s) regexm
is the last character of the escape sequencedocker logs container | ForEach-Object { $_ -replace '\x1b\[[0-9;]*m','' }| Out-File -FilePath .\docker-logs.log
The above command will remove the special characters like [1;35m
, [0m[1;3
and ^[[37mABC
from the output stream.
Upvotes: 3
Reputation: 10094
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
).
Upvotes: 0
Reputation: 265045
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
Upvotes: 6