Custom Logging Site Events


/ Published in: PHP
Save to your folder(s)

To keep up on important incidents on your site without having to dig through server logs the alternative can be custom logs.


Copy this code and paste it in your HTML
  1. <?php
  2. // first we declare a class to handle the file operations.
  3. class logfile{
  4. function write($logString )
  5. {
  6. if( $handle = @fopen( '../logs/actionLog.txt', 'a+' ) )
  7. // this can be parameratized
  8. {
  9. $logString .="\n"; // insures each entry is on a new line
  10. fputs( $handle, $logString, strlen($logString) );
  11. fclose( $handle );
  12. return( true );
  13. }
  14. else
  15. {
  16. return( false );
  17. }
  18. }
  19. } // end logfile class
  20.  
  21. function logAction($job,$cd,$msg)
  22. {
  23. // now we can create the custom code using the class
  24. $msgTypes=explode(',',"INFO,PROGRESS,WARN,ERROR,SECURITY,INVALID");
  25. //invalid must always be the last entry ... add more if needed
  26. $cod = ($cd>=count($msgTypes)) ? (count($msgTypes) - 1) : $cd;
  27. // If the code is invalid return INVALID as the message type
  28. $dt=date("Y-m-d H:i:s");
  29. // customize the date format to fit your requiremments
  30. $theLog = new logfile();
  31. // an instance of the logfile class
  32. $msgStr=$msgTypes[$cod].'{'.$cd.'}: '.$job.': '.$dt.'>>> '.$msg;
  33. // create the log message
  34. $theLog->write($msgStr); // this fires the file handling in the class
  35. }
  36. ?>

URL: http://coboldinosaur.com/pages/Custom_Logging_Site_Events.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.