Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding docker logs

We all know that we can search for a string in a log file and show the surroundings lines to the searched string:
grep -AXX -BXX "searched" file.log

Is possible do the same with the docker logs command?
I want to do is that if already know the string i'm searching happened around one hour ago, using "since" with "after" and "before", get only the result and not all the log, for example something like this:
docker logs --since=65m -A20 -B5 "searched string" [ID]

By now i copy all to a file, resulting sometimes a big file, and use a grep:
docker logs --since=65m [ID] >> file.log
grep -AXX -BXX "searched" file.log

like image 742
Juanjo Avatar asked May 06 '26 02:05

Juanjo


2 Answers

According to documentation you cannot. However, you can grep with pipe

docker logs | grep "whatever"

stackoverflow answer for grep

docker documentation about logs

Alternative for Development

However for development purposes I am using datalust/seq - just add the image to your docker-compose file and with small configuration (depends on language which you are using) redirect logs to nice searchable portal.

# docker compose 
 seq:
    image: datalust/seq:latest 
    environment:
      - ACCEPT_EULA=Y
    ports:
      - "8082:80"

The portal (under http://localhost:8082 as I redirected default 80 port from Container to my 8082 port): seq image

Alternative for Production

For production purposes I would recommend to use something which is able to automatically collect logs from stdout and stderr like:

  • Logstack + Elastick Search
  • Azure Application Insights
  • New Relic logs

To provide the highest standards to your app regarding logging and monitoring.

like image 182
Michał Jarzyna Avatar answered May 09 '26 00:05

Michał Jarzyna


grep will read from stdin when you don't give it a file. Specifically with docker logs, you'll want to merge stdout and stderr together using the 2>&1 notation. The result looks like:

 docker logs --since=65m [ID] 2>&1 | grep -A20 -B5 "searched string"
like image 37
BMitch Avatar answered May 09 '26 01:05

BMitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!