Last active
December 19, 2015 09:28
-
-
Save tamboer/5932714 to your computer and use it in GitHub Desktop.
PHP Class to create logfile logging actions with messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * NOTES: | |
| * | |
| * TODO: Consider some static methods or running the logger as a plugin | |
| * | |
| * | |
| */ | |
| class Logger { | |
| public $dir; | |
| public $file; | |
| protected $handle; | |
| public $logged; | |
| public $file_list = array(); | |
| static $count = 0; | |
| /** | |
| * Create logfile with log messages | |
| * | |
| * requires the directory where log files will be created | |
| * and another parameter for a filename | |
| * | |
| * | |
| * @param string $dir with trailing / | |
| * @param string $filename without any extensions | |
| * | |
| * Best practise: pass filename param equal to category of what is logged client/action/type | |
| */ | |
| public function __construct($dir, $filename) | |
| { | |
| $this->date = date('y-m-d H:i:s'); | |
| $this->dir = $dir; | |
| if ($filename) | |
| $this->file = $this->fileName($filename); | |
| $this->fileList(); | |
| } | |
| /** | |
| * Write a msg to a file, | |
| * | |
| * Using the FILE_APPEND flag to append the content to the end of the file | |
| * | |
| * and LOCK_EX flag to prevent anyone else writing to the file at the same time | |
| * | |
| * @param type $msg | |
| * | |
| * Date and EOL is prepended and appended to msg | |
| * | |
| * @return boolean | |
| */ | |
| function logAction($msg) | |
| { | |
| static::$count++; | |
| $sp = ': '; | |
| $this->logged = file_put_contents($this->dir . $this->file, $this->date . $sp . self::$count . $sp . $msg . PHP_EOL, FILE_APPEND | LOCK_EX); | |
| return $this->logged; | |
| } | |
| function fileName($filename) | |
| { | |
| $date = date('Y-m'); | |
| $file_name = $filename . '_' . $date . '.log'; | |
| return $file_name; | |
| } | |
| function fileList() | |
| { | |
| $this->handle = opendir($this->dir); | |
| if ($this->handle) { | |
| /* This is the correct way to loop over the directory. */ | |
| while (false !== ($entry = readdir($this->handle))) { | |
| $this->file_list[] = $entry; | |
| } | |
| closedir($this->handle); | |
| } | |
| return $this->file_list; | |
| } | |
| function printFileList(){ | |
| $str = ''; | |
| foreach ($this->file_list as $file => $name) { | |
| $str .= "File #{$file} : " . htmlspecialchars($name) . "<br />\n"; | |
| } | |
| return $str; | |
| } | |
| function findFile($filename) | |
| { | |
| if (in_array($filename, $this->file_list)) | |
| return true; | |
| } | |
| function readFile() | |
| { | |
| $lines = file($this->dir.$this->file); | |
| $str = ''; | |
| // Loop through our array, show HTML source as HTML source; and line numbers too. | |
| foreach ($lines as $line_num => $line) { | |
| $str .= "#<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; | |
| } | |
| return $str; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment