Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. class PartnernettLogger {
  4.  
  5. private $_path;
  6. private $_log_files = array();
  7. private $fileNumber = 0;
  8.  
  9. public function __construct() {
  10. //$this->_path = wp_upload_dir()["basedir"] . "/logs";
  11. $this->_log_files = array(
  12. "Debug" => "files/debug.log",
  13. "Error" => "files/error.log",
  14. "Mail" => "files/mail.log",
  15. "UserLogin" => "files/user_login.log",
  16. "UserChoices" => "files/user_choices.log",
  17. "Stacktrace" => "files/stacktrace.log"
  18. );
  19. $this->_createLogfilesIfNotExists($this->_log_files);
  20. }
  21.  
  22. private function _renameFile($path){
  23. $logFiles = 5;
  24. for($i = $logFiles; $i > 0; $i--){
  25. $newPath = $path.".".$i;
  26. $currentFileNumber = $i;
  27.  
  28. if(file_exists($path.".".$logFiles)){
  29. unlink($path.".".$logFiles) or die("Couldn't delete file");
  30. }
  31.  
  32. if (file_exists($newPath)){
  33. rename($newPath, $path.".".++$currentFileNumber);
  34. }
  35. }
  36. rename($path, $path.".1");
  37.  
  38. $f = @fopen($path, 'x+');
  39. @fclose($f);
  40. }
  41.  
  42. /** returns void **/
  43. private function _createLogfilesIfNotExists($input_array) {
  44. foreach ($input_array as $type=>$path) {
  45. if (file_exists($path)) {
  46. $fileSize = filesize($path);
  47.  
  48. if($fileSize > 500){
  49. $this->_renameFile($path);
  50. }
  51.  
  52. continue;
  53. }
  54. $f = @fopen($path, 'x+');
  55. @fclose($f);
  56. }
  57. }
  58.  
  59. /** returns void**/
  60. private function _appendToLogfile($body,$file) {
  61. $fh = @fopen($file, 'a');
  62. @fwrite($fh,$body . PHP_EOL);
  63. @fclose($fh);
  64. }
  65.  
  66. /** returns void **/
  67. public function insert_log($type,$user,$body,$stacktrace = null) {
  68.  
  69. if(!isset($user) || $user == null) {
  70. $user = "NULL";
  71. $body = "No user specified. ".$body;
  72. }
  73. if(!isset($type) || $type == null) {
  74. $type = "Error";
  75. $body = "No error type specified. ".$body;
  76. }
  77.  
  78. if(!isset($body) || $body == null) {
  79. $body = "";
  80. }
  81.  
  82. $timestamp = date(DATE_ISO8601, time());
  83.  
  84. if(isset($stacktrace) || $stacktrace != null) {
  85. $body = $stacktrace;
  86. }
  87.  
  88. $log = sprintf(
  89. "Timestamp: %s | User: %s | Message: %s",
  90. $timestamp, $user, $body
  91. );
  92.  
  93. $file = $this->_log_files[$type];
  94.  
  95. $this->_appendToLogfile($log,$file);
  96.  
  97. }
  98.  
  99. }
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement