File.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\analysis\logger\adapter;
  9. use lithium\util\String;
  10. use lithium\core\Libraries;
  11. /**
  12. * A simple log adapter that writes messages to files. By default, messages are written to
  13. * `resources/tmp/logs/<type>.log`, where `<type>` is the log message priority level.
  14. *
  15. * {{{
  16. * use lithium\analysis\Logger;
  17. *
  18. * Logger::config(array(
  19. * 'simple' => array('adapter' => 'File')
  20. * ));
  21. * Logger::write('debug', 'Something happened!');
  22. * }}}
  23. *
  24. * This will cause the message and the timestamp of the log event to be written to
  25. * `resources/tmp/logs/debug.log`. For available configuration options for this adapter, see
  26. * the `__construct()` method.
  27. *
  28. * @see lithium\analysis\logger\adapter\File::__construct()
  29. */
  30. class File extends \lithium\core\Object {
  31. /**
  32. * Class constructor.
  33. *
  34. * @see lithium\util\String::insert()
  35. * @param array $config Settings used to configure the adapter. Available options:
  36. * - `'path'` _string_: The directory to write log files to. Defaults to
  37. * `<app>/resources/tmp/logs`.
  38. * - `'timestamp'` _string_: The `date()`-compatible timestamp format. Defaults to
  39. * `'Y-m-d H:i:s'`.
  40. * - `'file'` _closure_: A closure which accepts two parameters: an array
  41. * containing the current log message details, and an array containing the `File`
  42. * adapter's current configuration. It must then return a file name to write the
  43. * log message to. The default will produce a log file name corresponding to the
  44. * priority of the log message, i.e. `"debug.log"` or `"alert.log"`.
  45. * - `'format'` _string_: A `String::insert()`-compatible string that specifies how
  46. * the log message should be formatted. The default format is
  47. * `"{:timestamp} {:message}\n"`.
  48. */
  49. public function __construct(array $config = array()) {
  50. $defaults = array(
  51. 'path' => Libraries::get(true, 'resources') . '/tmp/logs',
  52. 'timestamp' => 'Y-m-d H:i:s',
  53. 'file' => function($data, $config) { return "{$data['priority']}.log"; },
  54. 'format' => "{:timestamp} {:message}\n"
  55. );
  56. parent::__construct($config + $defaults);
  57. }
  58. /**
  59. * Appends a message to a log file.
  60. *
  61. * @see lithium\analysis\Logger::$_priorities
  62. * @param string $priority The message priority. See `Logger::$_priorities`.
  63. * @param string $message The message to write to the log.
  64. * @return closure Function returning boolean `true` on successful write, `false` otherwise.
  65. */
  66. public function write($priority, $message) {
  67. $config = $this->_config;
  68. return function($self, $params) use (&$config) {
  69. $path = $config['path'] . '/' . $config['file']($params, $config);
  70. $params['timestamp'] = date($config['timestamp']);
  71. $message = String::insert($config['format'], $params);
  72. return file_put_contents($path, $message, FILE_APPEND);
  73. };
  74. }
  75. }
  76. ?>