简体   繁体   中英

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 :

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? 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. When this parameter is set to TRUE, print_r() will return the information rather than print it.

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

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

print_r has second parameter: return . Wich means will print_r() returns it's output, or just display it. It's false by default

So you need to try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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