Yii Log to console (STDOUT)

Yii has some methods for logging: to a db, website, email or file. But I’m using a cloud hosting service(heroku), which uses a console (heroku logs) to display logs. From the documentation I read that to get your logs in the console, you have to output your logs using stdout (http://nl.php.net/manual/en/features.commandline.io-streams.php).

Is this already possible with yii? And if not, how do I hook into the Yii::log() to alsoo output the message using stdout.

You could extend CLogRoute defining your own console log route. You will only have to implement your own version of processLogs($logs) e.g.: using stdout. This route could then be used as your standard logroute defined in the main.php config

greetings,

Hannes

btw.: Heroku supports PHP? Didn’t know that

Yes since a while now.


heroku create --stack cedar

Your repository needs a php file in the root though. So you can’t have

/framework

/public_html

---- /index.php

---- /protected/

And there’s a few other quirks, but nothing big. I create all my facebook apps with it (facebook has buildin heroku support).

Do you have an example of a logger using stdout. The whole concept of stdout is new to me, and I can’t quite grasp it.

Thanks for the info,

I never tried that and I don’t know if that will even work but I would try something like:


class StdOutRoute extends CLogRoute

{

    public function processLogs($logs)

    {

        $STDOUT = fopen("php://stdout", "w");

        foreach($logs as $log)

            fwrite($STDOUT, $log[0]); //write the message [1] = level, [2]=category

        fclose($STDOUT);

    }

}

Thanks I’ll give that a try :)

Ow, people reading this and are interested in heroku, atm it runs on Apache server v2.2.22 and PHP 5.3.10. It has free SSL, addons for MySQL, MongoDB, CouchDB and tons of others. Basic stuff is free, so handy to test out your code in ssl

hello

i am french developper on facebook

i try to write some data on heroku log

i try this evening in paris

and i confirm tomorow (or not ;-()

regards

no error in log

but i see no my_data in logs !!!

i work in php

i can write on a file with data

$fp = fopen ("my_secret_file.txt", "r");

$contenu = fgets ($fp, 255);

fclose ($fp);

echo 'Notre fichier contient : '.$contenu;

$contenu = $contenu.$my_secret_data.’;’;

echo 'Notre fichier contient : '.$contenu;

$fp = fopen ("results.txt", "a+");

fputs ($fp, $contenu);

fclose ($fp);

are you happy ??? yes i am

regards

Anyone care to right a tutorial on how to install Yii on a Heroku server?

I’ve got a lot of info on my blog in regards to Heroku, you can check it out at aaronfrancis.com

Thanks, good approach! I wrapped this into an extension: http://www.yiiframework.com/extension/yii-streamlog/

No need to define the custom target as a class, you can use FileTarget

            'targets' => [
                [
                    'class' => FileTarget::class,
                    'logFile' => 'php://stdout',
1 Like

thanks! this should be included in the docs