How to output to console from Redis Lua script?
Asked Answered
P

5

16

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"
Pagandom answered 7/10, 2014 at 18:32 Comment(0)
P
19

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

Pagandom answered 7/10, 2014 at 20:13 Comment(2)
I've recently posted this collection of methods that are all aimed to help in tracing and debugging Lua in Redis - hope it helps :) redislabs.com/blog/…Cosmorama
Eg: redis.log(redis.LOG_DEBUG, "example text")Alisealisen
G
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.
Gibbosity answered 10/2, 2021 at 13:32 Comment(0)
L
3

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).

Lentha answered 9/12, 2014 at 9:4 Comment(0)
C
1

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

Coccus answered 28/1, 2015 at 9:40 Comment(1)
The question is asking how to write to the console from the script. This answer is only writing console output using Java (System.out.println).Forbade
C
1

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
Cowherb answered 8/3, 2022 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.