Filtered Redis log of executed commands


If you deal with some kind of database, you should know by now that Redis is TEH AWESOME.

As part of the provided tools, the redis-cli binary is an invaluable tool to connect to a Redis database, via a TCP port or a UNIX socket (my preferred way).

Once connected to a Redis server via redis-cli you can send the MONITOR command. The clients then stops listening to regular inputs and starts to log every command received by the server, with a timestamp (seconds and microseconds).

Here is an output sample :

1353711173.069255 "MONITOR"
1353711204.631496 "SET" "users:1:login" "alfred"
1353711224.119123 "SET" "users:2:login" "tom"
1353711281.926336 "SADD" "users" "1" "2"
1353711297.878012 "SMEMBERS" "users"

The problem is that there doesn’t seem to be a built-in way to filter or save the command logs.

Redis being a good UNIX citizen acts like any other command regarding to input, outputs and pipes.

Let’s assume that the server is reachable on the default TCP port,

  • To simply print a command log
    echo "MONITOR" | redis-cli
  • To print a filtered command log (only “add to Sets”)
    echo "MONITOR" | redis-cli | grep -i 'SADD'
  • To save a command log to a file
    echo "MONITOR" | redis-cli > redis.log
  • To save a filtered command log to a file
    echo "MONITOR" | redis-cli | grep -i 'SADD' > redis.log

NB : Redis is case insensitive for the commands it accepts.