We are going to implement user request logging in the blog web app that we created in Chapter 2, Building your First MongoDB Powered Web App. We will build a Logger class that will handle logging. We will modify the blog.php
file, the script used for viewing individual blog posts, to log page views through the logger module:
1. Open up your text editor and create a new file named
log.php
. Add the following code to it:<?php require_once('dbconnection.php'); define('LOGNAME', 'access_log'); class Logger { private $_dbconnection; private $_db; public function __construct() { $this->_dbconnection = DBConnection::instantiate(); //obtain a reference to the collection where the data //will be logged $this->_collection = $this->_dbconnection ->getCollection(LOGNAME); } public function logRequest($data = array()) { $request = array(); //obtain HTTP request information by accessing $_SERVER $request['page'] = $_SERVER['SCRIPT_NAME...