1
+ import logging
1
2
import os
2
3
import time
3
4
import threading
19
20
from ._zset import ZSet
20
21
21
22
23
+ LOGGER = logging .getLogger ('fakeredis' )
24
+ REDIS_LOG_LEVELS = {
25
+ b'LOG_DEBUG' : 0 ,
26
+ b'LOG_VERBOSE' : 1 ,
27
+ b'LOG_NOTICE' : 2 ,
28
+ b'LOG_WARNING' : 3
29
+ }
30
+ REDIS_LOG_LEVELS_TO_LOGGING = {
31
+ 0 : logging .DEBUG ,
32
+ 1 : logging .INFO ,
33
+ 2 : logging .INFO ,
34
+ 3 : logging .WARNING
35
+ }
36
+
22
37
MAX_STRING_SIZE = 512 * 1024 * 1024
23
38
24
39
INVALID_EXPIRE_MSG = "invalid expire time in {}"
59
74
BAD_COMMAND_IN_PUBSUB_MSG = \
60
75
"only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context"
61
76
CONNECTION_ERROR_MSG = "FakeRedis is emulating a connection error."
77
+ REQUIRES_MORE_ARGS = "{} requires {} arguments or more."
78
+ LOG_WRONG_FIRST_ARG = "First argument must be a number (log level)."
79
+ LOG_INVALID_DEBUG_LEVEL = "Invalid debug level."
62
80
63
81
FLAG_NO_SCRIPT = 's' # Command not allowed in scripts
64
82
@@ -2312,9 +2330,20 @@ def _lua_redis_pcall(self, lua_runtime, expected_globals, op, *args):
2312
2330
except Exception as ex :
2313
2331
return lua_runtime .table_from ({b"err" : str (ex )})
2314
2332
2333
+ def _lua_redis_log (self , lua_runtime , expected_globals , lvl , * args ):
2334
+ self ._check_for_lua_globals (lua_runtime , expected_globals )
2335
+ if len (args ) < 1 :
2336
+ raise redis .ResponseError (REQUIRES_MORE_ARGS .format ("redis.log()" , "two" ))
2337
+ if lvl not in REDIS_LOG_LEVELS .values ():
2338
+ raise redis .ResponseError (LOG_INVALID_DEBUG_LEVEL )
2339
+ msg = ' ' .join ([x .decode ('utf-8' )
2340
+ if isinstance (x , bytes ) else str (x )
2341
+ for x in args if not isinstance (x , bool )])
2342
+ LOGGER .log (REDIS_LOG_LEVELS_TO_LOGGING [lvl ], msg )
2343
+
2315
2344
@command ((bytes , Int ), (bytes ,), flags = 's' )
2316
2345
def eval (self , script , numkeys , * keys_and_args ):
2317
- from lupa import LuaRuntime , LuaError
2346
+ from lupa import LuaRuntime , LuaError , as_attrgetter
2318
2347
2319
2348
if numkeys > len (keys_and_args ):
2320
2349
raise redis .ResponseError (TOO_MANY_KEYS_MSG )
@@ -2326,10 +2355,14 @@ def eval(self, script, numkeys, *keys_and_args):
2326
2355
2327
2356
set_globals = lua_runtime .eval (
2328
2357
"""
2329
- function(keys, argv, redis_call, redis_pcall)
2358
+ function(keys, argv, redis_call, redis_pcall, redis_log, redis_log_levels )
2330
2359
redis = {}
2331
2360
redis.call = redis_call
2332
2361
redis.pcall = redis_pcall
2362
+ redis.log = redis_log
2363
+ for level, pylevel in python.iterex(redis_log_levels.items()) do
2364
+ redis[level] = pylevel
2365
+ end
2333
2366
redis.error_reply = function(msg) return {err=msg} end
2334
2367
redis.status_reply = function(msg) return {ok=msg} end
2335
2368
KEYS = keys
@@ -2342,7 +2375,9 @@ def eval(self, script, numkeys, *keys_and_args):
2342
2375
lua_runtime .table_from (keys_and_args [:numkeys ]),
2343
2376
lua_runtime .table_from (keys_and_args [numkeys :]),
2344
2377
functools .partial (self ._lua_redis_call , lua_runtime , expected_globals ),
2345
- functools .partial (self ._lua_redis_pcall , lua_runtime , expected_globals )
2378
+ functools .partial (self ._lua_redis_pcall , lua_runtime , expected_globals ),
2379
+ functools .partial (self ._lua_redis_log , lua_runtime , expected_globals ),
2380
+ as_attrgetter (REDIS_LOG_LEVELS )
2346
2381
)
2347
2382
expected_globals .update (lua_runtime .globals ().keys ())
2348
2383
0 commit comments