1. 假设Yii项目路径为 \www\to8to\apps\
    2. 并且 日志文件目录在\www\to8to\apps\protected\data
    3. 一、关联文件:
    4. 01ES核心包
    5. \www\to8to\apps\protected\vendor\Elastica
    6. 02ES导入核心文件
    7. ..\protected\components\Elastica.php
    8. ..\protected\components\ElasticaAutoLoader.php
    9. ..\protected\components\ElasticaDataProvider.php
    10. ..\protected\components\ElasticaSort.php
    11. 02、自动执行配置信息
    12. \www\to8to\apps\protected\config\console.php
    13. 03、定时执行启动程序
    14. \www\to8to\apps\protected\commands\crons.php
    15. 04、定时执行脚本内容
    16. \www\to8to\apps\protected\commands\ApplogCommand.php
    17. 二、实现步骤
    18. 1. 创建文件 \www\to8to\apps\protected\commands\crons.php
    19. 代码如下:
    20. <?php
    21. defined('YII_DEBUG') or define('YII_DEBUG',true);
    22. // including Yii
    23. require_once(dirname(dirname(dirname(__FILE__))).'/framework/yii.php');
    24. // we'll use a separate config file
    25. $configFile=dirname(dirname(__FILE__)).'/config/console.php';
    26. // creating and running console application
    27. Yii::createConsoleApplication($configFile)->run();
    28. ?>
    29. 2. 创建需要的配置文件 \www\to8to\apps\protected\config\console.php,配置需要的组件、数据库连接,日志等信息,格式类似主配置文件main.php
    30. 代码如下:
    31. <?php
    32. // This is the configuration for yiic console application.
    33. // Any writable CConsoleApplication properties can be configured here.
    34. return array(
    35. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    36. 'name'=>'My Console Application',
    37. 'import' => array (
    38. 'application.models.*',
    39. 'application.components.*',
    40. 'application.components.base.*',
    41. 'application.components.imgthumb.*',
    42. 'application.models.form.*',
    43. //'等等,导入所要的类包'
    44. ),
    45. // preloading 'log' component
    46. 'preload'=>array('log'),
    47. // application components
    48. 'components'=>array(
    49. // database settings are configured in database.php
    50. 'db'=>require(dirname(__FILE__).'/database.php'),
    51. 'log'=>array(
    52. 'class'=>'CLogRouter',
    53. 'routes'=>array(
    54. array(
    55. 'class'=>'CFileLogRoute',
    56. 'levels'=>'error, warning',
    57. ),
    58. ),
    59. ),
    60. ),
    61. 'params' => array(
    62. //配置ES连接服务
    63. 'ES_conn' => array(
    64. '_index'=> 'logcl',
    65. '_type' => 'app',
    66. 'host'=>'192.168.1.60',//58.67.156.84
    67. 'port'=>'39200',
    68. ),
    69. ),
    70. );
    71. 3. \www\to8to\apps\protected\commands\ 下新建 ApplogCommand类,继承 CConsoleCommand,在ApplogCommand中,可以使用项目的配置信息和Yii的各种方法
    72. 代码如下:
    73. <?php
    74. class ApplogCommandextends CConsoleCommand
    75. {
    76. public function run()
    77. {
    78. ...
    79. }
    80. }
    81. 4. 创建定时任务
    82. 代码如下:
    83. $ crontab -e
    84. 插入
    85. 代码如下:
    86. 1 * * * * /php -f /www/to8to/apps/protected/commands/crons.php Applog
    87. 即为每小时的第一分钟执行ApplogCommand类中的内容,类似的可以在\www\to8to\apps\protected\commands\下新建其他类,使用命令行执行。
    88. 命令行执行如下
    89. php d:/wamp/www/tboss/protected/commands/crons.php Applog >>d:/wamp/www/tboss/protected/data/test.log