View logs since a specific time or tail the output
In this step, we will explore how to view container logs generated after a specific time or view only the most recent log entries. This is particularly useful when dealing with containers that have been running for a long time and have generated a large volume of logs.
To view logs generated since a specific time, you can use the --since
flag. The value for --since
can be a timestamp (like the ones we saw with the -t
flag) or a relative time duration (e.g., 10m
for 10 minutes, 1h
for 1 hour).
Let's wait for a few seconds to allow our my-logging-container
to generate some more logs. Then, we will view the logs generated in the last 30 seconds.
sleep 30
docker logs --since 30s my-logging-container
This command will display only the log entries that were generated within the last 30 seconds. The output will be similar to the full logs, but it will only include the recent entries.
Hello from the container at ...
Hello from the container at ...
Hello from the container at ...
... (only logs from the last 30 seconds)
Another common requirement is to view only the last few lines of the logs, similar to using the tail
command. You can achieve this using the --tail
flag, followed by the number of lines you want to see.
Let's view the last 5 lines of the logs from our container:
docker logs --tail 5 my-logging-container
This command will output only the most recent 5 log entries from the container.
Hello from the container at ...
Hello from the container at ...
Hello from the container at ...
Hello from the container at ...
Hello from the container at ... (the last 5 lines)
You can combine --since
and --tail
with other flags like -t
and -f
to get more specific views of your container logs. For example, to view the last 10 lines with timestamps:
docker logs -t --tail 10 my-logging-container
These options provide powerful ways to filter and view container logs, making it easier to find the information you need quickly.