Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file option #63

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ return [
* The path to the directory that contains your logs.
*/
'log_directory' => env('TAIL_LOG_DIRECTORY_PRODUCTION', ''),

/*
* The filename of the log file that you want to tail.
* Leave null to let the package automatically select the file.
*/
'file' => env('TAIL_LOG_FILE_PRODUCTION', null),

],
];
Expand All @@ -81,6 +87,13 @@ You can start the output with displaying the last lines in the log by using the
php artisan tail --lines=50
```

By default, the most-recently modified file in the directory will be used.
You can specify the file that you would like to tail by using the `file` option.

```bash
php artisan tail --file="other-file.log"
```

It's also possible to fully clear the output buffer after each log item.
This can be useful if you're only interested in the last log entry when debugging.

Expand All @@ -96,15 +109,15 @@ php artisan tail --grep="only display lines that contain this string"

### Tailing remote logs

To tail remote logs, you must first specify values for `host`, `user` and `log_directory` keys of an environment in the `tail` config file.
To tail remote logs, you must first specify values for `host`, `user`, `log_directory`, and `file` keys of an environment in the `tail` config file.

After that you can tail that logs of an environment like this

```bash
php artisan tail production
```

You can also use the `--clear` and `--lines` options described above.
You can also use the `--clear`, `--file`, and `--lines` options described above.

### Changelog

Expand Down
6 changes: 6 additions & 0 deletions config/tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@
*/
'log_directory' => env('TAIL_LOG_DIRECTORY_PRODUCTION', ''),

/*
* The filename of the log file that you want to tail.
* Leave null to let the package automatically select the file to tail.
*/
'file' => env('TAIL_LOG_FILE_PRODUCTION', null),

],
];
20 changes: 17 additions & 3 deletions src/TailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
class TailCommand extends Command
{
protected $signature = 'tail {environment?}
{--file= : Name of the log file to tail}
{--lines=0 : Output the last number of lines}
{--clear : Clear the terminal screen}
{--grep= : Grep specified string}
{--debug : "Display the underlying tail command}';
{--debug : Display the underlying tail command}';

protected $description = 'Tail the latest logfile';

Expand Down Expand Up @@ -70,7 +71,7 @@ protected function tailRemotely(string $environment): void
})
->execute([
"cd {$environmentConfig['log_directory']}",
$this->getTailCommand($environmentConfig['log_directory']),
$this->getTailCommand(),
]);
}

Expand All @@ -85,12 +86,25 @@ protected function getEnvironmentConfiguration(string $environment): array
return $config[$environment];
}

public function getTailFile(): string
{
$environment = $this->argument('environment');
$environmentConfig = is_null($environment)
? []
: $this->getEnvironmentConfiguration($environment);

return $this->option('file')
?? $environmentConfig['file']
?? "`ls -t | head -1`";
}

public function getTailCommand(): string
{
$grep = $this->option('grep')
? ' | grep "'.$this->option('grep').'"'
: '';
$file = $this->getTailFile();

return 'tail -f -n '.$this->option('lines').' "`ls -t | head -1`"'.$grep;
return 'tail -f -n '.$this->option('lines').' "'.$file.'"'.$grep;
}
}
11 changes: 11 additions & 0 deletions tests/TailCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function the_tail_command_is_correct()
->expectsOutput('tail -f -n 0 "`ls -t | head -1`"');
}

/** @test */
public function the_tail_command_with_file_is_correct()
{
$this
->artisan('tail', [
'--debug' => true,
'--file' => 'file.log',
])
->expectsOutput('tail -f -n 0 "file.log"');
}

/** @test */
public function the_command_when_grepping_is_correct()
{
Expand Down