假设Yii项目路径为 \www\to8to\apps\并且 日志文件目录在\www\to8to\apps\protected\data一、关联文件:01、ES核心包\www\to8to\apps\protected\vendor\Elastica02、ES导入核心文件..\protected\components\Elastica.php..\protected\components\ElasticaAutoLoader.php..\protected\components\ElasticaDataProvider.php..\protected\components\ElasticaSort.php02、自动执行配置信息\www\to8to\apps\protected\config\console.php03、定时执行启动程序\www\to8to\apps\protected\commands\crons.php04、定时执行脚本内容\www\to8to\apps\protected\commands\ApplogCommand.php二、实现步骤1. 创建文件 \www\to8to\apps\protected\commands\crons.php代码如下:<?phpdefined('YII_DEBUG') or define('YII_DEBUG',true);// including Yiirequire_once(dirname(dirname(dirname(__FILE__))).'/framework/yii.php');// we'll use a separate config file$configFile=dirname(dirname(__FILE__)).'/config/console.php';// creating and running console applicationYii::createConsoleApplication($configFile)->run();?>2. 创建需要的配置文件 \www\to8to\apps\protected\config\console.php,配置需要的组件、数据库连接,日志等信息,格式类似主配置文件main.php代码如下:<?php// This is the configuration for yiic console application.// Any writable CConsoleApplication properties can be configured here.return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'My Console Application', 'import' => array ( 'application.models.*', 'application.components.*', 'application.components.base.*', 'application.components.imgthumb.*', 'application.models.form.*', //'等等,导入所要的类包' ), // preloading 'log' component 'preload'=>array('log'), // application components 'components'=>array( // database settings are configured in database.php 'db'=>require(dirname(__FILE__).'/database.php'), 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), ), ), ), 'params' => array( //配置ES连接服务 'ES_conn' => array( '_index'=> 'logcl', '_type' => 'app', 'host'=>'192.168.1.60',//58.67.156.84 'port'=>'39200', ), ),);3. 在 \www\to8to\apps\protected\commands\ 下新建 ApplogCommand类,继承 CConsoleCommand,在ApplogCommand中,可以使用项目的配置信息和Yii的各种方法代码如下:<?php class ApplogCommandextends CConsoleCommand { public function run() { ... } }4. 创建定时任务代码如下:$ crontab -e插入代码如下:1 * * * * /php -f /www/to8to/apps/protected/commands/crons.php Applog即为每小时的第一分钟执行ApplogCommand类中的内容,类似的可以在\www\to8to\apps\protected\commands\下新建其他类,使用命令行执行。命令行执行如下 :php d:/wamp/www/tboss/protected/commands/crons.php Applog >>d:/wamp/www/tboss/protected/data/test.log