</para>
<para>
To log messages with the main PowerDNS Recursor process, use <function>pdnslog(message)</function>. Available since version 3.2.
+ pdnslog can also write out to a syslog loglevel if specified. Use <function>pdnslog(message, pdns.loglevels.LEVEL)</function> with the correct pdns.loglevels entry. Entries are listed in the following table:
+ <table>
+ <title>pdnslog() loglevels</title>
+ <tgroup cols="2">
+ <tbody>
+ <row><entry>All</entry><entry>pdns.loglevels.All</entry></row>
+ <row><entry>NTLog</entry><entry>pdns.loglevels.NTLog</entry></row>
+ <row><entry>Alert</entry><entry>pdns.loglevels.Alert</entry></row>
+ <row><entry>Critical</entry><entry>pdns.loglevels.Critical</entry></row>
+ <row><entry>Error</entry><entry>pdns.loglevels.Error</entry></row>
+ <row><entry>Warning</entry><entry>pdns.loglevels.Warning</entry></row>
+ <row><entry>Notice</entry><entry>pdns.loglevels.Notice</entry></row>
+ <row><entry>Info</entry><entry>pdns.loglevels.Info</entry></row>
+ <row><entry>Debug</entry><entry>pdns.loglevels.Debug</entry></row>
+ <row><entry>None</entry><entry>pdns.loglevels.None</entry></row>
+ </tbody>
+ </tgroup>
+ </table>
+ pdnslog(message) will write out to Info by default.
</para>
<para>
To retrieve the IP address on which a query was received, use <function>getlocaladdress()</function>. Available since version 3.2.
lua_gettable(lua, -2); // replace by the first entry of our table we hope
bool ret=false;
+
if(!lua_isnil(lua, -1)) {
value = (uint32_t)lua_tonumber(lua, -1);
ret=true;
lua_settable(lua, -3); // pushes the table we just built into the master table at position pushed above
}
}
-// this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values
+// override the __index metatable under loglevels to return Logger::Error to account for nil accesses to the loglevels table
+int loglevels_index(lua_State* lua)
+{
+ lua_pushnumber(lua, Logger::Error);
+ return 1;
+}
+// push the loglevel subtable onto the stack that will eventually be the pdns table
void pushSyslogSecurityLevelTable(lua_State* lua)
{
+ lua_newtable(lua);
+// this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values
lua_pushnumber(lua, Logger::All);
lua_setfield(lua, -2, "All");
lua_pushnumber(lua, Logger::NTLog);
lua_setfield(lua, -2, "Debug");
lua_pushnumber(lua, Logger::None);
lua_setfield(lua, -2, "None");
+ lua_createtable(lua, 0, 1);
+ lua_pushcfunction(lua, loglevels_index);
+ lua_setfield(lua, -2, "__index");
+ lua_setmetatable(lua, -2);
}
int getLuaTableLength(lua_State* lua, int depth)
{
} else if(argc >= 2) {
string message=lua_tostring(lua, 1);
int urgencylevel = lua_tonumber(lua, 2);
- switch(urgencylevel)
- {
- case Logger::Alert:
- theL()<<Logger::Alert<<message<<endl;
- break;
- case Logger::Critical:
- theL()<<Logger::Critical<<message<<endl;
- break;
- case Logger::Error:
- theL()<<Logger::Error<<message<<endl;
- break;
- case Logger::Warning:
- theL()<<Logger::Warning<<message<<endl;
- break;
- case Logger::Notice:
- theL()<<Logger::Notice<<message<<endl;
- break;
- case Logger::Info:
- theL()<<Logger::Info<<message<<endl;
- break;
- case Logger::Debug:
- theL()<<Logger::Debug<<message<<endl;
- break;
- case Logger::All:
- theL()<<Logger::All<<message<<endl;
- break;
- case Logger::NTLog:
- theL()<<Logger::All<<message<<endl;
- break;
- case Logger::None:
- theL()<<Logger::None<<message<<endl;
- break;
- default:
- theL()<<Logger::Error<<message<<endl;
- break;
- }
+ theL()<<urgencylevel<<" "<<message<<endl;
}
return 0;
}
lua_setfield(d_lua, -2, "REFUSED");
// set syslog codes used by Logger/enum Urgency
pushSyslogSecurityLevelTable(d_lua);
+ lua_setfield(d_lua, -2, "loglevels");
lua_setglobal(d_lua, "pdns");
+pdnslog("pdns-recursor starting!", pdns.loglevels.Info)
function endswith(s, send)
return #s >= #send and s:find(send, #s-#send+1, true) and true or false
end
function preresolve ( remoteip, domain, qtype )
+
print ("prequery handler called for: ", remoteip, getlocaladdress(), domain, qtype)
- pdnslog("a test message.. received query from "..remoteip.." on "..getlocaladdress());
+ pdnslog("a test message.. received query from "..remoteip.." on "..getlocaladdress(), pdns.loglevels.Info);
if endswith(domain, "f.f.7.7.b.1.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.")
then
if domain == "www.donotcache.org."
then
- print("making sure www.donotcache.org will never end up in the cache")
+ print("making sure www.donotcache.org will never end up in the cache", pdns.loglevels.Debug)
setvariable()
return -1, {}
end
function nxdomain ( remoteip, domain, qtype )
print ("nxhandler called for: ", remoteip, getlocaladdress(), domain, qtype, pdns.AAAA)
- if qtype ~= pdns.A then return -1, {} end -- only A records
- if not string.find(domain, "^www%.") then return -1, {} end -- only things that start with www.
+ if qtype ~= pdns.A then
+ pdnslog("Only A records", pdns.loglevels.Error)
+ return -1, {}
+ end -- only A records
+ if not string.find(domain, "^www%.") then
+ pdnslog("Only strings that start with www.", pdns.loglevels.Error)
+ return -1, {}
+ end -- only things that start with www.
setvariable()
if matchnetmask(remoteip, {"127.0.0.1/32", "10.1.0.0/16"})
-- records contains the entire packet, ready for your modifying pleasure
function postresolve ( remoteip, domain, qtype, records, origrcode )
- print ("postresolve called for: ", remoteip, getlocaladdress(), domain, qtype, origrcode)
+ print ("postresolve called for: ", remoteip, getlocaladdress(), domain, qtype, origrcode, pdns.loglevels.Info)
for key,val in ipairs(records)
do
pdnslog ("q: ".. qname.." "..qtype)
if qtype == pdns.A and qname == "www.domain.com"
then
- pdnslog ("calling dnspacket:setRcode")
+ pdnslog ("calling dnspacket:setRcode", pdns.loglevels.Debug)
dnspacket:setRcode(pdns.NXDOMAIN)
- pdnslog ("called dnspacket:setRcode")
- pdnslog ("adding records")
+ pdnslog ("called dnspacket:setRcode", pdns.loglevels.Debug)
+ pdnslog ("adding records", pdns.loglevels.Debug)
ret = {}
ret[1] = {qname=qname, qtype=qtype, content="1.2.3.4", place=2}
ret[2] = {qname=qname, qtype=pdns.TXT, content=os.date("Retrieved at %Y-%m-%d %H:%M"), ttl=ttl}
dnspacket:addRecords(ret)
- pdnslog ("returning true")
+ pdnslog ("returning true", pdns.loglevels.Debug)
return true
end
pdnslog ("returning false")