log.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! Custom logger
  16. class Log {
  17. protected
  18. //! File name
  19. $file;
  20. /**
  21. * Write specified text to log file
  22. * @return string
  23. * @param $text string
  24. * @param $format string
  25. **/
  26. function write($text,$format='r') {
  27. $fw=Base::instance();
  28. foreach (preg_split('/\r?\n|\r/',trim($text)) as $line)
  29. $fw->write(
  30. $this->file,
  31. date($format).
  32. (isset($_SERVER['REMOTE_ADDR'])?
  33. (' ['.$_SERVER['REMOTE_ADDR'].']'):'').' '.
  34. trim($line).PHP_EOL,
  35. TRUE
  36. );
  37. }
  38. /**
  39. * Erase log
  40. * @return NULL
  41. **/
  42. function erase() {
  43. @unlink($this->file);
  44. }
  45. /**
  46. * Instantiate class
  47. * @param $file string
  48. **/
  49. function __construct($file) {
  50. $fw=Base::instance();
  51. if (!is_dir($dir=$fw->LOGS))
  52. mkdir($dir,Base::MODE,TRUE);
  53. $this->file=$dir.$file;
  54. }
  55. }