-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbea-logger.php
More file actions
180 lines (153 loc) · 3.86 KB
/
bea-logger.php
File metadata and controls
180 lines (153 loc) · 3.86 KB
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
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
Plugin Name: BEA lOGGER
Version: 0.3
Plugin URI: https://github.com/beapi/bea-logger
Description: Allow to log basic data on a log file
Author: BeAPI
Author URI: http://www.beapi.fr
----
Copyright 2015 Beapi Technical team (technique@beapi.fr)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Check if class already exists
* @since 0.3
*/
if ( class_exists( 'Bea_Log' ) ) {
return;
}
class Bea_Log {
/**
* The log file path
*
* @var string
*/
private $file_path;
/**
* The log file extension
* .log by default
* @var string
*/
private $file_extension ='.log' ;
/**
* The log file max size
* Here 400Mo
*
* @var string
*/
private $retention_size = '419430400';
/**
* If the logger is ready or not
*
* @var bool
*/
private $is_configured = false;
/**
* Level of gravity for the logging
*/
const gravity_0 = 'Emerg';
const gravity_1 = 'Alert';
const gravity_2 = 'Crit';
const gravity_3 = 'Err';
const gravity_4 = 'Warning';
const gravity_5 = 'Notice';
const gravity_6 = 'Info';
const gravity_7 = 'Debug';
/**
* Construct the logged file
*
* @param $file_path
* @param string $file_extension
* @param string $retention_size
*/
function __construct( $file_path, $file_extension = '.log', $retention_size = '' ) {
if ( ! isset( $file_path ) || empty( $file_path ) ) {
return false;
}
// Put file path
$this->file_path = $file_path;
// File extension
if ( isset( $file_extension ) ) {
$this->file_extension = $file_extension;
}
// Retention size
if ( isset( $retention_size ) && ! empty( $retention_size ) && (int) $retention_size > 0 ) {
$this->retention_size = $retention_size;
}
$this->is_configured = true;
}
/**
* Log data in multiple files when full
*
* @param $message
* @param string $type
*extension
* @return bool
* @author Nicolas Juen
*/
public function log_this( $message, $type = self::gravity_7 ) {
if ( false === $this->is_configured ) {
return false;
}
// Make the file path
$file_path = $this->file_path.$this->file_extension;
// Maybe move the file
$this->maybe_move_file( $file_path );
// Log the error
error_log( sprintf( '[%s][%s] %s', date( 'd-m-Y H:i:s' ), $type, self::convert_message( $message ) )."\n", 3, $file_path );
return true;
}
/**
* Change the message to the right type if needed
*
* @param mixed $message
*
* @return string
* @author Nicolas Juen
*/
private static function convert_message( $message ) {
if ( is_object( $message ) || is_array( $message ) ) {
$message = print_r( $message, true );
}
return $message;
}
/**
* Rename the file if exceed the file max retention
*
* @param $file_path
*
* @author Nicolas Juen
*/
private function maybe_move_file( $file_path ) {
// If the file exists
if ( ! is_file( $file_path ) ) {
return;
}
if ( ! $this->exceed_retention( filesize( $file_path ) ) ) {
return;
}
// Rename the file
rename( $file_path, sprintf( '%s-%s%s', $this->file_path, date( 'Y-m-d-H-i-s' ), $this->file_extension ) );
}
/**
* Check retention size is exceeded or not
*
* @param $size
*
* @return bool
*/
private function exceed_retention( $size ) {
return $size > $this->retention_size;
}
}