<?php
/**
 * ログクラス
 */
class logger {
  public $logClass = "com";
  public $errorLogFile = "";

  private $INF_CLASS  = "INFO  ";
  private $ERR_CLASS  = "ERROR ";
  private $FAL_CLASS  = "FAITAL";
  private $SQL_CLASS  = "SQL   ";
  private $RET_CLASS  = "SQLRET";
  private $LOG_DIR    = "/usr/local/apache2/logs/php";

  function __construct($className) {
    $this->logClass=$className;
    $ymd = date("YmdH");
    if(!file_exists($this->LOG_DIR . '/' . session_id())) {
      mkdir($this->LOG_DIR . '/' . session_id());
    }
    $file = $this->LOG_DIR . "/" .session_id() . "/phplog_" . session_id() . "_" . $ymd . ".log";
    $this->fp = fopen($file,"a");
  }

  function __destruct() {
    fclose($this->fp);
  }

	public function info($title, $log = null) {
    $this->logging($this->INF_CLASS, $title, $log);
	}
	public function error($title, $log = null) {
    $this->logging($this->ERR_CLASS, $title, $log);
	}
	public function faital($title, $log = null) {
    $this->logging($this->FAL_CLASS, $title, $log);
	}
	public function sql($title, $log = null) {
    $this->logging($this->SQL_CLASS, $title, $log);
	}
	public function sqlRet($title, $log = null) {
    //$this->logging($this->RET_CLASS, $title, $log);
	}

  private function logging($type = "Q", $title, $log = null) {
    if($log == null || $log == "") {
      $t = "";
      $l = print_r($title,true);
    } else {
      $t = print_r($title,true);
      $l = print_r($log,true);
    }
    //$this->outputErrorLogFile($type, $t, $l);
    $this->outputLogFile($type, $t, $l);

  }
  private function outputLogFile($type, $title, $log) {
    $output = "[" . date("Y/m/d H:i:s") . "][@" . $type . "][" . $this->logClass . "][" . $title . "]" . $log . "\n";
    fputs($this->fp, $output);
  }

  private function outputErrorLogFile($type, $title, $log) {
    error_log("[@" . $type . "][". $this->logClass."][".$title."]". $log);
  }

}

?>
