2323
2424namespace OCA \LogReader \Log ;
2525
26+ use OCP \Files \IAppData ;
27+ use OCP \Files \NotFoundException ;
2628use OCP \IConfig ;
2729use OCP \Log \IFileBased ;
2830use OCP \Log \ILogFactory ;
2931
3032class LogIteratorFactory {
31- private $ config ;
32- private $ logFactory ;
3333
34- public function __construct (IConfig $ config , ILogFactory $ logFactory ) {
35- $ this ->config = $ config ;
36- $ this ->logFactory = $ logFactory ;
34+ public function __construct (
35+ private IConfig $ config ,
36+ private ILogFactory $ logFactory ,
37+ private IAppData $ appData ,
38+ ) {
3739 }
3840
3941 /**
@@ -44,18 +46,39 @@ public function __construct(IConfig $config, ILogFactory $logFactory) {
4446 public function getLogIterator (array $ levels ) {
4547 $ dateFormat = $ this ->config ->getSystemValue ('logdateformat ' , \DateTime::ATOM );
4648 $ timezone = $ this ->config ->getSystemValue ('logtimezone ' , 'UTC ' );
47- $ log = $ this ->logFactory ->get ('file ' );
48- if ($ log instanceof IFileBased) {
49- $ handle = fopen ($ log ->getLogFilePath (), 'rb ' );
50- if ($ handle ) {
51- $ iterator = new LogIterator ($ handle , $ dateFormat , $ timezone );
52- return new \CallbackFilterIterator ($ iterator , function ($ logItem ) use ($ levels ) {
53- return $ logItem && in_array ($ logItem ['level ' ], $ levels );
54- });
49+
50+ if ($ this ->config ->getSystemValue ('log_type ' , 'file ' ) !== 'file ' ) {
51+ try {
52+ $ folder = $ this ->appData ->getFolder ('logreader ' );
53+ } catch (NotFoundException $ e ) {
54+ $ folder = $ this ->appData ->newFolder ('logreader ' );
55+ }
56+
57+ try {
58+ $ logfile = $ folder ->getFile ('nextcloud.log ' );
59+ } catch (NotFoundException $ e ) {
60+ $ logfile = $ folder ->newFile ('nextcloud.log ' );
61+ }
62+
63+ $ handle = $ logfile ->read ();
64+ if (!$ handle ) {
65+ throw new \Exception ('Error while opening internal logfile ' );
66+ }
67+ } else {
68+ $ logfile = $ this ->logFactory ->get ('file ' );
69+ if ($ logfile instanceof IFileBased) {
70+ $ handle = fopen ($ logfile ->getLogFilePath (), 'rb ' );
71+ if (!$ handle ) {
72+ throw new \Exception ("Error while opening " . $ logfile ->getLogFilePath ());
73+ }
5574 } else {
56- throw new \Exception (" Error while opening " . $ log-> getLogFilePath () );
75+ throw new \Exception (' Can \' t find log class ' );
5776 }
5877 }
59- throw new \Exception ('Can \'t find log class ' );
78+
79+ $ iterator = new LogIterator ($ handle , $ dateFormat , $ timezone );
80+ return new \CallbackFilterIterator ($ iterator , function ($ logItem ) use ($ levels ) {
81+ return $ logItem && in_array ($ logItem ['level ' ], $ levels );
82+ });
6083 }
6184}
0 commit comments