Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 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. echo $fileSize;
  48. echo "<br>";
  49.  
  50. if($fileSize > 500){
  51. $this->_renameFile($path);
  52. }
  53.  
  54. continue;
  55. }
  56. $f = @fopen($path, 'x+');
  57. @fclose($f);
  58. }
  59. }
  60.  
  61. /** returns void**/
  62. private function _appendToLogfile($body,$file) {
  63. $fh = @fopen($file, 'a');
  64. @fwrite($fh,$body . PHP_EOL);
  65. @fclose($fh);
  66. }
  67.  
  68. /** returns void **/
  69. public function insert_log($type,$user,$body,$stacktrace = null) {
  70.  
  71. if(!isset($user) || $user == null) {
  72. $user = "NULL";
  73. $body = "No user specified. ".$body;
  74. }
  75. if(!isset($type) || $type == null) {
  76. $type = "Error";
  77. $body = "No error type specified. ".$body;
  78. }
  79.  
  80. if(!isset($body) || $body == null) {
  81. $body = "";
  82. }
  83.  
  84. $timestamp = date(DATE_ISO8601, time());
  85.  
  86. if(isset($stacktrace) || $stacktrace != null) {
  87. $body = $stacktrace;
  88. }
  89.  
  90. $log = sprintf(
  91. "Timestamp: %s | User: %s | Message: %s",
  92. $timestamp, $user, $body
  93. );
  94.  
  95. $file = $this->_log_files[$type];
  96.  
  97. $this->_appendToLogfile($log,$file);
  98.  
  99. }
  100.  
  101. }
  102.  
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement