Skip to main content

Output Node Logs To Files

Table of Contents

Intro

If you're working on a Node.js project and your console is flooded with logs, so that you can't even scroll up and read them all, you can output them to a file.

Output Logs to File

npm START_COMMAND > output.log

where START_COMMAND could be whatever your start command would be, like run dev or test.

However > will only output the stdout to the file. If you want to output both stdout and stderr to the file, you can use:

npm START_COMMAND > output.log 2>&1

To organize logs by date and time, you can dynamically name the output file. For example:

node START_COMMAND > logs/$(date +'%Y-%m-%d_%H-%M-%S').log 2>&1

This will create a file in the logs directory with the current date and time as the file name. You will need to manually create the logs directory before running the command, as it won't create it for you.

tip

Don't forget to add the logs directory to your .gitignore file so you don't accidentally commit the log files.

Comments

Recent Work

Free desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.

Learn More

BidBear

bidbear.io

Bidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.

Learn More