log_lua记录行为日志

上篇讲到ngx_lua模块的安装,既然站在巨人的肩膀上,那么接下来就是把日志放入到文件中

用的lua的脚本语言,IDE工具是mac  Sublime Text 3,以及windows Lua Editor

local cjson  = require "cjson"

local logging = {}

local function incr(dict, key, increment)
    increment = increment or 1
    local newval, err = dict:incr(key, increment)
    
    if not newval or err then
      dict:add(key, increment)
      newval = increment
    end
    
    return newval
end

function logging.log_response_time(dict, value)
    local sum_key = "request_time-sum"
    local count_key = "request_time-count"
    local start_time_key = "request_time-start_time"
    local request_time_key = value
    
    dict:add(start_time_key, ngx.now())
    
    incr(dict, sum_key, value)
    incr(dict, count_key)
    incr(dict, request_time_key)
    
    return true
end

function logging.log_response_status(dict)
    local response_code = ngx.var.status or ""
    local response_key = "response_code-"..response_code
    
    incr(dict, response_key)
    
    return true
end

function logging.get_timing_values(dict)
    local keys = dict:get_keys(0)
    local response_times = {}
    
    for k,v in pairs(keys) do
        if tonumber(v) then
            val = dict:get(v)
            response_times[#response_times] = {response_time = v, count = val}
        end
    end
    
    dict:flush_all()
    
    return cjson.encode(response_times)
end

function logging.get_timing_summary(dict)
    local sum_key = "request_time-sum"
    local count_key = "request_time-count"
    local start_time_key = "request_time-start_time"
    local keys = dict:get_keys(0)
    local start_time = dict:get(start_time_key)
    local count = dict:get(count_key) or 0
    local sum = dict:get(sum_key) or 0
    local mean = 0
    local stdevsum = 0
    local qps = 0
    
    if count > 0 then mean = sum / count end
    
    for k,v in pairs(keys) do
        if tonumber(v) then
            val = dict:get(v)
            maxval = maxval and (maxval > val and maxval or val) or val
            if oldmax ~= maxval then mode = v modecount = val end
            oldmax = maxval
            mintime = mintime and (mintime < v and mintime or v) or v
            maxtime = maxtime and (maxtime > v and maxtime or v) or v
            stdevsum = stdevsum + (mean - v)^2
        end
    end
    
    local stdev = math.sqrt(stdevsum/count)
    
    local elapsed_time = 0
    
    if start_time then
        elapsed_time = ngx.now() - start_time
    end
    
    if elapsed_time > 0 then
        qps = count / elapsed_time
    end
    
    dict:flush_all()
    
    local summary = {count=count,
                     qps=qps,
                     mean=mean,
                     mode=mode,
                     modecount=modecount,
                     mintime=mintime,
                     maxtime=maxtime,
                     stdev=stdev,
                     elapsed_time=elapsed_time}
    
    return cjson.encode(summary)
end

function logging.get_response_summary(dict)
    local response_key_prefix = "response_code-"
    local response_codes = {}
    local keys = dict:get_keys(0)
    
    for k,v in pairs(keys) do
        if v:find(response_key_prefix, 1, true) then
            val = dict:get(v)
            response_codes[#response_codes] = {response_code = v, count = val}
        end
    end
    
    return cjson.encode(response_codes)
end

return logging

 在Hub上弄了个案例,然后简单地把日志记录了下来

 

看到一个缩略图的解决方案

-- 写入文件
local function writefile(filename, info)
    local wfile=io.open(filename, "w") --写入文件(w覆盖)
    assert(wfile)  --打开时验证是否出错		
    wfile:write(info)  --写入传入的内容
    wfile:close()  --调用结束后记得关闭
end

-- 检测路径是否目录
local function is_dir(sPath)
    if type(sPath) ~= "string" then return false end

    local response = os.execute( "cd " .. sPath )
    if response == 0 then
        return true
    end
    return false
end

-- 检测文件是否存在
local file_exists = function(name)
    local f=io.open(name,"r")
    if f~=nil then io.close(f) return true else return false end
end

local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
if index then 
    originalUri = string.sub(ngx.var.uri, 0, index-2);  
    area = string.sub(ngx.var.uri, index);  
    index = string.find(area, "([.])");  
    area = string.sub(area, 0, index-1);  

    local index = string.find(originalFile, "([0-9]+)x([0-9]+)");  
    originalFile = string.sub(originalFile, 0, index-2)
end

-- check original file
if not file_exists(originalFile) then
    local fileid = string.sub(originalUri, 2);
    -- main
    local fastdfs = require('restyfastdfs')
    local fdfs = fastdfs:new()
    fdfs:set_tracker("192.168.1.113", 22122)
    fdfs:set_timeout(1000)
    fdfs:set_tracker_keepalive(0, 100)
    fdfs:set_storage_keepalive(0, 100)
    local data = fdfs:do_download(fileid)
    if data then
       -- check image dir
        if not is_dir(ngx.var.image_dir) then
            os.execute("mkdir -p " .. ngx.var.image_dir)
        end
        writefile(originalFile, data)
    end
end

-- 创建缩略图
local image_sizes = {"80x80", "800x600", "40x40", "60x60"};  
function table.contains(table, element)  
    for _, value in pairs(table) do  
        if value == element then
            return true  
        end  
    end  
    return false  
end 

if table.contains(image_sizes, area) then  
    local command = "gm convert " .. originalFile  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;  
    os.execute(command);  
end;

if file_exists(ngx.var.file) then
    --ngx.req.set_uri(ngx.var.uri, true);  
    ngx.exec(ngx.var.uri)
else
    ngx.exit(404)
end

 

你可能感兴趣的:(lua,luaeditor)