# Capturing Script Logs

DevFeed: [Capturing Script Logs](<https://devfeed.tech/articles/capturing-script-logs-19827.md>)

Original publisher: [Read original article](<https://tech.gc.com/capturing-script-logs/>)

Author: GameChanger

Published: 2020-12-01T00:00:00Z

Content type: tutorial

Language: en

Sources: [GameChanger](<https://devfeed.tech/sources/gamechanger.md>)

Topics: [Script](<https://devfeed.tech/topics/script.md>), [Scripting, bash](<https://devfeed.tech/topics/scripting-bash.md>), [Processes](<https://devfeed.tech/topics/processes.md>), [Ansible](<https://devfeed.tech/topics/ansible.md>), [Slack](<https://devfeed.tech/topics/slack.md>), [SIEM, Security, Observability](<https://devfeed.tech/topics/siem-security-observability.md>)

Tags: [ansible](<https://devfeed.tech/tags/ansible.md>), [bash](<https://devfeed.tech/tags/bash.md>), [dev](<https://devfeed.tech/tags/dev.md>), [logs](<https://devfeed.tech/tags/logs.md>), [metrics](<https://devfeed.tech/tags/metrics.md>), [monitor](<https://devfeed.tech/tags/monitor.md>), [operations](<https://devfeed.tech/tags/operations.md>), [process](<https://devfeed.tech/tags/process.md>), [slack](<https://devfeed.tech/tags/slack.md>)

## AI overview

This tutorial explains how GameChanger captures script output and reports errors to Slack when DataDog integration is unavailable or its alerts lack sufficient context. It introduces the Bash tee command and process substitution as building blocks for capturing and forwarding script logs.

## Source excerpt

At GameChanger, we use scripts in many of our flows such as during deploys or running Ansible while booting new instances. Some of these flows are critical to our operations and require good visibility. Traditionally we tried to send both metrics and logs to DataDog where we can both monitor what scripts are doing and set alerts on various metrics. DataDog integration, however, is not always available such as during the instance boot. In addition, sometimes a DataDog alert does not provide enough context of what failed and most importantly why it failed. It simply states that some threshold was reached. To investigate the issue requires more manual steps by looking for the appropriate log which is not always intuitive. That is why recently at GameChanger we started integrating Slack error reporting directly into some of our critical scripts. This post describes the exact mechanism of how that is achieved since it uses a really cool bash trick. Useful Commands First some background on some of the useful commands. tee Anyone? Tee is a really useful command. It captures an output from a script and both echoes it to standard out as well as forwards it to a file. This allows to both see an output as well as capture the same output for later use. For example: $ echo hello there | tee hello.log hello there $ cat hello.log hello there Process Substitution Some commands only work with files. For example a classic diff: $ echo one > one.txt $ echo two > two.txt $ diff -u one.txt two.txt --- one.txt +++ two.txt @@ -1 +1 @@ -one +two Sometimes however it is convenient to be able to refer to an output of commands as a file without manually creating a temporary file. This is what process substitution allows to do. Same example as above but with process substitution: $ diff -u <(echo one) <(echo two) --- /dev/fd/63 2020-11-30 12:33:08.539905663 -0500 +++ /dev/fd/62 2020-11-30 12:33:08.540272967 -0500 @@ -1 +1 @@ -one +two This is the output form of process substitution which uses