I am following the cakephp documentation to implement the database logger function. I got everything working after creating the logger class in
app/Lib/Log/Engine/DatabaseLogger.php
and add these lines to bootstrap
CakeLog::config('otherFile', array(
'engine' => 'DatabaseLogger',
'model' => 'LogEntry',
// ...
));
After this I can log to the database by calling
CakeLog::info(message);
Everything works fine, but then I ran into problem trying to automatically log the user’s username and ip address. I have searched for many solutions onlien but I do not seems to be able to find an answer. Is it possible to access the controller in the DatabaseLogger class?
You don’t need access to the controller
The information you need is effectively global, you don’t need to access the controller object to get it.
Client IP
You can use the Router::getRequest method to get the current request object and retrieve the client IP from that:
Or simply use the env method:
Current User’s name
For this, just use the static session interface: