【问题标题】:How can I capture output from a CakePHP Shell如何从 CakePHP Shell 捕获输出
【发布时间】:2012-07-23 11:44:15
【问题描述】:

有什么方法可以在 CakePHP 中捕获 shell 的输出吗?

我编写了一些 shell 来为 CakePHP 2.x 应用程序生成报告。我可以在命令行上运行 shell 并查看输出,但是,现在我想通过电子邮件发送这些 shell 的结果。

我曾考虑使用另一个 shell 作为包装器,然后使用 $this->dispatchShell('shellname') 捕获其输出,但似乎 dispatchShell 只是运行 shell 并将其输出转储到 CLI。

【问题讨论】:

  • 刚刚再次测试,它接受来自报告 shell 的 main 方法的返回值,而不是 $this->out 内容。

标签: cakephp cakephp-2.0


【解决方案1】:

要将 Shell 输出到文件,请在 Shell 的构造函数中声明一个输出流。下面是一个示例,让标准输出成为 CakePHP 的 TMP 目录(通常为 app/tmp/)上名为 shell.out 的文件中的日志文件:

<?php
class FooShell extends AppShell {

    public function __construct($stdout = null, $stderr = null, $stdin = null) {
        // This will cause all Shell outputs, eg. from $this->out(), to be written to
        // TMP.'shell.out'
        $stdout = new ConsoleOutput('file://'.TMP.'shell.out');

        // You can do the same for stderr too if you wish
        // $stderr = new ConsoleOutput('file://'.TMP.'shell.err');

        parent::__construct($stdout, $stderr, $stdin);
    }

    public function main() {
        // The following output will not be printed on your console
        // but be written to TMP.'shell.out'
        $this->out('Hello world');
    }
}

【讨论】:

  • 我在 shell 中尝试了这个,使用 CakePhp 1.3.6,但我在第 7 行的 /path/sitemap.php 中找不到 Class 'ConsoleOutput'。我在想 ConsoleOutput 是 cakephp 2.0+?
  • 这很好,正是我想要的,但你知道是否可以追加到文件而不是每次都覆盖它?
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多