| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-log.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 4.0.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Logger |
| 28 |
*/ |
| 29 |
class Groups_Log { |
| 30 |
|
| 31 |
/** |
| 32 |
* Process ID. |
| 33 |
* |
| 34 |
* @var int|string |
| 35 |
*/ |
| 36 |
private static $pid = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* Hashes. |
| 40 |
* |
| 41 |
* @var string[] |
| 42 |
*/ |
| 43 |
private static $hashes = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Log message. |
| 47 |
* |
| 48 |
* Always logs errors. Warnings and infos are logged with $force or GROUPS_DEBUG. |
| 49 |
* |
| 50 |
* @param string $message |
| 51 |
* @param int|string|null $level 'info' (default), 'warning', 'error' |
| 52 |
* @param boolean $force |
| 53 |
*/ |
| 54 |
public static function log( $message, $level = null, $force = false ) { |
| 55 |
if ( !is_string( $message ) || strlen( $message ) === 0 ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
if ( !is_string( $level ) ) { |
| 59 |
$level = 'info'; |
| 60 |
} |
| 61 |
$level = strtolower( $level ); |
| 62 |
switch ( $level ) { |
| 63 |
case 'error': |
| 64 |
case 'warning': |
| 65 |
case 'info': |
| 66 |
break; |
| 67 |
default: |
| 68 |
$level = 'info'; |
| 69 |
} |
| 70 |
|
| 71 |
// process or pseudo ID |
| 72 |
if ( self::$pid === null ) { |
| 73 |
if ( function_exists( 'getmypid' ) ) { |
| 74 |
self::$pid = @getmypid(); |
| 75 |
if ( self::$pid === false ) { |
| 76 |
self::$pid = null; |
| 77 |
} |
| 78 |
} |
| 79 |
if ( self::$pid === null ) { |
| 80 |
self::$pid = hash( 'crc32', rand( 0, time() ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$level_str = strtoupper( $level ); |
| 85 |
if ( |
| 86 |
$force || |
| 87 |
defined( 'GROUPS_DEBUG' ) && GROUPS_DEBUG || |
| 88 |
'ERROR' === $level_str |
| 89 |
) { |
| 90 |
$entry = sprintf( |
| 91 |
'Groups [%7s] [%8s] | %s', |
| 92 |
$level_str, |
| 93 |
self::$pid ?? '?', |
| 94 |
$message, |
| 95 |
); |
| 96 |
$hash = md5( $entry ); |
| 97 |
if ( !in_array( $hash, self::$hashes ) ) { |
| 98 |
self::$hashes[] = $hash; |
| 99 |
error_log( $entry ); // phpcs:ignore QITStandard.PHP.DebugCode.DebugFunctionFound, WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|