Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class PartnernettLogger {
- private $_path;
- private $_log_files = array();
- private $fileNumber = 0;
- public function __construct() {
- //$this->_path = wp_upload_dir()["basedir"] . "/logs";
- $this->_log_files = array(
- "Debug" => "files/debug.log",
- "Error" => "files/error.log",
- "Mail" => "files/mail.log",
- "UserLogin" => "files/user_login.log",
- "UserChoices" => "files/user_choices.log",
- "Stacktrace" => "files/stacktrace.log"
- );
- $this->_createLogfilesIfNotExists($this->_log_files);
- }
- private function _renameFile($path){
- $logFiles = 5;
- for($i = $logFiles; $i > 0; $i--){
- $newPath = $path.".".$i;
- $currentFileNumber = $i;
- if(file_exists($path.".".$logFiles)){
- unlink($path.".".$logFiles) or die("Couldn't delete file");
- }
- if (file_exists($newPath)){
- rename($newPath, $path.".".++$currentFileNumber);
- }
- }
- rename($path, $path.".1");
- $f = @fopen($path, 'x+');
- @fclose($f);
- }
- /** returns void **/
- private function _createLogfilesIfNotExists($input_array) {
- foreach ($input_array as $type=>$path) {
- if (file_exists($path)) {
- $fileSize = filesize($path);
- if($fileSize > 500){
- $this->_renameFile($path);
- }
- continue;
- }
- $f = @fopen($path, 'x+');
- @fclose($f);
- }
- }
- /** returns void**/
- private function _appendToLogfile($body,$file) {
- $fh = @fopen($file, 'a');
- @fwrite($fh,$body . PHP_EOL);
- @fclose($fh);
- }
- /** returns void **/
- public function insert_log($type,$user,$body,$stacktrace = null) {
- if(!isset($user) || $user == null) {
- $user = "NULL";
- $body = "No user specified. ".$body;
- }
- if(!isset($type) || $type == null) {
- $type = "Error";
- $body = "No error type specified. ".$body;
- }
- if(!isset($body) || $body == null) {
- $body = "";
- }
- $timestamp = date(DATE_ISO8601, time());
- if(isset($stacktrace) || $stacktrace != null) {
- $body = $stacktrace;
- }
- $log = sprintf(
- "Timestamp: %s | User: %s | Message: %s",
- $timestamp, $user, $body
- );
- $file = $this->_log_files[$type];
- $this->_appendToLogfile($log,$file);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement