FileLog.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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-2012, 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-2012, 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('BaseLog', 'Log/Engine');
  20. App::uses('Hash', 'Utility');
  21. /**
  22. * File Storage stream for Logging. Writes logs to different files
  23. * based on the type of log it is.
  24. *
  25. * @package Cake.Log.Engine
  26. */
  27. class FileLog extends BaseLog {
  28. /**
  29. * Path to save log files on.
  30. *
  31. * @var string
  32. */
  33. protected $_path = null;
  34. /**
  35. * Constructs a new File Logger.
  36. *
  37. * Config
  38. *
  39. * - `types` string or array, levels the engine is interested in
  40. * - `scopes` string or array, scopes the engine is interested in
  41. * - `file` log file name
  42. * - `path` the path to save logs on.
  43. *
  44. * @param array $options Options for the FileLog, see above.
  45. */
  46. public function __construct($config = array()) {
  47. parent::__construct($config);
  48. $config = Hash::merge(array(
  49. 'path' => LOGS,
  50. 'file' => null,
  51. 'types' => null,
  52. 'scopes' => array(),
  53. ), $this->_config);
  54. $config = $this->config($config);
  55. $this->_path = $config['path'];
  56. $this->_file = $config['file'];
  57. if (!empty($this->_file) && !preg_match('/\.log$/', $this->_file)) {
  58. $this->_file .= '.log';
  59. }
  60. }
  61. /**
  62. * Implements writing to log files.
  63. *
  64. * @param string $type The type of log you are making.
  65. * @param string $message The message you want to log.
  66. * @return boolean success of write.
  67. */
  68. public function write($type, $message) {
  69. $debugTypes = array('notice', 'info', 'debug');
  70. if (!empty($this->_file)) {
  71. $filename = $this->_path . $this->_file;
  72. } elseif ($type == 'error' || $type == 'warning') {
  73. $filename = $this->_path . 'error.log';
  74. } elseif (in_array($type, $debugTypes)) {
  75. $filename = $this->_path . 'debug.log';
  76. } elseif (in_array($type, $this->_config['scopes'])) {
  77. $filename = $this->_path . $this->_file;
  78. } else {
  79. $filename = $this->_path . $type . '.log';
  80. }
  81. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  82. return file_put_contents($filename, $output, FILE_APPEND);
  83. }
  84. }