function testItWritesLogsToDatabase() { global $wpdb; $logger = new Logger(); $event = new OptionEvent(); $event->setBody('Hello World {user}!'); $event->setSeverity('notice'); $event->setContext(['user' => 'John Doe']); $logger->log('notice', 'Hello World {user}!', ['user' => 'John Doe'], $event); $result = $wpdb->get_row(sprintf('SELECT * FROM %s.%s WHERE body LIKE "%%World%%"', DB_NAME, $wpdb->prefix . 'wplog')); $this->assertNotEmpty($result); $this->assertStringStartsWith('Hello World', $result->body); $this->assertStringEndsWith('Doe!', $result->body); }
public function testItLogsToFile() { $logger = new \Wplog\Logging\File\Logger(); $testLogFile = ABSPATH . '/wplog-testing.log'; $logger->setLogFile($testLogFile); $event = new OptionEvent(); $event->setBody('Hello World {user}!'); $event->setSeverity('notice'); $event->setContext(['user' => 'John Doe']); $logger->debug('Hello World!', [], $event); $logContents = file_get_contents($testLogFile); $this->assertRegExp('%Hello World John Doe%imu', $logContents); unlink($testLogFile); }
function testItCanWriteLogCollection() { global $wpdb; $collection = new LoggerCollection(); $collection->addLogger(new WpdbLogger()); $event = new OptionEvent(); $event->setBody('Hello World!'); $event->setSeverity('notice'); $event->setUserId('1'); $collection->write($event); $row = $wpdb->get_row(sprintf('SELECT * FROM %s.%s', DB_NAME, $wpdb->prefix . 'wplog')); $this->assertNotEmpty($row); $this->assertStringStartsWith('Hello World', $row->body); }