<?php
namespace plugins;

use Qii\Loger\Writer;

class logger implements Writer
{
	public $logerPath = 'tmp';
	public $fileName = 'loger.log';

	public function __construct()
	{
		$this->logerPath = dirname(dirname(__FILE__)) .DS. $this->logerPath;
		$this->fileName = $this->logerPath . DS . date('Y-m-d') .'.'. $this->fileName;
		return $this;
	}

	public function setFileName($fileName)
	{
		$this->fileName = $this->logerPath . DS . date('Y-m-d') .'.'. $fileName . '.log';
		return $this;
	}
	protected function trimSpace($text)
	{
	    return  str_replace(array("\r\n", "\r", "\n", "\t", "\s", '　', chr(32)), "", $text);
	}

	public function formatLog($log)
	{
		if(!is_array($log)) return $log;
		return json_encode($log);
		return $this->trimSpace(print_r($log, true));
	}

	public function writeLog($logger)
	{
		if(is_array($logger)) $logger = $this->formatLog($logger);

		file_put_contents($this->fileName, date('Y-m-d H:i:s') ."\n\t". $logger . "\n", FILE_APPEND);
	}
}