Skip to content

Add timestamp prefixes to command outputs

License

Notifications You must be signed in to change notification settings

mokiat/preftime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

preftime

Build Status Go Report Card

Preftime is a tool that allows you to prefix the output of another program with timestamp information.

Ideally, the program you are running should have the capability to log timestamps on its own. Alternatively, you could check whether you terminal supports such a feature. However, I have been in situations where neither option was available. I ended up writing these few lines of code and decided to share it with anyone who may need it.

If you have a Go development environment set up, you can use the following command to acquire the tool.

go install github.com/mokiat/preftime@latest

Otherwise, there are pre-built versions of the tool available in the Releases section of this project.

You would use the tool as follows.

<command> 2>&1 | preftime

The 2>&1 part is optional. It just assures that both stdout and stderr go through preftime.

As an alternative, you could do the following.

<command> > >(preftime) 2> >(preftime)

This redirects both stdout and stderr to preftime. The >(preftime) part makes preftime behave like a writable file.

Following the same logic, you could have your bash script redirect it's output to preftime.

#!/bin/bash -e

exec > >(preftime)
exec 2> >(preftime)

<command1>
<command2>

For example, here is a real-life simple usage scenario.

$ host example.org | preftime
[2017-06-25 15:42:00.664] example.org has address 93.184.216.34
[2017-06-25 15:42:00.665] example.org has IPv6 address 2606:2800:220:1:248:1893:25c8:1946