FileLog.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  16. * @package Cake.Log.Engine
  17. * @since CakePHP(tm) v 1.3
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('BaseLog', 'Log/Engine');
  21. App::uses('Hash', 'Utility');
  22. /**
  23. * File Storage stream for Logging. Writes logs to different files
  24. * based on the type of log it is.
  25. *
  26. * @package Cake.Log.Engine
  27. */
  28. class FileLog extends BaseLog {
  29. /**
  30. * Path to save log files on.
  31. *
  32. * @var string
  33. */
  34. protected $_path = null;
  35. /**
  36. * Constructs a new File Logger.
  37. *
  38. * Config
  39. *
  40. * - `types` string or array, levels the engine is interested in
  41. * - `scopes` string or array, scopes the engine is interested in
  42. * - `file` log file name
  43. * - `path` the path to save logs on.
  44. *
  45. * @param array $options Options for the FileLog, see above.
  46. */
  47. public function __construct($config = array()) {
  48. parent::__construct($config);
  49. $config = Hash::merge(array(
  50. 'path' => LOGS,
  51. 'file' => null,
  52. 'types' => null,
  53. 'scopes' => array(),
  54. ), $this->_config);
  55. $config = $this->config($config);
  56. $this->_path = $config['path'];
  57. $this->_file = $config['file'];
  58. if (!empty($this->_file) && substr($this->_file, -4) !== '.log') {
  59. $this->_file .= '.log';
  60. }
  61. }
  62. /**
  63. * Implements writing to log files.
  64. *
  65. * @param string $type The type of log you are making.
  66. * @param string $message The message you want to log.
  67. * @return boolean success of write.
  68. */
  69. public function write($type, $message) {
  70. $debugTypes = array('notice', 'info', 'debug');
  71. if (!empty($this->_file)) {
  72. $filename = $this->_path . $this->_file;
  73. } elseif ($type == 'error' || $type == 'warning') {
  74. $filename = $this->_path . 'error.log';
  75. } elseif (in_array($type, $debugTypes)) {
  76. $filename = $this->_path . 'debug.log';
  77. } else {
  78. $filename = $this->_path . $type . '.log';
  79. }
  80. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  81. return file_put_contents($filename, $output, FILE_APPEND);
  82. }
  83. }