From 371556232f70d9427ec6b146c4373d19bffe1b93 Mon Sep 17 00:00:00 2001 From: he2ss Date: Thu, 9 Sep 2021 11:08:14 +0200 Subject: [PATCH] add log level --- README.md | 3 ++- lib/CrowdSec.lua | 30 ++++++++++++++++++++++++++---- lib/config.lua | 2 +- template.conf | 1 + 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2c20154..a1d2ec4 100644 --- a/README.md +++ b/README.md @@ -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 ` -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 diff --git a/lib/CrowdSec.lua b/lib/CrowdSec.lua index fd10cf4..238f1ee 100644 --- a/lib/CrowdSec.lua +++ b/lib/CrowdSec.lua @@ -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 diff --git a/lib/config.lua b/lib/config.lua index d308bea..812d8f9 100644 --- a/lib/config.lua +++ b/lib/config.lua @@ -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 diff --git a/template.conf b/template.conf index 1ab2f33..8026120 100644 --- a/template.conf +++ b/template.conf @@ -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