Skip to content

Commit 8e39b9e

Browse files
committed
Added singleline option
1 parent 3c1b018 commit 8e39b9e

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

src/Commands/SearchLog.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public function handle() {
2222
parent::handle();
2323
}
2424

25-
protected function printRecord($record) {
26-
// simply add highlighting and then let the parent handle it
25+
protected function addHighlightingToRecord(LogRecord $record): LogRecord {
2726
$message = str_replace($this->filter['search'], '<bg=yellow>'.$this->filter['search'].'</>', $record['message']);
2827
$driver = $record->getDriver();
2928
$record = new LogRecord(
@@ -35,6 +34,19 @@ protected function printRecord($record) {
3534
$record['extra'],
3635
);
3736
$record->setDriver($driver);
37+
return $record;
38+
}
39+
40+
protected function printRecord(LogRecord $record): void {
41+
// simply add highlighting and then let the parent handle it
42+
$record = $this->addHighlightingToRecord($record);
3843
parent::printRecord($record);
3944
}
45+
46+
protected function printRecordSingleline(LogRecord $record): void {
47+
// same as print Record
48+
// simply add highlighting and then let the parent handle it
49+
$record = $this->addHighlightingToRecord($record);
50+
parent::printRecordSingleline($record);
51+
}
4052
}

src/Commands/ShowLog.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Devdot\LogArtisan\Helpers\CommandHelper;
66
use Devdot\LogArtisan\Models\DriverMultiple;
7+
use Devdot\LogArtisan\Models\LogRecord;
78
use Illuminate\Console\Command;
89

910
class ShowLog extends Command
@@ -32,6 +33,7 @@ class ShowLog extends Command
3233
{--l|level= : Show only entries with this log level}
3334
{--channel= : Use this specified logging channel}
3435
{--s|short : Only show short snippets}
36+
{--singleline : Show single-lined layout}
3537
{--stacktrace : Show the full stacktrace}
3638
';
3739

@@ -125,8 +127,12 @@ public function handle()
125127
}
126128

127129
foreach($records as $log) {
128-
$this->printSeparator();
129-
$this->printRecord($log);
130+
if($this->option('singleline'))
131+
$this->printRecordSingleline($log);
132+
else {
133+
$this->printSeparator();
134+
$this->printRecord($log);
135+
}
130136
}
131137

132138
$this->printSeparator();
@@ -146,13 +152,8 @@ protected function printSeparator() {
146152
$this->newLine();
147153
}
148154

149-
protected function printRecord($record) {
150-
$this->line(
151-
$record['datetime']->format('Y-m-d H:i:s').
152-
' <fg=gray>'.$record['channel'].'</>.'.
153-
CommandHelper::styleDebugLevel($record['level']).
154-
' <fg=gray>@'.$record->getDriver()->getLaravelChannel().'</>:'
155-
);
155+
protected function printRecord(LogRecord $record): void {
156+
$this->line(CommandHelper::styleLogRecordHeader($record));
156157
$this->line($record['message']);
157158
// stop output here if it's short output
158159
if($this->option('short')) {
@@ -194,4 +195,49 @@ protected function printRecord($record) {
194195

195196
}
196197
}
198+
199+
protected function printRecordSingleline(LogRecord $record): void {
200+
// create the heading text
201+
$str = CommandHelper::styleLogRecordHeader($record);
202+
203+
// now add the message
204+
$str .= ' '.$record['message'];
205+
206+
// remove line wraps
207+
$str = str_replace(PHP_EOL, ' ', $str);
208+
209+
// and create a string without the formatting
210+
$plain = preg_replace('/<[^\/]*?>(.*?)<\/>/', '$1', $str);
211+
212+
// and shorten the entire string to fit one line
213+
if(strlen($plain) > $this->terminalWidth) {
214+
// calculate the allowed length by adding the formatting length
215+
$formattingLength = strlen($str) - strlen($plain);
216+
217+
// make it shorter
218+
$str = substr($str, 0, $this->terminalWidth + $formattingLength);
219+
220+
// and make sure we are closing every formatting properly
221+
if(substr($str, -2) === '</')
222+
$str = substr($str, 0, -3).'</>';
223+
224+
// count to make sure we close every opened formatting tag
225+
$countOpen = count(preg_split('/<[^\/]*?>/', $str));
226+
$countClose = count(preg_split('/<\/>/', $str));
227+
228+
// check if we are odd between open and close
229+
if($countOpen > $countClose) {
230+
// make sure we didn't cut a closing tag short
231+
if(substr($str, -1) == '<')
232+
$str = substr($str, 0, -3);
233+
234+
// append for each missing one the close tag
235+
for($i = $countClose; $i < $countOpen; $i++)
236+
$str .= '</>';
237+
}
238+
}
239+
240+
// print the string
241+
$this->line($str);
242+
}
197243
}

src/Helpers/CommandHelper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Devdot\LogArtisan\Helpers;
44

5+
use Devdot\LogArtisan\Models\LogRecord;
56
use Illuminate\Console\Command;
67

78
// this helper needs to extend Command in order to access protected properties
@@ -47,4 +48,12 @@ public static function styleDebugLevel(string $level): string {
4748
$value = '<fg='.$color.'>'.strtoupper($level).'</>';
4849
return $value;
4950
}
51+
52+
public static function styleLogRecordHeader(LogRecord $record): string {
53+
return
54+
$record['datetime']->format('Y-m-d H:i:s').
55+
' <fg=gray>'.$record['channel'].'</>.'.
56+
CommandHelper::styleDebugLevel($record['level']).
57+
' <fg=gray>@'.$record->getDriver()->getLaravelChannel().'</>:';
58+
}
5059
}

0 commit comments

Comments
 (0)