2929use Symfony \Component \Console \Input \InputArgument ;
3030use Symfony \Component \Console \Input \InputInterface ;
3131use Symfony \Component \Console \Input \InputOption ;
32- use Symfony \Component \Console \Input \StringInput ;
3332use Symfony \Component \Console \Output \OutputInterface ;
3433use Symfony \Component \Console \Style \SymfonyStyle ;
3534use Symfony \Component \Console \Terminal ;
@@ -51,42 +50,49 @@ protected function configure() {
5150 ->setName ('log:tail ' )
5251 ->setDescription ('Tail the nextcloud logfile ' )
5352 ->addArgument ('lines ' , InputArgument::OPTIONAL , 'The number of log entries to print ' , "10 " )
54- ->addOption ('follow ' , 'f ' , InputOption::VALUE_NONE , 'Output new log entries as they appear ' );
53+ ->addOption ('follow ' , 'f ' , InputOption::VALUE_NONE , 'Output new log entries as they appear ' )
54+ ->addOption ('raw ' , 'r ' , InputOption::VALUE_NONE , 'Output raw log json instead of formatted log item ' );
5555 parent ::configure ();
5656 }
5757
5858 protected function execute (InputInterface $ input , OutputInterface $ output ): int {
59+ $ raw = $ input ->getOption ('raw ' );
5960 $ count = (int )$ input ->getArgument ('lines ' );
60- $ terminal = new Terminal ();
61- $ totalWidth = $ terminal ->getWidth ();
62- // 8 level, 18 for app, 26 for time, 6 for formatting
63- $ messageWidth = $ totalWidth - 8 - 18 - 26 - 6 ;
6461 $ io = new SymfonyStyle ($ input , $ output );
6562 $ logIterator = $ this ->logIteratorFactory ->getLogIterator ('11111 ' );
66- $ i = 0 ;
67- $ tableItems = [];
68- foreach ($ logIterator as $ logItem ) {
69- $ i ++;
70- if ($ i > $ count ) {
71- break ;
63+ $ logIterator = new \LimitIterator ($ logIterator , 0 , $ count );
64+ $ logItems = iterator_to_array ($ logIterator );
65+ $ logItems = array_reverse ($ logItems );
66+
67+ if ($ raw ) {
68+ foreach ($ logItems as $ logItem ) {
69+ $ output ->writeln (json_encode ($ logItem ));
7270 }
73- $ tableItems [] = [
74- self ::LEVELS [$ logItem ['level ' ]],
75- wordwrap ($ logItem ['app ' ], 18 ),
76- $ this ->formatter ->formatMessage ($ logItem , $ messageWidth ) . "\n" ,
77- $ logItem ['time ' ]
78- ];
71+ } else {
72+ $ terminal = new Terminal ();
73+ $ totalWidth = $ terminal ->getWidth ();
74+ // 8 level, 18 for app, 26 for time, 6 for formatting
75+ $ messageWidth = $ totalWidth - 8 - 18 - 26 - 6 ;
76+
77+ $ tableItems = array_map (function (array $ logItem ) use ($ messageWidth ) {
78+ return [
79+ self ::LEVELS [$ logItem ['level ' ]],
80+ wordwrap ($ logItem ['app ' ], 18 ),
81+ $ this ->formatter ->formatMessage ($ logItem , $ messageWidth ) . "\n" ,
82+ $ logItem ['time ' ],
83+ ];
84+ }, $ logItems );
85+ $ io ->table ([
86+ 'Level ' ,
87+ 'App ' ,
88+ 'Message ' ,
89+ 'Time ' ,
90+ ], $ tableItems );
7991 }
80- $ io ->table ([
81- 'Level ' ,
82- 'App ' ,
83- 'Message ' ,
84- 'Time '
85- ], array_reverse ($ tableItems ));
8692
8793 if ($ input ->getOption ('follow ' )) {
8894 $ watch = new Watch ($ this ->formatter , $ this ->logIteratorFactory );
89- $ watch ->run ( new StringInput ( '' ) , $ output );
95+ $ watch ->watch ( $ raw , $ output );
9096 }
9197
9298 return 0 ;
0 commit comments