Klasa Error Log w PHP

Klasa Error Log w PHP

error-logi-php
error-logi-php

Od pewnego czasu potrzebowałem klasy do zapisywania błędów do plików, a także żeby ważniejsze informacje były wysyłane na e-mail.
Więc wykonałem taką klasę, oparłem ją o gotową funkcję PHP w celu ułatwienia sobie pracy. Efekt można zobaczyć poniżej.

Zapis logów i co dalej?

Klasa pozwala na zapisywanie błędów do plików .log i wysłaniu ich na wskazany e-mail,

  • Wybór zależy tylko od nas, można użyć obu na raz i zapis do pliku i wysłać e-mail.
  • W klasie należy podać ścieżkę do plików i przypisać e-mail gdzie nastąpi wysłanie naszych logów z błędami
  • Sami ustalamy co chcemy tam umieścić, a klasa dopisuje dane takie jak ip, host, serwer i jeszcze parę innych sami zobaczcie .

test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  include("error-logi.class.php");
 
  $logi = new error_logi($_SERVER);
   
  $mesage='Błąd log przypisujemy np. błąd pdo lub mysql
                    albo błąd logowania'
;
     $user='Adam ';
     $html=true;
   
  ## ustawiamy kiedy zapisujey logi uzytkownika ##
    $logi--->user_logi_save($mesage,$user);
     $logi->user_logi_email($mesage, $user, $html);
 
  ## ustawiamy kiedy zapisujey logi globalne ##
     $logi->global_logi_save($mesage);
      $logi->global_logi_email($mesage, $html);

class-error-log.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#The MIT License
###############################
#Copyright (c) 2014 Adam Berger
###############################
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
###############################
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
###############################
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
##############################
 
/**
 * PHP 5.1
 */

   
/**
 * @version 1.00
 * error_logi
 * @author Adam Berger <ber34#o2.pl---->
 * Class allows you to save the log file error and send them to the e-mail,
 * The choice depends only on us, you can use both at once and save to a file and send an e-mail.
 * The class must specify the path to the file and assign e-mail address where you will send
 * Our logs with errors
 * Themselves we determine what we want to put there, and the class appends data
 * Such as ip, host, server, and a few others see for yourself.
 *
 * Klasa pozwala na zapisywanie błędów do plików .log i wysłaniu ich na wskazany e-mail,
 * Wybór zależy tylko od nas, można użyć obu na raz i zapis do pliku i wysłać e-mail.
 * W klasie należy podać ścieżkę do plików i przypisać e-mail gdzie nastąpi wysłanie
 * naszych logów z błędami
 * Sami ustalamy co chcemy tam umieścić, a klasa dopisuje dane
 * takie jak ip, host, serwer i jeszcze parę innych sami zobaczcie .
   
 */

 
class error_logi {

      const USER_DIR = "user_errors.log";
      const GLOBAL_DIR = 'global_errors.log';
      const EMAIL = 'example@o2.pl';

     private $logi;
     private $ip;
     private $serwer;
     private $data;
   
    public function __construct($SERVER) {
        if(!empty($SERVER)){
            $this->serwer[] = $SERVER;
          while(list($kkk, $jjj)=each($this->serwer[0])){
            if(array_key_exists($kkk, $this->serwer[0])){
               $this->serwer[$kkk]=$jjj;
        }else{
              $this->serwer = false;
            }
          }
        }
        $this->data = date('d-m-Y h:i:s');
    }
   
    private function global_ip(){
     if(filter_var($this->serwer['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE ) === false){
       return $this->ip = "Prywatny zakres ".$this->serwer['REMOTE_ADDR'];
     }else if(filter_var($this->serwer['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false){
        return $this->ip = "IPV4 ".$this->serwer['REMOTE_ADDR'];
        }else if(filter_var($this->serwer['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false){
        return $this->ip = "IPV6 ".$this->serwer['REMOTE_ADDR'];
     }
 }
    private function global_host(){
      if(!empty($this->serwer['REMOTE_ADDR'])){
         return gethostbyaddr($this->serwer['REMOTE_ADDR']);
      }
    }
   
     private function global_ip_server(){
      if(!empty($this->serwer['SERVER_ADDR'])){
         return $this->serwer['SERVER_ADDR'];
      }
    }
   
     private function global_agent(){
      if(!empty($this->serwer['HTTP_USER_AGENT'])){
         return $this->serwer['HTTP_USER_AGENT'];
      }
    }
   
     private function global_port(){
      if(!empty($this->serwer['REMOTE_PORT'])){
         return $this->serwer['REMOTE_PORT'];
      }
    }
   
    private function global_server_name(){
      if(!empty($this->serwer['SERVER_NAME'])){
         return $this->serwer['SERVER_NAME'];
      }
    }
      ### zapisujemy logi na serwerze w podanej &#347;cie&#380;ce ###
 public function user_logi_save($mesage,$user){
       
    $this->logi = "| Data: ".$this->data." | User: ".$user." | Wiadomosc: ".$mesage." | IP: ".$this->global_ip()."| Global Port: ".$this->global_port()." | Host: ".$this->global_host()."| Agent: ".$this->global_agent()."| IP Server: ".$this->global_ip_server()." \n";
    error_log($this->logi, 3, self::USER_DIR);
 }
   ### wysy&#322;amy logi na e-mail z loginem usera ###
 public function user_logi_email($mesage, $user, $html=false){
   
     $this->logi = "<br>| Logi z: ".$this->global_server_name()."<br>| User: ".$user."<br>| Data: ".$this->data." <br>| Wiadomosc: ".$mesage." <br>| IP: ".$this->global_ip()."<br>| Global Port: ".$this->global_port()." <br>| Host: ".$this->global_host()."<br>| Agent: ".$this->global_agent()."<br>| IP Server: ".$this->global_ip_server()." <br>";
    ### wiadomo&#347;&#263; html ###
   if($html === true){
     $html='';
     $html.='<html>';
     $html.='<h2>Logi z '.$this->global_server_name().'</h2><br>';
     $html.='<h3>Data: '.$this->data.'</h3>';
     $html.='<h3>User: '.$user.'</h3>';
     $html.='<h3>IP: '.$this->global_ip().'</h3>';
     $html.='<h3>Global Port: '.$this->global_port().'</h3>';
         $html.='<h3>Host: '.$this->global_host().'</h3>';
     $html.='<h3>Agent: '.$this->global_agent().'</h3>';
     $html.='<h3>IP Server: '.$this->global_ip_server().'</h3>';
     $html.='<h4>Wiadomosc: '.$mesage.'</h4>';
     $html.='</html>';
        ### Wysy&#322;amy w imieniu example@o2.pl z adresu examle@o2.pl
        error_log($html, 1, self::EMAIL,"subject :lunch\nContent-Type: text/html; charset=UTF-8; Foo\nFrom: ".self::EMAIL."\n");
    }else{
         error_log($this->logi, 1, self::EMAIL,"subject :lunch\nContent-Type: text/html;charset=UTF-8; Foo\nFrom: ".self::EMAIL."\n");
    }
 }
  ### zapisujemy logi na serwerze w podanej &#347;cie&#380;ce ###
 public function global_logi_save($mesage){
     
     $this->logi = "| Data: ".$this->data." | Wiadomosc: ".$mesage." | IP: ".$this->global_ip()."| Global Port: ".$this->global_port()." | Host: ".$this->global_host()."| Agent: ".$this->global_agent()."| IP Server: ".$this->global_ip_server()." \n";
     error_log($this->logi, 3, self::GLOBAL_DIR);
 }
 ### wysy&#322;amy logi na e-mail ###
 public function global_logi_email($mesage, $html=false){
   
    $this->logi = "<br>| Logi z: ".$this->global_server_name()."<br>| Data: ".$this->data." <br>| Wiadomosc: ".$mesage." <br>| IP: ".$this->global_ip()."<br>| Global Port: ".$this->global_port()." <br>| Host: ".$this->global_host()."<br>| Agent: ".$this->global_agent()."<br>| IP Server: ".$this->global_ip_server()." <br>";
     if($html === true){
     $html='';
     $html.='<html>';
     $html.='<h2>Logi z '.$this->global_server_name().'</h2><br>';
     $html.='<h3>Data: '.$this->data.'</h3>';
     $html.='<h3>IP: '.$this->global_ip().'</h3>';
     $html.='<h3>Global Port: '.$this->global_port().'</h3>';
         $html.='<h3>Host: '.$this->global_host().'</h3>';
     $html.='<h3>Agent: '.$this->global_agent().'</h3>';
     $html.='<h3>IP Server: '.$this->global_ip_server().'</h3>';
     $html.='<h4>Wiadomosc: '.$mesage.'</h4>';
     $html.='</html>';
        ### Wysy&#322;amy w imieniu example@o2.pl z adresu example@o2.pl
        error_log($html, 1, self::EMAIL,"subject :ab\nContent-Type: text/html; charset=UTF-8; Foo\nFrom: ".self::EMAIL."\n");
    }else{
         error_log($this->logi, 1, self::EMAIL,"subject :ab\nContent-Type: text/html;charset=UTF-8; Foo\nFrom: ".self::EMAIL."\n");
    }
   }
}

Download do testów: Error Log

Add a Comment