How to Track Your Engineering Work
There are lots of reasons you might track your work. Maybe you do an annual performance review. Maybe you want to post status updates online. In my case, I want to create a monthly mailing list and let people know what I've been up to lately.

But, right now, I'm not keeping great track of my work.

I spend much of my day on the command-line and I typically enjoy command-line tools, so lets fix that with a quick bash script.

## How I want it to work

The syntax I ultimately want to use is something like the following:

log "Something I did today"

## Echoing to a file

At first, I'm going to keep this extremely simple. I'll append the string to a file. The command to do that would be something like this:

echo "Something I did today" >> ~/monthly.log

To break this down:

- echo prints the argument you pass it

- >> means to redirect that to the end of a file, appending to it

- ~/monthly.log means the log file in my home (~) directory

## Creating a shorter alias

Because I want a shorter command, just the word `log` followed by my comments, I'm going to put this into my `~/.zshrc` file as an alias. I use a Mac and have my terminal set to the default zsh shell. Something similar should work in Linux and maybe even Windows Subsystem for Linux (WSL). Here's the alias I'll use:

alias log="echo $1 >> ~/monthly.log"

This sets up an alias with the name of `log` that runs the echo command outlined above. You'll notice that I added `$1` this time. That's a variable that brings in the first argument after your command. So, I can now use the following command:

log "Something I did today"

The result will be a `monthly.log` file in my home directory with that line added to it.

## Reducing future maintenance

That's great, and I could stop there, but it will be a little bit manual right now. Every month I would need to rename that file. Instead, I want it to write to a file named after the month automatically.

One way I could do that is by running the `date` command. Here's a command that outputs the date in the format `YYYY-dd`.

date +%Y-%m

I can use backticks to run the date command and return its value into the alias above. Here's how my alias will look after I update it to do that.

alias log="echo $1 >> ~/log/`date +%Y-%m`.log"

This will echo the first argument ($1) to a file named `~/log/2024-10.log` when you run it in October. There is no longer a need to do any updates manually. Your log will be stored in the `~/log` directory as a series of files named after the current month and year.

We've added a `log` directory to the path but that directory needs to exist before we can write to it. For now, I'll create the directory manually with the following command:

mkdir ~/log

## Using a function instead of an alias

If you make your own logging command more complex, you're probably getting to the point where you should write a function instead of an alias. Here's an example of this updated to a function:

function log() {
if [[ -z "$1" ]]; then
echo "Usage: log \"<message>\""
return 1
fi
echo "- $1" >> ~/log/`date +%Y-%m`.log
}

## Happy logging

That's it. Happy logging!

In an upcoming post I'll figure out how to send this log as an email to a small number of subscribers.

Note: I'm not sure if I'm allowed to post a little how-to like this on HN. I don't think it's specifically disallowed in the rules but I'm not sure I've seen a lot of it either. Let me know if you think this doesn't belong here.