PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.1.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.1.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / logger.php

logger.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.1.0, at includes/classes/logger.php

85 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Classes;
4
5 use StoreEngine\Classes\Exceptions\StoreEngineRuntimeException;
6 use StoreEngine\Utils\Constants;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Logger {
13
14 const SUCCESS = 'success';
15 const WARNING = 'warning';
16 const ERROR = 'error';
17 const INFO = 'info';
18 const DEBUG = 'debug';
19 const EMERGENCY = 'emergency';
20 const CRITICAL = 'critical';
21 const ALERT = 'alert';
22
23 public static function log( string $title, $content, string $status = 'success', string $module = 'system' ) {
24 try {
25 self::log_to_db( $title, $content, $status, $module );
26 } catch ( \Throwable $t ) {
27 error_log( 'Unable to write StoreEngine log data to database. Error: ' . $t->getMessage() );
28
29 // Fallback.
30 try {
31 self::log_to_file( $title, $content, $status, $module );
32 } catch ( \Throwable $t ) {
33 error_log( 'Unable to write StoreEngine log data to log file. Error: ' . $t->getMessage() );
34 }
35 }
36 }
37
38 public static function log_to_db( string $title, $content, string $status = 'success', string $module = 'system' ) {
39 $log = new LogItem();
40 $log->set_props( [
41 'date' => current_time( 'mysql' ),
42 'module' => $module,
43 'title' => $title,
44 'status' => $status,
45 'content' => is_array( $content ) || is_object( $content ) ? json_encode( $content ) : $content,
46 ] );
47
48 $log->save();
49 }
50
51 public static function log_to_file( string $title, $content, string $status = 'success', string $module = 'system' ) {
52 if ( ! defined( 'STOREENGINE_LOGS_DIR' ) ) {
53 throw new StoreEngineRuntimeException( 'Logs directory is not defined.' );
54 }
55
56 if ( ! file_exists( STOREENGINE_LOGS_DIR ) ) {
57 wp_mkdir_p( STOREENGINE_LOGS_DIR );
58 }
59
60 $message = is_string( $content ) ? $title . $content : $title;
61
62 if ( ! is_scalar( $content ) ) {
63 $message .= PHP_EOL . json_encode( $content );
64 }
65
66 $log_file = STOREENGINE_LOGS_DIR . 'storeengine-' . $module . '-' . gmdate( 'Y-m-d' ) . '.log';
67 $timestamp = gmdate( 'c' );
68 $module = strtoupper( $module );
69 $status = strtoupper( $status );
70 $message = "[$timestamp] – [$module] [$status] $message";
71
72 // Append via WP_Filesystem — Plugin Check disallows direct fopen/fwrite.
73 // Log files rotate daily by name, so this read-modify-write stays cheap.
74 require_once ABSPATH . 'wp-admin/includes/file.php';
75 global $wp_filesystem;
76 if ( empty( $wp_filesystem ) && ! WP_Filesystem() ) {
77 return;
78 }
79
80 $existing = $wp_filesystem->exists( $log_file ) ? (string) $wp_filesystem->get_contents( $log_file ) : '';
81 $chmod = Constants::is_defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : false;
82 $wp_filesystem->put_contents( $log_file, $existing . $message . PHP_EOL, $chmod );
83 }
84 }
85