So consider for a moment the following code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
use App\Etis\Domain\Services\TwitterService;
use \Twitter;
use Log;
class FetchTweets extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fetch_tweets';
/**
* The console command description.
*
* @var string
*/
protected $description = 'fetches the latest ten tweets';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tweets = Twitter::getUserTimeline([
'screen_name' => env('TWITTER_USER_NAME'),
'count' => env('TWITTER_TWEET_AMOUNT'),
'format' => 'json'
]);
Log::info('Tweet Json: ' + $tweets);
$twitterService = new TwitterService();
$twitterService->processTweets(json_decode($tweets, true));
}
}
Really basic command, nothing fancy at all. So lets see how I have called this:
$schedule->command('fetch_tweets')
->everyMinute()
->withoutOverlapping()
->appendOutputTo('storage/logs/tweets.log');
Super simple. Lets look at the underlying code of how we get tweets every minute. Also note that we are logging output to a storage/logs/tweets.log
<?php
namespace App\Etis\Domain\Services;
use App\Etis\Domain\Entities\TweetsEntity;
use \Twitter;
class TwitterService {
public function processTweets(array $tweets) {
foreach ($tweets as $tweet) {
$tweetsEntity = new TweetsEntity();
if (is_null($tweetsEntity::where('tweet', Twitter::linkify($tweet['text'])))) {
$tweetsEntity->user_name = $tweet['user']['screen_name'];
$tweetsEntity->time_ago = Twitter::ago($tweet['created_at']);
$tweetsEntity->tweet = Twitter::linkify($tweet['text']);
$tweetsEntity->save();
}
}
}
}
Super simple. Nothing fancy. We get some tweets, we loop over, we save them. Now The issue is with this line:
Log::info('Tweet Json: ' + $tweets);
A) It does not print out out anything in the log file:
[2015-12-23 00:05:02] local.INFO: 0
This is not helpful at all. I specifically told it what to log (no?)
B) It logs to laravel.log, not to where I told it to append output too ???
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire