FileLog.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * File Storage stream for Logging
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  15. * @package Cake.Log.Engine
  16. * @since CakePHP(tm) v 1.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeLogInterface', 'Log');
  20. /**
  21. * File Storage stream for Logging. Writes logs to different files
  22. * based on the type of log it is.
  23. *
  24. * @package Cake.Log.Engine
  25. */
  26. class FileLog implements CakeLogInterface {
  27. /**
  28. * Path to save log files on.
  29. *
  30. * @var string
  31. */
  32. protected $_path = null;
  33. /**
  34. * Constructs a new File Logger.
  35. *
  36. * Options
  37. *
  38. * - `path` the path to save logs on.
  39. *
  40. * @param array $options Options for the FileLog, see above.
  41. */
  42. public function __construct($options = array()) {
  43. $options += array('path' => LOGS);
  44. $this->_path = $options['path'];
  45. }
  46. /**
  47. * Implements writing to log files.
  48. *
  49. * @param string $type The type of log you are making.
  50. * @param string $message The message you want to log.
  51. * @return boolean success of write.
  52. */
  53. public function write($type, $message) {
  54. $debugTypes = array('notice', 'info', 'debug');
  55. if ($type == 'error' || $type == 'warning') {
  56. $filename = $this->_path . 'error.log';
  57. } elseif (in_array($type, $debugTypes)) {
  58. $filename = $this->_path . 'debug.log';
  59. } else {
  60. $filename = $this->_path . $type . '.log';
  61. }
  62. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  63. return file_put_contents($filename, $output, FILE_APPEND);
  64. }
  65. }