chg: [test] Show application logs

pull/8796/head
Jakub Onderka 2022-12-01 13:57:15 +01:00
parent fbe66a26e8
commit 4a76fae0ea
3 changed files with 28 additions and 9 deletions

View File

@ -263,4 +263,6 @@ jobs:
run: |
tail -n +1 `pwd`/app/tmp/logs/*
tail -n +1 /var/log/apache2/*.log
sudo -u $USER app/Console/cake Log export /tmp/logs.json.gz --without-changes
zcat /tmp/logs.json.gz

View File

@ -24,11 +24,14 @@ class LogShell extends AppShell
]);
$parser->addSubcommand('export', [
'help' => __('Export application logs to compressed file in JSON Lines format (one JSON encoded line per entry).'),
'parser' => array(
'arguments' => array(
'parser' => [
'arguments' => [
'file' => ['help' => __('Path to output file'), 'required' => true],
),
),
],
'options' => [
'without-changes' => ['boolean' => true, 'help' => __('Do not include add, edit or delete actions.')],
],
],
]);
$parser->addSubcommand('recompress', [
'help' => __('Recompress compressed data in logs.'),
@ -39,6 +42,7 @@ class LogShell extends AppShell
public function export()
{
list($path) = $this->args;
$withoutChanges = $this->param('without-changes');
if (file_exists($path)) {
$this->error("File $path already exists");
@ -49,21 +53,24 @@ class LogShell extends AppShell
$this->error("Could not open $path for writing");
}
$rows = $this->Log->query("SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'logs';");
/** @var ProgressShellHelper $progress */
$progress = $this->helper('progress');
$progress->init([
'total' => $rows[0]['TABLES']['TABLE_ROWS'], // just estimate, but fast
'total' => $this->Log->tableRows(), // just estimate, but fast
'width' => 50,
]);
$lastId = 0;
while (true) {
$conditions = ['Log.id >' => $lastId]; // much faster than offset
if ($withoutChanges) {
$conditions['NOT'] = ['Log.action' => ['add', 'edit', 'delete']];
}
$logs = $this->Log->find('all', [
'conditions' => ['id >' => $lastId], // much faster than offset
'conditions' => $conditions,
'recursive' => -1,
'limit' => 100000,
'order' => ['id ASC'],
'order' => ['Log.id ASC'],
]);
if (empty($logs)) {
break;

View File

@ -3053,6 +3053,16 @@ class AppModel extends Model
return [$subQuery];
}
/**
* Returns estimated number of table rows
* @return int
*/
public function tableRows()
{
$rows = $this->query("SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{$this->table}';");
return $rows[0]['TABLES']['TABLE_ROWS'];
}
// start a benchmark run for the given bench name
public function benchmarkInit($name = 'default')
{