Log.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Logger file.
  4. *
  5. * Provides an utility to log messages.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package IV
  10. */
  11. /**
  12. * Logger class.
  13. *
  14. * Logs different types of messages.
  15. *
  16. * @category Util
  17. */
  18. final class Log{
  19. /**
  20. * Reads the log configuration in ../config/config.ini
  21. *
  22. * @return mixed[] Log configuration.
  23. */
  24. private static function read_config(){
  25. return $configuration = parse_ini_file(__DIR__ . "/../config/config.ini", true)["log"];
  26. }
  27. /**
  28. * Formats a message for logging, including the current time.
  29. *
  30. * @param string $tag Log type tag.
  31. * @param string $message Message to log.
  32. * @param int $code Log message associated code, optional.
  33. * @return string Message, ready to be written.
  34. */
  35. private static function format($tag, $message, $code = null){
  36. $msg = "[" . $tag . "][" . date("c") . "]";
  37. if (is_int($code)) $msg .= "[$code]";
  38. $msg .= " $message\n";
  39. return $msg;
  40. }
  41. /**
  42. * Sends a debug message.
  43. *
  44. * Will do nothing unless the log level is set to DEBUG.
  45. *
  46. * @param string $message Message to log.
  47. * @param int $code Message associated code, optional.
  48. */
  49. public static function debug($message, $code = null){
  50. $config = Log::read_config();
  51. if ($config["level"] == "debug"){
  52. $msg = Log::format("DEBUG", $message, $code);
  53. file_put_contents($config["file"], $msg, FILE_APPEND);
  54. }
  55. }
  56. /**
  57. * Sends an informative message.
  58. *
  59. * Will do nothing unless the log level is set to DEBUG or INFO.
  60. *
  61. * @param string $message Message to log.
  62. * @param int $code Message associated code, optional.
  63. */
  64. public static function info($message, $code = null){
  65. $config = Log::read_config();
  66. if (in_array($config["level"], ["debug", "info"])){
  67. $msg = Log::format("INFO ", $message, $code);
  68. file_put_contents($config["file"], $msg, FILE_APPEND);
  69. }
  70. }
  71. /**
  72. * Sends a warning message.
  73. *
  74. * Will do nothing unless the log level is set to WARN.
  75. *
  76. * @param string $message Message to log.
  77. * @param int $code Message associated code, optional.
  78. */
  79. public static function warn($message, $code = null){
  80. $config = Log::read_config();
  81. if (in_array($config["level"], ["debug", "info", "warn"])){
  82. $msg = Log::format("WARN ", $message, $code);
  83. file_put_contents($config["file"], $msg, FILE_APPEND);
  84. }
  85. }
  86. /**
  87. * Sends an error message.
  88. *
  89. * Will do nothing if the log level is set to FATAL.
  90. *
  91. * @param string $message Message to log.
  92. * @param int $code Message associated code, optional.
  93. */
  94. public static function error($message, $code = null){
  95. $config = Log::read_config();
  96. if (in_array($config["level"], ["debug", "info", "warn", "error"])){
  97. $msg = Log::format("ERROR", $message, $code);
  98. file_put_contents($config["file"], $msg, FILE_APPEND);
  99. }
  100. }
  101. /**
  102. * Sends a fatal error message.
  103. *
  104. * Fatal messages are always logged.
  105. *
  106. * @param string $message Message to log.
  107. * @param int $code Message associated code, optional.
  108. */
  109. public static function fatal($message, $code = null){
  110. $config = Log::read_config();
  111. $msg = Log::format("FATAL", $message, $code);
  112. file_put_contents($config["file"], $msg, FILE_APPEND);
  113. }
  114. }