Do not separate new lines from contents in logs

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the logging category.

Last Updated: 2024-03-27

I had something like this in my deploy script

#!/usr/bin/env bash
echo "Pushing $git_branch to Github"
echo
git push

echo "Deploying staging environment to Heroku"
echo
git push staging

echo "Migrating database"
echo
heroku run php artisan migrate --remote staging

The output, when it was run, was grouped weirdly - specifically, the label statements were separated from the things they label (rather than the separation being between each label/value group):

Pushing master to Github

Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 503 bytes | 503.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 3 local objects.
To github.com:daanlo/shield-doc-backend.git
   18f4e1e..3e7b349  master -> master
Deploying staging environment to Heroku

Enumerating objects: 22, done.ing git

The fix was to instead place echo before each label line:

#!/usr/bin/env bash
echo
echo "Pushing $git_branch to Github"
git push

echo
echo "Deploying staging environment to Heroku"
git push staging

echo
echo "Migrating database"
heroku run php artisan migrate --remote staging

This printed


Pushing master to Github
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 503 bytes | 503.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 3 local objects.
To github.com:daanlo/shield-doc-backend.git
   18f4e1e..3e7b349  master -> master

Deploying staging environment to Heroku
Enumerating objects: 22, done.
...

Lesson

Do not separate label lines from their contents with newlines in logs.