如何捕获CakePHP Shell的输出 [英] How can I capture output from a CakePHP Shell

查看:119
本文介绍了如何捕获CakePHP Shell的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP中捕获shell的输出有什么办法吗

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

I've written some shells that generates reports for a CakePHP 2.x application. I'm able to run the shells on a command line and view the output, however, now I'd like to email out the results of those shells.

我想关于使用另一个shell作为包装,然后使用 $ this-> dispatchShell('shellname')捕获其输出,但看起来 dispatchShell 只是运行shell并将其输出转储到CLI。

I thought about using another shell as a wrapper and then using $this->dispatchShell('shellname') to capture its output, but it seems dispatchShell just runs the shell and dumps it's output to the CLI.

推荐答案

要将Shell输出到文件,请在Shell的构造函数声明一个输出流。这里有一个例子,让stdout成为您的CakePHP的 TMP dir(通常 app / tmp / )上的日志文件文件名为 shell.out

To have Shell output to a file, declare an output stream at your Shell's constructor. Here's an example to have stdout be a log file at your CakePHP's TMP dir (usually app/tmp/) on a file named 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');
    }
}

这篇关于如何捕获CakePHP Shell的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆