41 const LOG_SEPARATOR =
'---------------------------------------------------------------------------------------------';
104 public $extensionFilename;
127 public function __construct( $path, $folder =
'logs', $extension =
'php' )
129 $this->path = trailingslashit( trailingslashit( $path ) . $folder );
130 $this->enabled =
true;
131 $this->triggerError =
false;
132 $this->separator = self::LOG_SEPARATOR;
133 $this->extensionFilename = $extension;
136 if ( !file_exists( $this->path ) ) {
137 @wp_mkdir_p( $this->path );
140 $this->logname = sprintf(
'%s%s.%s', $this->path, date(
'Ymd' ), $this->extensionFilename );
143 $handle = @fopen( $this->logname,
"a+" );
144 $this->available = (
false !== $handle );
147 $yesterday_log = sprintf(
'%s%s.%s', $this->path, date(
'Ymd', strtotime(
'-1 days' ) ), $this->extensionFilename );
148 if ( file_exists( $yesterday_log ) ) {
149 $size = @filesize( $yesterday_log );
150 if ( empty( $size ) ) {
151 @unlink( $yesterday_log );
169 public function log( $txt, $title =
'' )
171 if ( $this->enabled && $this->available ) {
174 if ( !is_string( $txt ) && !is_numeric( $txt ) ) {
177 $content = ob_get_contents();
182 do_action(
'wpdk_watchdog_log', $content );
185 $date = sprintf(
'[%s]', date(
'Y-m-d H:i:s' ) );
186 $sepa = substr( $this->separator, 0, ( strlen( $this->separator ) - strlen( $date ) - 1 ) );
187 $output = sprintf(
"%s (%s) %s\n%s\n\n", $date, $title, $sepa, $txt );
188 $handle = fopen( $this->logname,
'a+' );
189 if (
false !== $handle ) {
190 fwrite( $handle, $output );
194 $this->available =
false;
197 if ( $this->triggerError ) {
198 trigger_error( $output );
201 return $this->available;