Skip to content

Commit 4a600c7

Browse files
committed
Allow a custom log filename to be specified via a paramater
1 parent 44089d6 commit 4a600c7

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Command/BaseCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ protected function configure()
7272

7373
$this
7474
->addOption('log-level', 'l', InputOption::VALUE_REQUIRED,
75-
'Override the Monolog logging level for this execution of the command. Valid values: ' . implode(',', array_keys(Logger::getLevels())))
75+
'Override the Monolog logging level for this execution of the command. Valid values: ' .
76+
implode(',', array_keys(Logger::getLevels())))
77+
->addOption('log-filename', null, InputOption::VALUE_REQUIRED, 'Specify a different file (relative to the '.
78+
'kernel log directory) to send file logs to')
7679
->addOption('locking', null, InputOption::VALUE_REQUIRED, 'Switches locking on/off');
7780

7881
$this->advanceExecutionPhase(RuntimeConfig::PHASE_POST_CONFIGURE);

Helper/Config/RuntimeConfig.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ public function loadGlobalConfigFromContainer(ContainerInterface $container)
157157

158158
public function loadConfigFromCommandParameters(InputInterface $input)
159159
{
160-
// Override LogLevel to the one provided at runtime
160+
// Logging parameters
161161
if ($input->getOption('log-level')) {
162162
$loggerLevels = Logger::getLevels();
163163
$this->setLogLevel($loggerLevels[strtoupper($input->getOption('log-level'))]);
164164
}
165+
if ($input->getOption('log-filename')) {
166+
$this->setLogFilename($input->getOption('log-filename'));
167+
}
165168
}
166169

167170
/**
@@ -353,7 +356,9 @@ public function setConsoleLogLineFormat($format)
353356
*/
354357
public function setLogFilename($logFilename)
355358
{
356-
if ($this->getExecutionPhase() > self::PHASE_INITIALISE) {
359+
if($this->getExecutionPhase() == self::PHASE_LOAD_PARAMETERS){
360+
$this->logConfigDebug('LOG FILENAME CHANGED VIA PARAMETER: '.$logFilename);
361+
} elseif ($this->getExecutionPhase() > self::PHASE_INITIALISE) {
357362
throw new BaseCommandException('Cannot set manual logfile name. Logger is already initialised');
358363
}
359364

Tests/Helper/Logging/LoggingEnhancementTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,29 @@ public function testPushLogMessageOnPreInitQueue()
385385
);
386386
}
387387

388+
public function testChangeLogFilenameViaParameter()
389+
{
390+
$logFilename = 'customName.log';
391+
$command = $this->registerCommand(new LoggingCommand());
392+
393+
394+
$commandTester = $this->executeCommand($command, array('--log-filename'=>$logFilename));
395+
$this->assertEquals(
396+
$logFilename,
397+
$command->getLogFilename(false),
398+
'Log level does not appear to have been changed to '.$logFilename.' by the commandline parameter');
399+
400+
$this->assertTrue(
401+
$this->doesLogfileExist($logFilename),
402+
'A log file with the name '.$logFilename.' does not seem to have been created'
403+
);
404+
405+
$this->assertContains(
406+
'LOG FILENAME CHANGED VIA PARAMETER:',
407+
$commandTester->getDisplay(),
408+
'Log filename change was not outputted to console'
409+
);
410+
411+
}
412+
388413
}

0 commit comments

Comments
 (0)