Skip to content

Commit

Permalink
Send logs to error_log() by default (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwngallego authored Aug 3, 2021
1 parent 76374a1 commit 5540c9f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 36 deletions.
3 changes: 3 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ You can create an issue in our Github repo:

== Changelog ==

= 1.3.3 =
* Send logs to error_log() by default when logging is not even enabled. Fixes [#85](https://github.com/perfectyorg/perfecty-push-wp/issues/85)

= 1.3.2 =
* Add the plugin links shown in the WordPress Plugin installer
* Icon max width in the Notification details.
Expand Down
11 changes: 7 additions & 4 deletions includes/class-perfecty-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ private function load_dependencies() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/class-perfecty-push-lib-log.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/log/class-perfecty-push-lib-log-writer.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/log/class-perfecty-push-lib-log-db.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/log/class-perfecty-push-lib-log-errorlog.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'lib/class-perfecty-push-lib-cron-check.php';
$this->loader = new Perfecty_Push_Loader();
}
Expand All @@ -228,10 +229,12 @@ public function load_logger() {
$options = get_option( 'perfecty_push', array() );
$enabled_logs = isset( $options['logs_enabled'] ) && $options['logs_enabled'] == 1;

$logger = new Perfecty_Push_Lib_Log_Db();
Perfecty_Push_Lib_Log::init( $logger );
if ( ! $enabled_logs ) {
Perfecty_Push_Lib_Log::disable();
if ( $enabled_logs ) {
$logger = new Perfecty_Push_Lib_Log_Db();
Perfecty_Push_Lib_Log::init( $logger, Perfecty_Push_Lib_Log::DEBUG );
} else {
$logger = new Perfecty_Push_Lib_Log_ErrorLog();
Perfecty_Push_Lib_Log::init( $logger, Perfecty_Push_Lib_Log::INFO );
}
}

Expand Down
66 changes: 49 additions & 17 deletions lib/class-perfecty-push-lib-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
* Logger
*/
class Perfecty_Push_Lib_Log {
public const DEBUG = 'debug';
public const INFO = 'info';
public const WARNING = 'warning';
public const ERROR = 'error';
public const DEBUG = 0;
public const INFO = 1;
public const WARNING = 2;
public const ERROR = 3;

private static $writer;
private static $enabled = true;
private static $level = self::ERROR;

/**
* Sets the Log writer
*
* @param Perfecty_Push_Lib_Log_Writer $writer
* @param int $level Error level
*/
public static function init( Perfecty_Push_Lib_Log_Writer $writer ) {
public static function init( Perfecty_Push_Lib_Log_Writer $writer, $level = self::ERROR ) {
self::$writer = $writer;
self::$level = $level;
}

/**
Expand All @@ -38,48 +41,77 @@ public static function disable() {
/**
* Logs a debug message
*
* @param string $message Message to log
* @param string $message Message to log.
*/
public static function debug( $message ) {
if ( ! self::$enabled ) {
if ( ! self::$enabled || self::$level > self::DEBUG ) {
return;
}
self::$writer->write( self::DEBUG, $message );
self::$writer->write( self::level_string( self::DEBUG ), $message );
}

/**
* Logs a info message
*
* @param string $message Message to log
* @param string $message Message to log.
*/
public static function info( $message ) {
if ( ! self::$enabled ) {
if ( ! self::$enabled || self::$level > self::INFO ) {
return;
}
self::$writer->write( self::INFO, $message );
self::$writer->write( self::level_string( self::INFO ), $message );
}

/**
* Logs a warning message
*
* @param string $message Message to log
* @param string $message Message to log.
*/
public static function warning( $message ) {
if ( ! self::$enabled ) {
if ( ! self::$enabled || self::$level > self::WARNING ) {
return;
}
self::$writer->write( self::WARNING, $message );
self::$writer->write( self::level_string( self::WARNING ), $message );
}

/**
* Logs an error message
*
* @param string $message Message to log
* @param string $message Message to log.
*/
public static function error( $message ) {
if ( ! self::$enabled ) {
if ( ! self::$enabled || self::$level > self::ERROR ) {
return;
}
self::$writer->write( self::ERROR, $message );
self::$writer->write( self::level_string( self::ERROR ), $message );
}

/**
* Get the level string
*
* @param $level int Level code.
*
* @return string
* @since 1.3.3
*/
private static function level_string( $level ) {
$string = '';
switch ( $level ) {
case self::DEBUG:
$string = 'debug';
break;
case self::INFO:
$string = 'info';
break;
case self::WARNING:
$string = 'warning';
break;
case self::ERROR:
$string = 'error';
break;
default:
'';
}
return $string;
}
}
11 changes: 3 additions & 8 deletions lib/log/class-perfecty-push-lib-log-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ class Perfecty_Push_Lib_Log_Db implements Perfecty_Push_Lib_Log_Writer {
/**
* Insert a log entry in the DB
*
* @param string $level Level code
* @param string $message Message to log
* @param string $level Level code.
* @param string $message Message to log.
*/
public function write( $level, $message ) {
$db_version = get_option( 'perfecty_push_db_version' );
if ( $db_version >= 4 ) {
return Perfecty_Push_Lib_Db::insert_log( $level, $message );
} else {
error_log( strtoupper( $level ) . ' | ' . $message );
}
return Perfecty_Push_Lib_Db::insert_log( $level, $message );
}
}
8 changes: 4 additions & 4 deletions lib/log/class-perfecty-push-lib-log-errorlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/***
* Log writter that uses error_log()
*/
class Perfecty_Push_Lib_Log_ErrorLog implements Perfecty_Push_Lib_Log_Writter {
class Perfecty_Push_Lib_Log_ErrorLog implements Perfecty_Push_Lib_Log_Writer {

/**
* Write a message
*
* @param string $level Level code
* @param string $message Message to log
* @param string $level Level code.
* @param string $message Message to log.
*/
public function write( $level, $message ) {
error_log( strtoupper( $level ) . ' | ' . $message );
error_log( strtoupper( $level ) . ' | ' . addslashes( $message ) );
}
}
8 changes: 8 additions & 0 deletions lib/log/class-perfecty-push-lib-log-writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@
* Writer interface
*/
interface Perfecty_Push_Lib_Log_Writer {
/**
* Write a log message
*
* @param string $level Error Level.
* @param string $message Message to log.
*
* @return mixed
*/
public function write ( $level, $message );
}
5 changes: 2 additions & 3 deletions tests/test-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
class LoggerTest extends WP_UnitTestCase
{

private $mocked_vapid_callback = "md5";

public function setUp()
{
parent::setUp();
Expand All @@ -33,6 +31,8 @@ public function tearDown()
*/
public function test_logger_enabled()
{
$writter = new Perfecty_Push_Lib_Log_Db();
Perfecty_Push_Lib_Log::init($writter, Perfecty_Push_Lib_Log::DEBUG);
Perfecty_Push_Lib_Log::enable();
Perfecty_Push_Lib_Log::debug("debug message");
Perfecty_Push_Lib_Log::info("info message");
Expand Down Expand Up @@ -77,7 +77,6 @@ public function test_logger_disabled()
Perfecty_Push_Lib_Log::error("error message");
$logs = Perfecty_Push_Lib_Db::get_logs(0, 10);

var_dump($logs);
$this->assertSame(0, count($logs));
}
}

0 comments on commit 5540c9f

Please sign in to comment.