
- To demonstrate this, i have a sample spring boot application which prints log every 1000 milliseconds (1 second)
@RestController
public class RandomLoggerResource {
private static final Logger logger = LoggerFactory.getLogger(RandomLoggerResource.class);
private Random random = new Random();
@Scheduled(fixedRate = 1000)
public void logRandomMessage() {
int randomInt = random.nextInt();
String randomMessage = "Generating a Random integer every 1000 milliseconds (1 second): " + randomInt;
logger.info(randomMessage);
}
}
- Code for the application is available at : https://github.com/rajeshsgr/random-logger-svc
- Build a docker container of this application , by executing the below command
docker build -t raje/random-log-generator-svc .
- Run the container in a detached mode using the below command. This will expose the application on port 8080
docker run -d -p 8080:8080 raje/random-log-generator-svc
- Use the below command to get a list of running containers and get the container id
docker ps -a
- Output in my computer from the above command . Copy the container id from the output.

- To view the latest logs , use the below command and replace the container id with the container id that you see at your end
docker logs <container ID>
- In order to continuously stream the logs on a terminal , use the below command and replace the container id with the container id that you see at your end
docker logs --follow <container ID>
- To only get the last 100 lines or any number of lines , se the below command and replace the container id with the container id that you see at your end and the line number with the number of lines that you want to see.
docker logs --tail <<line number>> <container ID>
- To stream logs until a specific point in time, use the below command . You can use different notations to designate the timescale like 30m, 1h
docker logs --follow --until=<<desired time>> <container ID>
- To stream logs from a specific point in time, use the below command . The accepted date format here is YYYY-MM-DDTHH:MM:SS.
docker logs --since <<date since>> <container ID>
Code for the example is available at : https://github.com/rajeshsgr/random-logger-svc
You can download the command for logs from this link : Command PDF
Thank you , and if you like these posts, please do subscribe from below
Nice