# Shell script logging tips

**URL:** <https://forums.balena.io/t/shell-script-logging-tips/351772>\
**Category:** Product support\
**Created:** [February 5, 2022, 12:11pm UTC](https://forums.balena.io/t/shell-script-logging-tips/351772 "2022-02-05T12:11:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barryjump](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@barryjump](https://forums.balena.io/u/barryjump)\
**Post date:** [February 5, 2022, 12:11pm UTC](https://forums.balena.io/t/shell-script-logging-tips/351772/1 "2022-02-05T12:11:27Z")

</div>

Hi all, just wanted to share a great tip I uncovered last week. Lots of us use shell scripts and use echo to debug in the console. If you want to take it a bit further and capture your script logs in a structure JSON way, especially if you’re shipping those logs off to Cloudwatch, Datadog, Splunk, etc. this might be useful.

What I like about this approach, all you have to do is drop these functions at the top of your script and swap out your `echo` lines with either `info` or `error` followed by your message.

```auto
# Define the timestamp function
# Customize the date formatter as needed.
function timestamp(){
  date "+%s" # Here we're using unix timestamp
}

function info(){
    message="$1"
    level=INFO
    echo '{}' | \
    jq --monochrome-output \
        --compact-output \
        --raw-output \
        --arg timestamp "$(timestamp)" \
        --arg level "$level" \
        --arg message "$message" \
        --arg user "$USER" \
        --arg file $(basename $BASH_SOURCE) \
        '.timestamp=$timestamp|.level=$level|.message=$message|.user=$user|.file=$file' >> logs.log
    echo "$level: $message"
}

function error(){
    message="$1"
    level=ERROR
    echo '{}' | \
    jq --monochrome-output \
        --compact-output \
        --raw-output \
        --arg timestamp "$(timestamp)" \
        --arg level "$level" \
        --arg message "$message" \
        --arg user "$USER" \
        --arg file $(basename $BASH_SOURCE) \
        '.timestamp=$timestamp|.level=$level|.message=$message|.user=$user|.file=$file' >> logs.log
    echo "\033[91m$level: $message\033[0m"
}

info "this is an info message"
error "this is an error message"

```

Above defines two log levels `INFO` and `ERROR`.  
You can define more, but for simple shell scripts it’s unlikely you need more that those two levels.

Result in `logs.log`:

```auto
{"timestamp":"1644062249","level":"INFO","message":"this is an info message","user":"admin","file":"logstest.sh"}
{"timestamp":"1644062249","level":"ERROR","message":"this is an error message","user":"admin","file":"logstest.sh"}

```

And results in console:

```auto
INFO: this is an info message
ERROR: this is an error message

```

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [May 12, 2022, 11:52am UTC](https://forums.balena.io/t/shell-script-logging-tips/351772/2 "2022-05-12T11:52:26Z")

</div>

Hi @barryjump

thanks for sharing. i’m sure others will find this helpful too
