Syslog.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  10. * The Syslog adapter facilitates logging messages to a `syslogd` backend. See the constructor for
  11. * information on configuring this adapter.
  12. *
  13. * @see lithium\analysis\logger\adapter\Syslog::__construct()
  14. */
  15. class Syslog extends \lithium\core\Object {
  16. /**
  17. * Flag indicating whether or not the connection to `syslogd` has been opened yet.
  18. *
  19. * @var boolean
  20. */
  21. protected $_isConnected = false;
  22. /**
  23. * Array that maps `Logger` message priority names to `syslog`-compatible priority constants.
  24. *
  25. * @var array
  26. */
  27. protected $_priorities = array(
  28. 'emergency' => LOG_EMERG,
  29. 'alert' => LOG_ALERT,
  30. 'critical' => LOG_CRIT,
  31. 'error' => LOG_ERR,
  32. 'warning' => LOG_WARNING,
  33. 'notice' => LOG_NOTICE,
  34. 'info' => LOG_INFO,
  35. 'debug' => LOG_DEBUG
  36. );
  37. /**
  38. * Class constructor. Configures the `Syslog` adapter instance with the default settings. For
  39. * more information on these settings, see the documentation for
  40. * [the `openlog()` function](http://php.net/openlog).
  41. *
  42. * @param array $config Available configuration settings for this adapter:
  43. * - `'identity'` _string_: The identity string to be attached to each message in
  44. * the system log. This is usually a string that meaningfully identifies your
  45. * application. Defaults to `false`.
  46. * - `'options'` _integer_: The flags to use when opening the log. Defaults to
  47. * `LOG_ODELAY`.
  48. * - `'facility'` _integer_: A flag specifying the program to use to log the
  49. * messages. See the `openlog()` documentation for more information. Defaults to
  50. * `LOG_USER`.
  51. */
  52. public function __construct(array $config = array()) {
  53. $defaults = array('identity' => false, 'options' => LOG_ODELAY, 'facility' => LOG_USER);
  54. parent::__construct($config + $defaults);
  55. }
  56. /**
  57. * Appends `$message` to the system log.
  58. *
  59. * @param string $priority The message priority string. Maps to a `syslogd` priority constant.
  60. * @param string $message The message to write.
  61. * @return closure Function returning boolean `true` on successful write, `false` otherwise.
  62. */
  63. public function write($priority, $message) {
  64. $config = $this->_config;
  65. $_priorities = $this->_priorities;
  66. if (!$this->_isConnected) {
  67. closelog();
  68. openlog($config['identity'], $config['options'], $config['facility']);
  69. $this->_isConnected = true;
  70. }
  71. return function($self, $params) use ($_priorities) {
  72. $priority = $_priorities[$params['priority']];
  73. return syslog($priority, $params['message']);
  74. };
  75. }
  76. }
  77. ?>