44
55use Devdot \LogArtisan \Helpers \CommandHelper ;
66use Devdot \LogArtisan \Models \DriverMultiple ;
7+ use Devdot \LogArtisan \Models \LogRecord ;
78use Illuminate \Console \Command ;
89
910class 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}
0 commit comments