DEV Community

Cover image for How to view server logs in real-time in VS Code
Rajesh Royal
Rajesh Royal

Posted on

How to view server logs in real-time in VS Code

Ever wondered how to view log files in real time without downloading them? I recently needed it, as I was tired of downloading the log files from the server and then inspecting them. Traditionally, accessing the server directory, downloading the log file, and then examining them can be a cumbersome and time-consuming process. But what if there is a way to streamline this task directly within your favorite code editor, Visual Studio Code (VS Code)?

In this blog post, I'll guide you through the process of using an extension called Logtail available in the VS Code Marketplace. This tool allows developers to view server log files in real time, making the development process more productive by removing unnecessary steps.

Step 1: Installing Logtail in VS Code

  1. Open VS Code and navigate to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
  2. In the Extensions view search bar, type Logtail and press Enter.
  3. Find the Logtail extension in the list and click the Install button.

Step 2: Configuring Logtail

After installing Logtail, you'll need to configure it to connect to your server logs.

Add the below code to your vs code settings.json file.



"log-tail.channel": "document",       //Configuring the log printout channel
// make sure to update remote object with your server ssh details
"log-tail.remote": {
    "host": "localhost",
    "port": 22,
    "username": "root",
    "password": ""
},
//Configure a list of log file paths, followed by selective monitoring.
"log-tail.logsPath": [
    "/home/dev/main/log/12-02-2024.log",
    "/home/qa/main/log/12-02-2024.log",
]


Enter fullscreen mode Exit fullscreen mode
  1. Open the Command Palette with Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS).
  2. Type Logtail: Start and select the command to activate the extension.
  3. Enter/Select the path to your server's log file when prompted. For example, /home/dev/main/log/12-02-2024.log.
  4. The extension will now start tailing the log file, displaying updates in real time within VS Code.

Output File

nodejs winston logger realtime logs over ssh via logtail in vscode example screenshot

Example Usage

Let's say you're monitoring logs for a web application and want to focus on error messages. You could set up a filter for the word "error" to only display relevant entries. If an error occurs, you'll see it immediately in the Logtail output, allowing you to quickly jump into action and address the issue.

Conclusion

The Logtail extension for VS Code is a powerful tool that enhances productivity by allowing developers to view server logs in real time. By following the steps outlined above, you can set up Logtail and start monitoring your logs more efficiently, saving time and hassle in your development workflow.

Happy coding! 😃😃


Top comments (6)

Collapse
 
rajeshkanaka profile image
Rajesh P

For using key to remote connection,
"log-tail.remote": {
"host": "remote-server",
"port": 22,
"username": "admin",
"privateKeyPath": "/Users/XXX/keys/keytouse.pem"
},

Collapse
 
idubi profile image
idubi

I use tail -f on the log.
usually ssh works to connect to it and watch.

nice plugin.

Collapse
 
ahadhi profile image
ahadhi

I am using username & keyfile [ppk] to connect remote server [sftp] in windows. And the key file is available at C:\mykeyfolder\my.ppk. Please provide configuration for keyfile.

Collapse
 
rajeshroyal profile image
Rajesh Royal

Unfortunately the plugin does not provide a option for this as of now. I will suggest you to check the plugin source code and see if you can modify it as per your need.

Collapse
 
ferretchen profile image
Dan

Thanks for bringing this extension to my attention. Is there a way to specify multiple remote servers?

Collapse
 
rajeshroyal profile image
Rajesh Royal

Yes, Instead of using the user setting you can add project level settings.json file and can listen to two servers in two vscode windows.