Note
Complete Guide to Accessing and Debugging Logs: PM2 vs Docker
Complete Guide to Accessing and Debugging Logs: PM2 vs Docker
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.
1. Accessing Old Logs in PM2
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.
Viewing Logs in Real-Time
To view the currently streaming logs, you can simply use the command:
pm2 logs [app_name]
Finding the Location of Old Log Files
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.
How to Read and Search for Errors in Log Files
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):
bashless ~/.pm2/logs/app_name-error.log(Use the up/down arrow keys to navigate, and press
qto quit). -
Search for a specific keyword (e.g., "Error" or "Failed"):
bashgrep "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.
2. Accessing Old Logs in Docker
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.
Basic Docker Log Commands
- View all logs since container creation:
docker logs [container_name] - View logs in real-time (streaming):
docker logs -f [container_name] - View only the last 50 lines:
docker logs --tail 50 [container_name]
Filtering Logs by Time (Time-Filtering)
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:
bashdocker logs --since 60m [container_name] -
View logs from a specific date and time:
bashdocker logs --since "2023-10-25T10:00:00" --until "2023-10-25T11:00:00" [container_name]
Searching for Keywords in Docker
You can combine Docker commands with the Linux grep command to filter for specific messages:
docker logs [container_name] 2>&1 | grep -i "error"
Exporting Docker Logs for Debugging
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.
Conclusion
Both PM2 and Docker have slightly different approaches to storing logs, but both provide adequate tools for debugging.
- In PM2, you interact directly with physical log files using Linux utilities like
lessandgrep. - In Docker, you utilize built-in flags like
--sinceand--untilto filter specific timeframes, or export them to external files.
Always implement log rotation practices on both platforms to ensure your server remains healthy!