*/ namespace TheTempusProject\Canary\Classes; use TheTempusProject\Bedrock\Functions\Date; class Logger { public $file; public $logDirectory; public $logFilePath; public function __construct() { $this->setupLogFile(); $this->file = fopen( $this->logFilePath, 'a' ); fwrite( $this->file, '===================------++++++++++------===================' . PHP_EOL ); } public function setupLogFile() { $this->logDirectory = rtrim( CANARY_DEBUG_DIRECTORY, DIRECTORY_SEPARATOR ); if ( ! is_dir( $this->logDirectory ) ) { mkdir( $this->logDirectory, 0777, true ); } $currentFile = date('m-d-Y') . '.log'; $this->logFilePath = $this->logDirectory . DIRECTORY_SEPARATOR . $currentFile; if ( ! file_exists( $this->logFilePath ) ) { touch( $this->logFilePath ); chmod( $this->logFilePath, 0777 ); } } public function __destruct() { fwrite( $this->file, '============================================================' . PHP_EOL ); fclose( $this->file ); } public function addLog( $type = 'log', $log ) { switch ( CANARY_DEBUG_TO_FILE_LEVEL ) { case CANARY_DEBUG_LEVEL_ERROR: $acceptableLoggingLevels = [ CANARY_DEBUG_LEVEL_ERROR, ]; if (! in_array( $type, $acceptableLoggingLevels )) { return; } break; case CANARY_DEBUG_LEVEL_WARN: $acceptableLoggingLevels = [ CANARY_DEBUG_LEVEL_ERROR, CANARY_DEBUG_LEVEL_WARN, ]; if (! in_array( $type, $acceptableLoggingLevels )) { return; } break; case CANARY_DEBUG_LEVEL_INFO: $acceptableLoggingLevels = [ CANARY_DEBUG_LEVEL_ERROR, CANARY_DEBUG_LEVEL_WARN, CANARY_DEBUG_LEVEL_INFO, ]; if (! in_array( $type, $acceptableLoggingLevels )) { return; } break; case CANARY_DEBUG_LEVEL_LOG: $acceptableLoggingLevels = [ CANARY_DEBUG_LEVEL_ERROR, CANARY_DEBUG_LEVEL_WARN, CANARY_DEBUG_LEVEL_INFO, CANARY_DEBUG_LEVEL_LOG, ]; if (! in_array( $type, $acceptableLoggingLevels )) { return; } break; case CANARY_DEBUG_LEVEL_DEBUG: $acceptableLoggingLevels = [ CANARY_DEBUG_LEVEL_ERROR, CANARY_DEBUG_LEVEL_WARN, CANARY_DEBUG_LEVEL_INFO, CANARY_DEBUG_LEVEL_LOG, CANARY_DEBUG_LEVEL_DEBUG, ]; if (! in_array( $type, $acceptableLoggingLevels )) { return; } break; default: return; } $formattedMessage = $this->timestamp() . $this->typestamp( $type ) . $log . PHP_EOL; fwrite( $this->file, $formattedMessage ); } private function timestamp() { $dateString = Date::getReadableDate( time() ); return '['.$dateString.'] - '; } private function typestamp( $type ) { $dateString = Date::getReadableDate( time() ); return '[' . strtoupper( $type ) . '] - '; } }