What if we get a simple code which logs for us about about activity. which logs your message as well as the User IP Address, User Agent as well as Referrer…
Fantastic isn’t it . So, Here it is
This Class is a Abstract Class so, you need not worry about even instantiating the class for Logging your message each and every time,
By simply placing this file into your Application and customize the settings you require and choosing On what timely basis you need to create your new Log files for Once and then GO !!
Log Class has Constants parameters as follows
// LOG DIRECTORY PATH by DEfault it takes the Folder from where it is executing Please use Tailored Slash const LOG_DIR= "logs/"; // PREFIX FOR AUTO CREATED LOG FILE CHANGE THIS IF YOU Wish to do const LOG_PRE="Log"; // Default Extenstion const LOG_EXT=".txt"; // DISABLE WRITING LOGS IN LOCAL ENVIRONMENT const LOG_DISABLE=false; // LOGG IPADDRESS const LOG_IP=true; // LOGG HTTP REFERRER const LOG_REF=true; // LOGG USER AGENT const LOG_UA=true;
1.Choose Your Desired Log file Path Directory path followed by tailered slash
2. Your Log file Extension Extension eg: .txt .doc, .dat etc
3. if You are running it in Local Server Disable the Logging True or False
4. Choose if you want to see IP Address of the User True or False
5. Choose If you want to see the Refereer Page True or False
6. Choose if you want to see the USer Agent True or False
Now, using autoload() you can Load the File or a simple require(“Log.php”) also Do the favour.
Next and Final Step, Logging your Message
to Log, use
Log::message("Hello to Log File");
If you are willing to enter your Message to a special File
then Simple
// FILE_NAME with out EXTENSION Log::message("Hello to Log File","FILE_NAME");
What do you say ? Logging is SIMPLE isn’t it?
<?php final class Log { /** * Logging Class takes 2 parameters, * @param $msg Text to Log * @param $filename Name of file with out extension to write if not provided this class will Automatically creates a file in the Given Log Dir with Prefix * Author Nagendra Kumar www.ITTreats.com */ // MSG VARIABLE private static $message; // FILE NAME VARIABLE private static $filename; // LOG DIRECTORY PATH by DEfault it takes the Folder from where it is executing Please use Tailored Slash const LOG_DIR= "logs/"; // PREFIX FOR AUTO CREATED LOG FILE CHANGE THIS IF YOU Wish to do const LOG_PRE="Log"; // Default Extenstion const LOG_EXT=".txt"; // DISABLE WRITING LOGS IN LOCAL ENVIRONMENT const LOG_DISABLE=false; // LOGG IPADDRESS const LOG_IP=true; // LOGG HTTP REFERRER const LOG_REF=true; // LOGG USER AGENT const LOG_UA=true; function message($msg,$file='') { if (empty($file)) self::$filename = self::LOG_DIR.self::LOG_PRE.date('Y_m_d',time()).self::LOG_EXT; else self::$filename=self::LOG_DIR.$file.date('Y_m_d',time()).self::LOG_EXT; self::$message=$msg; if(self::LOG_DISABLE!==true) self::writeLog(); } function writeLog() { $fd =@fopen(self::$filename, "a"); // append date/time to message $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . self::$message; if (self::LOG_IP===true) $str .="\t". $_SERVER['REMOTE_ADDR']; if (self::LOG_REF===true) $str .="\t". $_SERVER['HTTP_REFERER']; if (self::LOG_UA===true) $str .="\t". $_SERVER['HTTP_USER_AGENT']; // write string @fwrite($fd, $str . "\n"); // close file @fclose($fd); } } /* File Created for Logging simple Log Message to a File */ /* USAGE EXAMPLE Log::message("error writing files"); */ ?>
