When you’re working as a software developer there are a lot of CLI tools that you can leverage to make your day better. Whether it’s jq, grep, scp, git or something else!
For me, working on Dashcam, I have started to love and use
tee a lot because that’s the best way to write logs from my local CLI environment so that the app can see ’em.
tee command is a tool for redirecting the output of a command or program. Since Dashcam uses flat files (text files), it makes sense to redirect the output to such file.
Then Dashcam can use the content of the file to synchronize it with a video of your screen.
How does tee work?
The tee command takes the standard output from any command and “tees” it off to both standard output (your screen) and one or more files. The command essentially lets you send the output stream to multiple destinations.
The syntax of tee is:
command | tee [options] [file]
The most important things to know about the
tee command are:
- It splits the output of a command into stdout – standard out (your screen) – and a file
- You can append the output to a file instead of overwriting it
Here are some examples of using tee in action:
Basic usage of tee:
Copy output to screen and file
ls -l | tee file.txt
This lists the directory contents and also saves the same output to file.txt.
Common usage of tee
Append output to end of file
npm run dev | tee -a myapp.log
This prints the a node application’s messages and appends them to a
How we use tee daily with Dashcam
npm run dev 2>&1 | tee -a myapp.log
Here we start our node application, and then append all the messages to
If you wonder what 2>&1 does: this is a Shell operator that is used in the command line to redirect stderr (file descriptor 2) to stdout (file descriptor 1).
In simpler terms, it merges both the normal output from npm and any errors into a single stream, tee in our case. This way, both regular output and error messages will be displayed in the same place when you run this command.
Let’s clarify even further by breaking it down
- The app is launched with
npm run dev
- Both the standard output and standard error of the script will be displayed on the terminal in real-time.
- Simultaneously, both the standard output and standard error will be appended to the
myapp.logfile, preserving any previous content in the file (because we used
Why Use Tee?
Since Dashcam synchronizes video and errors to give you a single view for debugging, it makes sense to redirect the output to a file, this way Dashcam can easily read its contents.
tee command is useful for saving command output to a file while also sending it to the screen. Some examples of how it can be used:
- Log a backup/archive creation process to a file
- Append output from various commands to a single log file
- Simultaneously view output on screen and write it to a file
How to make Dashcam read a log file?
In the “logs” panel of the Dashcam desktop app, add a system pattern that matches the log file written by
tee. In the below example it’s
tee is a handy tool and Dashcam pairs perfectly with it. Give it a try to debug any application, backend service or other – running on your machine!