PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/koi/libraries/log/log.php

https://github.com/ngonchan/koi
PHP | 143 lines | 26 code | 7 blank | 110 comment | 3 complexity | c9b54ac39eb8df40840cd1307afd24de MD5 | raw file
  1. <?php
  2. namespace Koi\Log;
  3. /**
  4. * The Logger class can be used to write, what else, log files. What you're
  5. * going to log is all up to you, Koi just provides you with the required tools.
  6. *
  7. * h2. Logging Data
  8. *
  9. * Before we can start logging data we need to create a new instance of the logger.
  10. * Creating a new logger instance is as simple as the following:
  11. *
  12. * @$l = new Koi\Log\Logger();@
  13. *
  14. * As you can see this works very much like the view system provided by Koi.
  15. * The only difference is that defining options has to be done using an associative
  16. * array as the second argument of the construct. Example:
  17. *
  18. * @$l = new Koi\Log\Logger('file', array('date_format' => 'd-m-Y'));@
  19. *
  20. * Writing data can be done by calling the write() method on the object
  21. * created earlier.
  22. *
  23. * @$l->write("HARRO!");@
  24. *
  25. * For more information see each individual logger or the log interface.
  26. *
  27. * h2. Creating Loggers
  28. *
  29. * Creating a new logger is fairly easy, create a class that implements the LogInterface
  30. * and add the class to the available drivers. A basic class looks like the following:
  31. *
  32. * bc. <?php
  33. * class MyLogger implements LogInterface
  34. * {
  35. * public function __construct($options = array())
  36. * {
  37. *
  38. * }
  39. *
  40. * public function write($data)
  41. * {
  42. *
  43. * }
  44. * }
  45. *
  46. * Once your class is done you can add it to the list of available drivers as following:
  47. *
  48. * @Koi\Log\Log::$drivers['mylogger'] = 'Koi\Log\MyLog';@
  49. *
  50. * @author Yorick Peterse
  51. * @link http://yorickpeterse.com/
  52. * @licence MIT License
  53. * @package Koi
  54. *
  55. * Copyright (c) 2010, Yorick Peterse
  56. *
  57. * Permission is hereby granted, free of charge, to any person obtaining a copy
  58. * of this software and associated documentation files (the "Software"), to deal
  59. * in the Software without restriction, including without limitation the rights
  60. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  61. * copies of the Software, and to permit persons to whom the Software is
  62. * furnished to do so, subject to the following conditions:
  63. *
  64. * The above copyright notice and this permission notice shall be included in
  65. * all copies or substantial portions of the Software.
  66. *
  67. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  68. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  69. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  70. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  71. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  72. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  73. * THE SOFTWARE.
  74. */
  75. class Log
  76. {
  77. /**
  78. * Static array containing all log drivers.
  79. *
  80. * @static
  81. * @access public
  82. * @var array
  83. */
  84. public static $drivers = array(
  85. 'file' => 'Koi\Log\File'
  86. );
  87. /**
  88. * Static string that contains the name of the default
  89. * driver to use in case no driver has been specified
  90. * when creating a new instance of this class.
  91. *
  92. * @static
  93. * @access public
  94. * @var string
  95. */
  96. public static $default_driver = 'file';
  97. /**
  98. * Variable containing a new instance of the view driver.
  99. *
  100. * @access public
  101. * @var object
  102. */
  103. public $log_driver = NULL;
  104. /**
  105. * Creates a new instance of the Logger class and loads the log driver.
  106. *
  107. * @author Yorick Peterse
  108. * @param string $driver The name of the log driver to use.
  109. * @param array $options Additional options such as the date format in case of the
  110. * File logger.
  111. * @return object
  112. */
  113. public function __construct($driver = 'file', $options = array())
  114. {
  115. if ( !isset($driver) OR empty($driver) )
  116. {
  117. $driver = self::$default_driver;
  118. }
  119. if ( !isset(self::$drivers[$driver]) )
  120. {
  121. throw new Koi\Exception\LogException("No class for the specified log driver (\"$driver\") could be found.");
  122. }
  123. $this->log_driver = new self::$drivers[$driver]($options);
  124. }
  125. /**
  126. * Writes the specified data to the log using the current log driver.
  127. *
  128. * @author Yorick Peterse
  129. * @param string $data The data to write to the log.
  130. * @return bool
  131. */
  132. public function write($data)
  133. {
  134. return $this->log_driver->write($data);
  135. }
  136. }