Günlük Log Eklemek, Görüntülemek ve Kayıtlarını Tutmak

PHP
30 lines
<?php
class log{
var $dosyaadi;
function log() {
$this->dosyaadi = dirname(__FILE__)."/".date("Y.m.d").".log.html";
if(!file_exists($this->dosyaadi)){
fopen($this->dosyaadi, "w");
fclose($this->dosyaadi);
}
}
function ekle($metin, $tip) {
$dosya = @fopen($this->dosyaadi, "a+");
if($tip=="error"){ $tip='style="color:red"';}
if($tip=="success"){ $tip='style="color:green"';}
if ($dosya) {
$icerik="<span $tip>".date("d.m.Y H:i:s")." - ".$_SERVER["REMOTE_ADDR"]." - ";
$icerik.="$metin</span><br>\n";
fputs($dosya, $icerik);
fclose($dosya);
}
}
function goster() {
echo file_get_contents($this->dosyaadi);
}
}
$l=new log;
$l->ekle("Bu bir hata mesajıdır.","error");
$l->ekle("Bu bir başarı mesajıdır.","success");
$l->goster();
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Çıktısı

HTML
4 lines
<span style="color:red">08.06.2018 03:54:09 - 127.0.0.1 - Bu bir hata mesajıdır.</span><br>
<span style="color:green">08.06.2018 03:54:09 - 127.0.0.1 - Bu bir başarı mesajıdır.</span><br>
<span style="color:red">08.06.2018 03:54:10 - 127.0.0.1 - Bu bir hata mesajıdır.</span><br>
<span style="color:green">08.06.2018 03:54:10 - 127.0.0.1 - Bu bir başarı mesajıdır.</span><br>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX