简体   繁体   English

我的网站的日志系统

[英]System of logs for my site

I create a small class for save the logs of my website. 我创建一个小类来保存我的网站的日志。 My log class : 我的日志类:

class Logs {

public static function  writeFile($message)
{
    $log_path = '/home/vagrant/Workspace/symfony/app/logs';
    // Set path:
    $log_path .= '/' . date('Ymd');
    $log_path .= '.log';
    $s_message = date("Y-m-d H:i:s") . ' (' . microtime(true) . ') ' . $message;
    $s_message .= "\n";
    file_put_contents($log_path, $s_message, FILE_APPEND);
}
public static function logInfo($s_message)
{
    self::writeFile($s_message);
}

}

And I call the static method in my controller : 然后在控制器中调用static方法:

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();
    $categories = $em->getRepository('EnsJobeetBundle:Category')->getWithJobs();
    $test = array(
        '1'=>'1',
        '2'=>'2',
        '3'=>'3'
    );
    Logs::logInfo(print_r($test));

    return $this->render('EnsJobeetBundle:Job:index.html.twig', array(
        'categories' => $categories
    ));
}

The problem is that : in my view it's show this $test array and in my log is write only the first value of array, so the value 1. What I'm doing wrong? 问题是:在我看来,它显示了这个$ test数组,而在我的日志中只写了数组的第一个值,所以值1。我在做什么错了? Help me please! 请帮帮我! Thx in advance! 提前谢谢!

In accordion with the doc : 根据文档

If you would like to capture the output of print_r(), use the return parameter. 如果要捕获print_r()的输出,请使用return参数。 When this parameter is set to TRUE, print_r() will return the information rather than print it. 当此参数设置为TRUE时,print_r()将返回信息而不是打印它。

Use this: 用这个:

Logs::logInfo(print_r($test, true));

instead of: 代替:

Logs::logInfo(print_r($test));

hope this help 希望这个帮助

I suggest you to use Monolog for this task 我建议您将Monolog用于此任务

http://symfony.com/doc/current/cookbook/logging/monolog.html http://symfony.com/doc/current/cookbook/logging/monolog.html

print_r has second parameter: return . print_r具有第二个参数: return Wich means will print_r() returns it's output, or just display it. Wich表示将print_r()返回其输出,或仅显示它。 It's false by default 默认是假的

So you need to try 所以你需要尝试

  Logs::logInfo(print_r($test,true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM