Bash to slack error log report

Multi tool use
Multi tool use


Bash to slack error log report



I currently have a script that is tailing our error log and sending logs to our slack channel. How can I get it to send an entire error and not line by line. This is what I have currently and one error gets sent as hundreds of single line posts.


#!/bin/bash
tail -f "$1" | while read LINE; do
(echo "$LINE") && curl -X POST --silent --data-urlencode
"payload={"text": "```$1 $(echo $LINE | sed "s/"/'/g")```"}" "$2";
done



Is there a way I can get bash to do this?



This is how it looks in slack:


```/var/log/php_error_log Stack trace:```
```/var/log/php_error_log #0 /opt/library/Zend/Mail/Protocol/Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://mail...')```
```/var/log/php_error_log #1 /opt/library/Zend/Mail/Transport/Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()```
```/var/log/php_error_log #2 /opt/library/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()```
```/var/log/php_error_log #3 /opt/library/Zend/Mail.php(1197): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))```
```/var/log/php_error_log #4 /opt//cronjobs/automate_ro.php(472): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))```
```/var/log/php_error_log #5 {main}```



This is how its meant to be formatted


```/var/log/php_error_log Stack trace:
/var/log/php_error_log #0 /opt/library/Zend/Mail/Protocol/Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://mail.grat...')
/var/log/php_error_log #1 /opt/library/Zend/Mail/Transport/Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()
/var/log/php_error_log #2 /opt/library/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
/var/log/php_error_log #3 /opt/library/Zend/Mail.php(1197): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
/var/log/php_error_log #4 /opt//cronjobs/automate_ro.php(472): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))
/var/log/php_error_log #5 {main}```





What would constitute an "Entire Error". Would that be the whole log? Or just lines from the log since the last send? Or something else entirely?
– JNevill
Jul 2 at 13:10





This is how it formats a multiline error
– Jed
Jul 2 at 14:22




2 Answers
2



Use jq to create the payload instead of trying to escape things yourself.


jq


post_log () {
log=$1
url=$2

json=$( jq --argjson t "$log" '{text: "```($t)```"}' )

curl -X POST --silent --data-urlencode "payload=$json" "$url"
}

tail -f "$1" | while read LINE; do
echo "$LINE"
post_log "$LINE" "$2"
done





Fair enough but is this code to change the way it reads line by line I don't think it is.
– Jed
Jul 2 at 18:31





Oh, multiple input lines per log message. Well, what signals the end of a stack trace?
– chepner
Jul 2 at 18:33





Its a normal php error log I am not sure what signals the end of the trace
– Jed
Jul 3 at 14:41





Then it's going to be difficult to programmatically bunch up the required lines into a single request.
– chepner
Jul 3 at 15:08



why not implement monolog in the application? This approach seems a goldberg machine to me (no offense).



https://seldaek.github.io/monolog/doc/02-handlers-formatters-processors.html#send-alerts-and-emails





No offence taken at all. I needed the fastest implementation possible and that was the fastest until I had time to add monolog. This is just for the short term
– Jed
Jul 2 at 18:12





@Jed so if you want a dirt approach: why not cat file and send to slack? after that simply truncate, blank or remove.
– m47730
Jul 3 at 10:50


cat file





Yes I am thinking I am going to have to go this way for now. I was hoping there was a way to do it with a tail like feature
– Jed
Jul 3 at 14:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

t5e Nr4kyoaHcy2WrhM vM FjCJM,svg xIf6x,f,tb746gTK QeZg,isnzuGDtmYmlTjVvhGZvggQGqGFFJN H
Z30E6ds uJVYYKIKss5EoHczxL3C2zy1qh5nalv65TwJbU1MK,Iq62,a,yHLSQ7,K s9ykTUorcKqFV,VTWLI937

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications