1. <?php
  2. /**
  3. * Class Logger.
  4. * A simple logger class for internal tooling.
  5. */
  6. class Logger {
  7. /**
  8. * Log level. Can be 'debug', 'info', 'warning', 'error', 'critical'.
  9. * @var string
  10. */
  11. private string $level = 'info';
  12. /**
  13. * Log file path.
  14. * @var string
  15. */
  16. private string $filePath;
  17. /**
  18. * Logger constructor.
  19. * @param string $filePath The path to the log file.
  20. */
  21. public function __construct(string $filePath) {
  22. $this->filePath = $filePath;
  23. }
  24. /**
  25. * Logs a message to the log file.
  26. * @param string $message The message to log.
  27. * @param string $level The log level (debug, info, warning, error, critical).
  28. * @return bool True on success, false on failure.
  29. */
  30. public function log(string $message, string $level = 'info'): bool {
  31. // Validate log level
  32. $validLevels = ['debug', 'info', 'warning', 'error', 'critical'];
  33. if (!in_array($level, $validLevels)) {
  34. $level = 'info'; // Default to info if invalid
  35. }
  36. // Prepare the log entry
  37. $timestamp = date('Y-m-d H:i:s');
  38. $logEntry = "[$timestamp] [$level] $message\n";
  39. // File open for appending
  40. $fileHandle = fopen($this->filePath, 'a');
  41. if ($fileHandle) {
  42. // Write the log entry to the file
  43. if (fwrite($fileHandle, $logEntry)) {
  44. fclose($fileHandle);
  45. return true; // Log successful
  46. } else {
  47. fclose($fileHandle);
  48. return false; // Log failed
  49. }
  50. } else {
  51. return false; // Failed to open log file.
  52. }
  53. }
  54. /**
  55. * Logs a debug message.
  56. * @param string $message The debug message.
  57. * @return bool True on success, false on failure.
  58. */
  59. public function debug(string $message): bool {
  60. return $this->log($message, 'debug');
  61. }
  62. /**
  63. * Logs an informational message.
  64. * @param string $message The informational message.
  65. * @return bool True on success, false on failure.
  66. */
  67. public function info(string $message): bool {
  68. return $this->log($message, 'info');
  69. }
  70. /**
  71. * Logs a warning message.
  72. * @param string $message The warning message.
  73. * @return bool True on success, false on failure.
  74. */
  75. public function warning(string $message): bool {
  76. return $this->log($message, 'warning');
  77. }
  78. /**
  79. * Logs an error message.
  80. * @param string $message The error message.
  81. * @return bool True on success, false on failure.
  82. */
  83. public function error(string $message): bool {
  84. return $this->log($message, 'error');
  85. }
  86. /**
  87. * Logs a critical message.
  88. * @param string $message The critical message.
  89. * @return bool True on success, false on failure.
  90. */
  91. public function critical(string $message): bool {
  92. return $this->log($message, 'critical');
  93. }
  94. }
  95. ?>

Add your comment