Modern mode
Notes
Home | Portfolio
- Loading notes...
- Loading note content...
Modern mode
Home | Portfolio
October 12, 2026
Tags: Tutorial
When your application is running on a production server, logs are your primary "eyes and ears" for understanding what is happening inside the system. Whether you are using PM2 to manage Node.js processes directly on the OS or using Docker to run applications inside containers, the ability to access and read historical logs is crucial for debugging.
This article will cover how to access, filter, and save old logs in both PM2 and Docker.
PM2 automatically captures the terminal output of your application and saves it into physical files on your server. This makes searching for old logs relatively straightforward using built-in Linux commands.
To view the currently streaming logs, you can simply use the command:
pm2 logs [app_name]
If you need to view logs from previous days, you must open the physical log files. To find out where PM2 stores these files, run:
pm2 info [app_name]
Look for the "error log path" and "out log path" sections. Usually, the locations are inside the ~/.pm2/logs/ directory.
Once you know the location, avoid using standard text editors if the log file is extremely large. Instead, use the following Linux commands:
Read logs slowly (Scrollable):
less ~/.pm2/logs/app_name-error.log
(Use the up/down arrow keys to navigate, and press q to quit).
Search for a specific keyword (e.g., "Error" or "Failed"):
grep "Error" ~/.pm2/logs/app_name-error.log
PM2 Tip: Make sure you have installed the
pm2-logrotatemodule so that these log files do not bloat and consume all of your server's disk space over time.
Unlike PM2, Docker manages its own logs (usually in a hidden JSON format) and provides a powerful Command Line Interface (CLI) to filter logs without having to touch the raw files directly.
docker logs [container_name]docker logs -f [container_name]docker logs --tail 50 [container_name]One of Docker's best features is its ability to search for logs within a specific time range. This is incredibly useful if you know the approximate time an error occurred.
View logs from the last hour:
docker logs --since 60m [container_name]
View logs from a specific date and time:
docker logs --since "2023-10-25T10:00:00" --until "2023-10-25T11:00:00" [container_name]
You can combine Docker commands with the Linux grep command to filter for specific messages:
docker logs [container_name] 2>&1 | grep -i "error"
If the logs are too long and difficult to read in the terminal, you can export them into a plain text file. You can then download this file to your local computer and open it using VS Code or any other text editor.
docker logs [container_name] > app_error_log.txt
Docker Warning: By default, Docker does not limit log size. If left unchecked, logs can grow to gigabytes in size and cause your VM disk to become 100% full. Always ensure you add log size limit rules (
max-sizeandmax-file) in yourdocker-compose.ymlconfiguration.
Both PM2 and Docker have slightly different approaches to storing logs, but both provide adequate tools for debugging.
less and grep.--since and --until to filter specific timeframes, or export them to external files.Always implement log rotation practices on both platforms to ensure your server remains healthy!