44use GO \Services \Interval ;
55
66use Cron \CronExpression ;
7+ use Psr \Log \LoggerAwareInterface ;
8+ use Psr \Log \LoggerInterface ;
79use Swift_Attachment ;
810use Swift_Mailer ;
911use Swift_MailTransport ;
1012use Swift_Message ;
1113
12- abstract class Job
14+ abstract class Job implements LoggerAwareInterface
1315{
1416 /**
1517 * The command
@@ -80,7 +82,24 @@ abstract class Job
8082 * @var bool
8183 */
8284 public $ truthTest = true ;
85+
86+ /**
87+ * PSR-3 compliant logger
88+ * @var \Psr\Log\LoggerInterface
89+ */
90+ private $ logger ;
91+
92+ /**
93+ * Label for the job, to make it easier to identify in the logs
94+ * @var string
95+ */
96+ private $ jobLabel ;
8397
98+ /**
99+ * Optional message that will be added to the
100+ * @var string
101+ */
102+ private $ jobDoneMessage ;
84103
85104 /**
86105 * Create a new instance of Job
@@ -237,7 +256,11 @@ protected function compile($command)
237256 }
238257 }
239258
240- $ command .= ' > /dev/null 2>&1 ' ;
259+ // Only hide output if no loggers are used
260+ if (!$ this ->logger ) {
261+ $ command .= ' > /dev/null 2>&1 ' ;
262+ }
263+
241264 if ($ this ->runInBackground === true ) {
242265 $ command .= ' & ' ;
243266 }
@@ -252,16 +275,23 @@ protected function compile($command)
252275 */
253276 public function exec ()
254277 {
278+ $ jobOutput = [];
255279 $ this ->compiled = $ this ->build ();
256280 if (is_callable ($ this ->compiled )) {
257281 $ return = call_user_func ($ this ->command , $ this ->args );
258282 foreach ($ this ->outputs as $ output ) {
259283 Filesystem::write ($ return , $ output , $ this ->mode );
260284 }
285+
286+ if (is_string ($ return )) {
287+ $ jobOutput [] = $ return ;
288+ }
261289 } else {
262- $ return = exec ($ this ->compiled );
290+ $ return = exec ($ this ->compiled , $ jobOutput );
263291 }
264292
293+ $ this ->logJobOutput ($ jobOutput );
294+
265295 if ($ this ->emails ) {
266296 $ this ->sendEmails ();
267297 }
@@ -329,4 +359,73 @@ public function when($test)
329359
330360 return $ this ;
331361 }
362+
363+ /**
364+ * Attach a PSR-3 compliant logger to this job
365+ *
366+ * @param \Psr\Log\LoggerInterface $logger
367+ *
368+ * @return $this
369+ */
370+ public function setLogger (LoggerInterface $ logger )
371+ {
372+ $ this ->logger = $ logger ;
373+
374+ return $ this ;
375+ }
376+
377+ /**
378+ * Define a label for this job, which is used by the logger
379+ *
380+ * @param string $label
381+ *
382+ * @return $this
383+ */
384+ public function setLabel ($ label )
385+ {
386+ $ this ->jobLabel = $ label ;
387+
388+ return $ this ;
389+ }
390+
391+ /**
392+ * Define a message that will be logged after the job has run
393+ *
394+ * @param string $message
395+ *
396+ * @return $this
397+ */
398+ public function setJobDoneMessage ($ message )
399+ {
400+ $ this ->jobDoneMessage = $ message ;
401+
402+ return $ this ;
403+ }
404+
405+ /**
406+ * Get the log label for this job
407+ * @return string
408+ */
409+ protected function getLogLabel ()
410+ {
411+ return (!empty ($ this ->jobLabel )) ? $ this ->jobLabel : '' ;
412+ }
413+
414+ /**
415+ * Log output, if there is anything to log
416+ *
417+ * @param array $jobOutput
418+ *
419+ * @return void
420+ */
421+ protected function logJobOutput ($ jobOutput )
422+ {
423+ if (count ($ jobOutput ) > 0 ) {
424+ $ this ->logger ->info ($ this ->getLogLabel (), $ jobOutput );
425+ }
426+
427+ if ($ this ->jobDoneMessage !== null ) {
428+ $ this ->logger ->info ($ this ->getLogLabel (), [ $ this ->jobDoneMessage ]);
429+ }
430+ }
332431}
0 commit comments