'How to output to console from Redis Lua script?
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"
Solution 1:[1]
I finally figured out there's redis.log(loglevel, message), which also writes to redis-server's console output.
Solution 2:[2]
There are better ways to develop LUA scripts against redis.
Using lua logs is one way. But you can also publish on a debug topic to have "on demand" logs by subscribing to it.
You can also setup an IDE with lua break points which I think is the best solution for development: http://www.trikoder.net/blog/make-lua-debugging-easier-in-redis-87/
Also, don't forget automatic testing, unit and/or integration tests are helpful (execution against a debug redis instance).
Solution 3:[3]
using jedis, here is how you can do it.. this is an example to use set and get commands.. you need to include the jedis-2.6.0 jar file in classpath.
//jar file - jedis-2.6.0.jar
import redis.clients.jedis.Jedis;
public class MainClass {
public static void main(String[] args){
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
jedis.set("name", "a");
System.out.println("Stored string (b4 lua) : "+ jedis.get("name"));
String script="redis.call('set','name','b')";
jedis.eval(script);
System.out.println("Stored string : "+ jedis.get("name"));
}
}
output : Connection to server sucessfully Stored string (b4 lua) : a Stored string : b
Solution 4:[4]
In case of hosted Redis instances. Sometimes, you don't have access to Redis' log file.
so redis.log(loglevel, message) won't help.
I've ended up with a bit naive solution, but does the trick though, and a bit neater than what the OP did in his question:
you can use a key of Redis it self to store a log lines (items) for the whole execution of the script , in the script itself:
You can write this at the top of the LUA script:
local loglist = "log:my_script"
redis.pcall("DEL", loglist) -- Clear the lines list of the previous execution
redis.pcall("EXPIRE", loglist, 300) -- auto-vanish to preserve a space in case forget to delete it
local function logme(msg)
redis.pcall("RPUSH", loglist, msg)
end
-- and here is an example how to use it in the same script later
logme(string.format("reached phase # %d", cur_phase))
To check up the log after execution finishes. you can list up the key lines (elements) from the CLI or your any other language client.
LRANGE "log:my_script" 0 -1
- DON'T FORGET TO COMMENT IT (or at least the line inside logme function) OUT ON PRODUCTION ENV.
Solution 5:[5]
try this, will record log and print after returned.
local logtable = {}
local function logit(msg)
logtable[#logtable+1] = msg
end
logit("foo")
logit("bar")
return logtable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | randers |
| Solution 2 | zenbeni |
| Solution 3 | Ridhima |
| Solution 4 | |
| Solution 5 | Yang Xu |
