Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output to console from Redis Lua script?

Tags:

redis

lua

Why does this not print 'hello'?

$ redis-cli
127.0.0.1:6379> eval "print( 'hello' )" 0
(nil)
127.0.0.1:6379>

Running 2.8.14 on Mac OS X, 2.8.12 on Windows 7.

I'm calling Lua scripts from Jedis. Developing these is like building a ship a bottle, wearing mittens, while someone's punching me in the face. My ultimate goal is somehow recreate a semi functional development stack via print trace statements, debug, whatever.

My workaround is for my Lua script is use a Redis list called 'log', returning it to Jedis, and then dumping the contents. Kinda like this:

redis.call( 'del', 'log' )
redis.call( 'rpush', 'log', 'trace statement 1' )
redis.call( 'rpush', 'log', 'trace statement 2' )

...

redis.call( 'lrange', 'log', 0, -1 )

Thanks in advance for any tips, help, etc.

Update: Just noticed the 'hello' does output via the terminal window for redis-server executable. Clever. So now I a terminal each for redis-server, redis-cli interactive, and redis-cli monitor.

Update 2: Just figured out I can kinda print trace statements to the redis-cli monitor like this:

eval "redis.call( 'echo', 'ugh')" 0

Which appears kinda like this:

123.456 [0 127.0.0.1:57709] "eval" "redis.call( 'echo', 'ugh')" "0"
123.456 [0 lua] "echo" "ugh"
like image 395
Jason Osgood Avatar asked Oct 07 '14 18:10

Jason Osgood


1 Answers

I finally figured out there's redis.log(loglevel, message), which also writes to redis-server's console output.

like image 128
Jason Osgood Avatar answered Nov 16 '22 02:11

Jason Osgood