add log level

This commit is contained in:
he2ss 2021-09-09 11:08:14 +02:00
parent 3c235c813f
commit 371556232f
4 changed files with 30 additions and 6 deletions

View file

@ -51,7 +51,8 @@ The configuration is located by default in `/usr/local/lua/crowdsec/crowdsec.con
```
API_URL=http://localhost:8080 <-- the API url
API_KEY= <-- the API Key generated with `cscli bouncers add -n <bouncer_name>`
LOG_FILE=/tmp/lua_mod.log <-- path to log file
LOG_FILE=/tmp/lua_mod.log <-- path to log file
LOG_LEVEL=INFO <-- log level (INFO, WARN, DEBUG, ERROR, FATAL)
CACHE_EXPIRATION=1 <-- in seconds
CACHE_SIZE=1000 <-- cache size
REQUEST_TIMEOUT=0.2 <-- Maximum duration in seconds for a request to LAPI

View file

@ -25,6 +25,27 @@ end
local csmod = {}
function getLogLevel( level )
if level and type(level)=="string"
if level == "INFO"
return logging.INFO
end
if level == "WARN"
return logging.WARN
end
if level == "DEBUG"
return logging.DEBUG
end
if level == "ERROR"
return logging.ERROR
end
if level == "FATAL"
return logging.FATAL
end
end
return logging.INFO
end
-- init function
function csmod.init(configFile, userAgent)
local conf, err = config.loadConfig(configFile)
@ -34,6 +55,7 @@ function csmod.init(configFile, userAgent)
runtime.conf = conf
local logger = log_file(conf["LOG_FILE"])
logger:setLevel (getLogLevel(conf["LOG_LEVEL"]))
runtime.logger = logger
runtime.userAgent = userAgent
local c, err = lrucache.new(conf["CACHE_SIZE"])
@ -105,12 +127,12 @@ function csmod.allowIp(ip)
return true, nil
end
if resp == "null" then -- no result from API, no decision for this IP
-- set ip in cache and DON'T block it
runtime.cache:set(ip, true,runtime.conf["CACHE_EXPIRATION"])
-- setLevel (logging.) ip in cache and DON'T block it
runtime.cache:setLevel (logging.)(ip, true,runtime.conf["CACHE_EXPIRATION"])
return true, nil
end
-- set ip in cache and block it
runtime.cache:set(ip, false,runtime.conf["CACHE_EXPIRATION"])
-- setLevel (logging.) ip in cache and block it
runtime.cache:setLevel (logging.)(ip, false,runtime.conf["CACHE_EXPIRATION"])
return false, nil
end

View file

@ -35,7 +35,7 @@ function config.loadConfig(file)
return nil, "File ".. file .." doesn't exist"
end
local conf = {}
local valid_params = {'API_URL', 'API_KEY', 'LOG_FILE'}
local valid_params = {'API_URL', 'API_KEY', 'LOG_FILE', 'LOG_LEVEL'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE', 'REQUEST_TIMEOUT'}
local default_values = {
['REQUEST_TIMEOUT'] = 0.2

View file

@ -1,5 +1,6 @@
API_URL=http://127.0.0.1:8080
API_KEY= ${API_KEY}
LOG_FILE=/tmp/lua_mod.log
LOG_LEVEL=INFO
CACHE_EXPIRATION=1
CACHE_SIZE=1000