use a logging system example

This commit is contained in:
uleelx 2015-04-07 17:40:55 +08:00
parent 52988e3b02
commit a9e7905ca1

View File

@ -70,60 +70,65 @@ Quick Look
========== ==========
```lua ```lua
-- This is an logging system example using FlatDB
local flatdb = require("flatdb") local flatdb = require("flatdb")
-- open a directory as a database local logger = flatdb("./log")
-- 'db' is just a plain empty Lua table that can contain pages
local db = flatdb("./db")
-- open or create a page named "default" local count = 0
-- it is also a plain empty Lua table where key-value pair stored in
if not db.default then local function common_log(logger, level, message)
db.default = {} local today = os.date("%Y-%m-%d")
if logger[today] == nil then logger[today] = {} end
if logger[today][level] == nil then logger[today][level] = {} end
table.insert(logger[today][level], {
timestamp = os.time(),
level = level,
message = message
})
count = (count+1)%10
if count == 0 then
logger:save()
end
end end
-- extend db methods for getting values from 'default' page flatdb.hack.debug = function(logger, msg)
flatdb.hack.get = function(db, key) common_log(logger, "debug", msg)
return db.default[key]
end end
-- extend db methods for setting values to 'default' page flatdb.hack.info = function(logger, msg)
flatdb.hack.set = function(db, key, value) common_log(logger, "info", msg)
db.default[key] = value
end end
-- extend db methods for watching new key flatdb.hack.warn = function(logger, msg)
flatdb.hack.guard = function(db, f) common_log(logger, "warn", msg)
setmetatable(db.default, {__newindex = f})
end end
-- get key-value data from 'default' page flatdb.hack.error = function(logger, msg)
print(db:get("hello")) common_log(logger, "error", msg)
end
-- set key-value data to 'default' page flatdb.hack.fatal = function(logger, msg)
db:set("hello", "world") common_log(logger, "fatal", msg)
end
-- get key-value data from 'default' page flatdb.hack.find = function(logger, level, date)
print(db:get("hello")) if logger[date or os.date("%Y-%m-%d")] then
return logger[date or os.date("%Y-%m-%d")][level]
end
end
-- set guard function for i = 1, 10 do
db:guard(function(page, key, value) logger:debug("This is a debug message.")
print("CREATE KEY permission denied!") logger:info("This is an info message.")
end) logger:warn("This is a warn message.")
logger:error("This is an error message.")
logger:fatal("This is a fatal message.")
end
-- try creating new key-value pair to 'default' page local pp = require("pp")
db:set("key1", 1) pp(logger:find("error"))
db:set("key2", 2)
-- update an existing key-value pair
db:set("hello", "bye")
print(db:get("key1")) -- prints nil
print(db:get("key2")) -- prints nil
print(db:get("hello")) -- prints 'bye'
-- store 'default' page to './db/default' file
db:save()
``` ```
@ -142,6 +147,14 @@ API
- **Tables** - **Tables**
- **flatdb**
When a db is loaded, there is two relations below:
*flatdb[dir] --> db*
*flatdb[db] --> dir*
- **flatdb.hack** - **flatdb.hack**
The 'hack' table contains db's methods. There is only one method 'save(db, page)' in it by default. The 'hack' table contains db's methods. There is only one method 'save(db, page)' in it by default.