log.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 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. (($fwd=filter_var($fw->get('HEADERS.X-Forwarded-For'),
  35. FILTER_VALIDATE_IP))?(' ('.$fwd.')'):'')
  36. .']'):'').' '.
  37. trim($line).PHP_EOL,
  38. TRUE
  39. );
  40. }
  41. /**
  42. * Erase log
  43. * @return NULL
  44. **/
  45. function erase() {
  46. @unlink($this->file);
  47. }
  48. /**
  49. * Instantiate class
  50. * @param $file string
  51. **/
  52. function __construct($file) {
  53. $fw=Base::instance();
  54. if (!is_dir($dir=$fw->LOGS))
  55. mkdir($dir,Base::MODE,TRUE);
  56. $this->file=$dir.$file;
  57. }
  58. }