How can I log all notify-send actions?

Solution 1:

Even didn't need a full script...
...but put it in the form of a script:

#!/bin/bash

file=$1

dbus-monitor "interface='org.freedesktop.Notifications'" |\
 grep --line-buffered "string" |\
 grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
 grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
 grep --line-buffered -v '^\s*$' |\
 xargs -I '{}' echo {} >> $file

To run it

  • Copy the "script" into an empty file, save it as keep_log.sh
  • Run it with the logfile as argument with the command

    /bin/bash /path/to/keep_log.sh /path/to/log.txt
    

The answer was retrieved from an earlier answer (not a dupe), in which this application of the method was mentioned as an example.

The answer I gave there, on its own turn, was based on this very nice answer, in which is explained that the method uses dbus-monitor to intercept the contents of notify-send. By editing the example there, we can make it write notify-send messages to a (log-) file.

Or, more elegant

...would be to add the date to the logfile, producing a logfile like:

---di 10 mei 2016 17:37:20 CEST---
SOme kind of a message!
---di 10 mei 2016 17:37:20 CEST---
The last message was misspelled so here i9s another one

In that case, the script would be:

#!/bin/bash

logfile=$1

dbus-monitor "interface='org.freedesktop.Notifications'" |\
grep --line-buffered "string" |\
grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
grep --line-buffered -v '^\s*$' |\
xargs -I '{}' \
printf "---$( date )---\n"{}"\n" >> $logfile