Bearsampp 2026.5.5
Loading...
Searching...
No Matches
Log Class Reference

Static Public Member Functions

static debug ($data, $file=null)
static error ($data, $file=null)
static flush ()
static getBufferSize ()
static getStats ()
static info ($data, $file=null)
static init ()
static initClass ($classInstance)
static reloadClass ($classInstance)
static reset ()
static separator ()
static setBufferSize ($size)
static trace ($data, $file=null)
static warning ($data, $file=null)

Data Fields

const DEBUG = 'DEBUG'
const ERROR = 'ERROR'
const INFO = 'INFO'
const TRACE = 'TRACE'
const WARNING = 'WARNING'

Static Private Member Functions

static write ($data, $type, $file=null)

Static Private Attributes

static $logBuffer = []
static $logBufferSize = 50
static $logStats
static $shutdownRegistered = false

Detailed Description

Log class providing static logging methods with buffered I/O.

Supports five log levels (TRACE, DEBUG, INFO, WARNING, ERROR), a separator utility, class-lifecycle helpers, and a configurable write buffer to reduce file-system pressure.

Call Log\init() once during bootstrap (after globals are available) to register the shutdown flush handler.

Usage:

Log::info('Application started');
Log::error('Something went wrong');
static info($data, $file=null)
static init()
Definition class.log.php:58
static error($data, $file=null)

Definition at line 28 of file class.log.php.

Member Function Documentation

◆ debug()

debug ( $data,
$file = null )
static

Logs a DEBUG-level message.

Parameters
mixed$data
string | null$file

Definition at line 287 of file class.log.php.

288 {
289 self::write($data, self::DEBUG, $file);
290 }
static write($data, $type, $file=null)
Definition class.log.php:78

References write().

Referenced by ActionQuit\__construct(), Action\call(), Util\changePath(), BinApache\changePort(), BinMailpit\changePort(), BinMariadb\changePort(), BinMemcached\changePort(), BinMysql\changePort(), BinPostgresql\changePort(), BinXlight\changePort(), QuickPick\checkDownloadId(), ActionQuit\checkForOrphanedProcesses(), BinApache\checkPort(), BinMailpit\checkPort(), BinMariadb\checkPort(), BinMemcached\checkPort(), BinMysql\checkPort(), BinPostgresql\checkPort(), BinXlight\checkPort(), Csrf\cleanupExpiredTokens(), ActionQuit\cleanupTemporaryFiles(), Win32Native\countFilesFolders(), Win32Native\countFilesFoldersCOM(), Win32Native\createShortcut(), QuickPick\fetchAndUnzipModule(), Csrf\generateToken(), Util\getAppBinsRegKey(), Win32Native\getDefaultBrowser(), Util\getFilesToScan(), Util\getHeaders(), HttpClient\getHttpHeaders(), Win32Native\getInstalledBrowsers(), Util\getLatestVersion(), QuickPick\getModuleUrl(), Win32Native\getProcessList(), Win32Native\getServiceInfo(), Win32Native\getSpecialFolderPath(), QuickPick\getVersions(), BinPostgresql\handleNonPostgresUsage(), Win32Service\infos(), QuickPick\installModule(), Win32Native\killProcess(), Win32Native\listServices(), QuickPick\logHeaders(), ActionQuit\performQuickCleanupVerification(), Action\process(), ActionQuit\processWindow(), QuickPick\rebuildQuickpickJson(), BinApache\refreshAlias(), BinApache\refreshConf(), BinApache\refreshVhosts(), QuickPick\regenerateMenuSafe(), Win32Native\registryDeleteKey(), Win32Native\registryDeleteValue(), Win32Native\registryExists(), Win32Native\registryGetValue(), Win32Native\registrySetValue(), Symlinks\safeRemoveSymlink(), ActionStartup\scanFolders(), BinApache\setEnable(), BinMailpit\setEnable(), BinMariadb\setEnable(), BinMemcached\setEnable(), BinMysql\setEnable(), BinNodejs\setEnable(), BinPhp\setEnable(), BinPostgresql\setEnable(), BinXlight\setEnable(), Util\setFileScanCache(), Util\setFileScanCacheDuration(), Win32Service\start(), BinApache\switchVersion(), BinMailpit\switchVersion(), BinMariadb\switchVersion(), BinMemcached\switchVersion(), BinMysql\switchVersion(), BinNodejs\switchVersion(), BinPhp\switchVersion(), BinPostgresql\switchVersion(), BinXlight\switchVersion(), Core\unzipFile(), AppPhpmyadmin\updateConfig(), AppPhppgadmin\updateConfig(), BinApache\updateConfig(), BinMailpit\updateConfig(), BinMariadb\updateConfig(), BinMemcached\updateConfig(), BinMysql\updateConfig(), BinNodejs\updateConfig(), BinPhp\updateConfig(), BinPostgresql\updateConfig(), BinXlight\updateConfig(), Module\updateConfig(), ToolGit\updateConfig(), QuickPick\updateModuleConfig(), Csrf\validateToken(), ActionQuit\verifyServicesStoppedAndCleanup(), ActionStartup\writeLog(), Batch\writeLog(), CommandRunner\writeLog(), Nssm\writeLog(), Registry\writeLog(), Win32Service\writeLog(), and WinBinder\writeLog().

◆ error()

error ( $data,
$file = null )
static

Logs an ERROR-level message. Errors bypass the buffer and are written immediately.

Parameters
mixed$data
string | null$file

Definition at line 321 of file class.log.php.

322 {
323 self::write($data, self::ERROR, $file);
324 }

References write().

Referenced by ActionLoading\__construct(), BinApache\changePort(), BinMailpit\changePort(), BinMariadb\changePort(), BinMemcached\changePort(), BinMysql\changePort(), BinPostgresql\changePort(), BinXlight\changePort(), QuickPick\checkDownloadId(), ActionQuit\checkForOrphanedProcesses(), BinApache\checkPort(), BinMailpit\checkPort(), BinMariadb\checkPort(), BinMemcached\checkPort(), BinMysql\checkPort(), BinPostgresql\checkPort(), BinXlight\checkPort(), ActionQuit\cleanupTemporaryFiles(), Win32Native\countFilesFolders(), Win32Native\countFilesFoldersCOM(), Win32Native\createShortcut(), Module\createSymlink(), Symlinks\deleteCurrentSymlinks(), QuickPick\fetchAndUnzipModule(), Win32Native\findProcessesByName(), UtilString\generateSecureBytes(), UtilString\generateSecureToken(), Csrf\generateToken(), HttpClient\get(), Core\getAppVersion(), Util\getCacheIntegrityKey(), HttpClient\getCurlHeaders(), HttpClient\getCurlHttpHeaders(), Core\getFileFromUrl(), Util\getLatestVersion(), QuickPick\getModuleUrl(), Win32Native\getProcessInfo(), Win32Native\getProcessList(), QuickPick\getQuickpickJson(), Win32Native\getServiceInfo(), Win32Native\getSpecialFolderPath(), QuickPick\getVersions(), Splash\init(), BinMariadb\initData(), QuickPick\installModule(), Util\installService(), Symlinks\isPathWithinAllowedBase(), Win32Native\killProcess(), Win32Native\listServices(), ActionCheckVersion\processWindow(), UtilString\random(), Win32Native\registryDeleteKey(), Win32Native\registryDeleteValue(), Win32Native\registryExists(), Win32Native\registrySetValue(), AppPhpmyadmin\reload(), AppPhppgadmin\reload(), BinApache\reload(), BinMailpit\reload(), BinMariadb\reload(), BinMemcached\reload(), BinMysql\reload(), BinNodejs\reload(), BinPhp\reload(), BinPostgresql\reload(), BinXlight\reload(), ToolBruno\reload(), ToolComposer\reload(), ToolGhostscript\reload(), ToolGit\reload(), ToolNgrok\reload(), ToolPerl\reload(), ToolPowerShell\reload(), ToolPython\reload(), ToolRuby\reload(), Util\removeService(), Symlinks\safeRemoveSymlink(), Win32Service\start(), Util\startService(), TplAppReload\triggerReload(), Core\unzipFile(), AppPhpmyadmin\updateConfig(), AppPhppgadmin\updateConfig(), BinApache\updateConfig(), BinMailpit\updateConfig(), BinMariadb\updateConfig(), BinMemcached\updateConfig(), BinMysql\updateConfig(), BinNodejs\updateConfig(), BinPhp\updateConfig(), BinPostgresql\updateConfig(), BinXlight\updateConfig(), QuickPick\updateModuleConfig(), ActionQuit\verifyServicesStoppedAndCleanup(), ActionQuit\verifySymlinksRemoved(), and Nssm\writeLogError().

◆ flush()

flush ( )
static

Flushes the log buffer to disk. Groups entries by file to minimise file operations. Falls back to error_log() if globals are unavailable or a write fails.

Returns
void

Definition at line 142 of file class.log.php.

143 {
144 if (empty(self::$logBuffer)) {
145 return;
146 }
147
148 global $bearsamppCore;
149
150 // If the core global is gone (e.g. during an abnormal shutdown), fall back to error_log
151 if (!isset($bearsamppCore)) {
152 foreach (self::$logBuffer as $log) {
153 error_log('[' . date('Y-m-d H:i:s', $log['time']) . '] [' . $log['type'] . '] ' . $log['data']);
154 }
155 self::$logStats['flushed'] += count(self::$logBuffer);
156 self::$logBuffer = [];
157 return;
158 }
159
160 // Group logs by destination file
161 $logsByFile = [];
162 foreach (self::$logBuffer as $log) {
163 $logsByFile[$log['file']][] = $log;
164 }
165
166 foreach ($logsByFile as $file => $logs) {
167 $content = '';
168 foreach ($logs as $log) {
169 $content .= '[' . date('Y-m-d H:i:s', $log['time']) . '] # ' .
170 APP_TITLE . ' ' . $bearsamppCore->getAppVersion() . ' # ' .
171 $log['type'] . ': ' . $log['data'] . PHP_EOL;
172 }
173
174 $written = @file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
175 if ($written === false) {
176 // File write failed — ensure entries are not silently lost
177 foreach ($logs as $log) {
178 error_log('[' . $log['type'] . '] ' . $log['data'] . ' (target: ' . $file . ')');
179 }
180 }
181
182 self::$logStats['writes']++;
183 }
184
185 self::$logStats['flushed'] += count(self::$logBuffer);
186 self::$logBuffer = [];
187 }
global $bearsamppCore
const APP_TITLE
Definition root.php:13

References $bearsamppCore, and APP_TITLE.

Referenced by write().

◆ getBufferSize()

getBufferSize ( )
static

Returns the current log buffer size.

Returns
int

Definition at line 234 of file class.log.php.

235 {
236 return self::$logBufferSize;
237 }

◆ getStats()

getStats ( )
static

Returns the current log buffer statistics.

Returns
array Array with keys 'buffered', 'flushed', and 'writes'.

Definition at line 211 of file class.log.php.

212 {
213 return self::$logStats;
214 }

◆ info()

◆ init()

init ( )
static

Registers the shutdown flush handler. Call once during bootstrap after globals are initialised.

Returns
void

Definition at line 58 of file class.log.php.

59 {
60 if (!self::$shutdownRegistered) {
61 register_shutdown_function([__CLASS__, 'flush']);
62 self::$shutdownRegistered = true;
63 }
64 }

Referenced by Root\register(), and write().

◆ initClass()

◆ reloadClass()

reloadClass ( $classInstance)
static

◆ reset()

reset ( )
static

Clears the buffer and resets statistics. Useful in tests or when re-initialising the application.

Returns
void

Definition at line 195 of file class.log.php.

196 {
197 self::$logBuffer = [];
198 self::$shutdownRegistered = false;
199 self::$logStats = [
200 'buffered' => 0,
201 'flushed' => 0,
202 'writes' => 0,
203 ];
204 }

◆ separator()

separator ( )
static

Appends a separator line to each log file that does not already end with one.

@global object $bearsamppRoot

Definition at line 244 of file class.log.php.

245 {
246 global $bearsamppRoot;
247
248 $logs = [
249 $bearsamppRoot->getLogFilePath(),
250 $bearsamppRoot->getErrorLogFilePath(),
251 $bearsamppRoot->getServicesLogFilePath(),
252 $bearsamppRoot->getRegistryLogFilePath(),
253 $bearsamppRoot->getStartupLogFilePath(),
254 $bearsamppRoot->getBatchLogFilePath(),
255 $bearsamppRoot->getWinbinderLogFilePath(),
256 ];
257
258 $separator = '========================================================================================' . PHP_EOL;
259 foreach ($logs as $log) {
260 if (!file_exists($log)) {
261 continue;
262 }
263 $logContent = @file_get_contents($log);
264 if ($logContent !== false && !str_ends_with($logContent, $separator)) {
265 file_put_contents($log, $separator, FILE_APPEND);
266 }
267 }
268 }
global $bearsamppRoot

References $bearsamppRoot.

Referenced by Root\register().

◆ setBufferSize()

setBufferSize ( $size)
static

Sets the log buffer size.

Parameters
int$sizeNew buffer size (1–1000).
Returns
void

Definition at line 222 of file class.log.php.

223 {
224 if ($size > 0 && $size <= 1000) {
225 self::$logBufferSize = $size;
226 }
227 }

◆ trace()

trace ( $data,
$file = null )
static

Logs a TRACE-level message.

Parameters
mixed$data
string | null$file

Definition at line 276 of file class.log.php.

277 {
278 self::write($data, self::TRACE, $file);
279 }

References write().

Referenced by ActionExec\__construct(), ActionLoading\__construct(), ActionManualRestart\__construct(), ActionRebuildini\__construct(), Win32Service\callWin32Service(), BinMysql\changeRootPassword(), ActionLoading\checkAllServicesStarted(), ActionStartup\checkApacheServiceWithTimeout(), ActionStartup\checkMySQLServiceWithTimeout(), BinMysql\checkPort(), BinMysql\checkRootPassword(), Win32Service\create(), OpenSsl\createCrt(), Win32Service\delete(), Win32Service\ensureReset(), Win32Service\fastServiceCheck(), Win32Service\infos(), initClass(), BinMariadb\initData(), BinMysql\initData(), ActionStartup\installServices(), ActionStartup\installServicesSequential(), Win32Service\isInstalled(), Win32Service\isPaused(), Win32Service\isPending(), Win32Service\isRunning(), Win32Service\isStopped(), ActionStartup\prepareService(), ActionLoading\processLoading(), ActionStartup\processWindow(), ActionSwitchVersion\processWindow(), BinMailpit\rebuildConf(), BinApache\refreshAlias(), BinApache\refreshConf(), BinApache\refreshVhosts(), reloadClass(), Config\replaceAll(), Util\replaceInFile(), ActionStartup\rotationLogs(), ActionStartup\scanFolders(), BinMailpit\setVersion(), BinPhp\setVersion(), Util\startLoading(), Win32Service\status(), Win32Service\stop(), ActionQuit\terminatePhpProcesses(), TplAppReload\triggerReload(), Core\unzipFile(), BinPhp\updateConfig(), ActionSwitchVersion\updateConfigVersion(), and Win32Service\waitForServiceDeletion().

◆ warning()

◆ write()

write ( $data,
$type,
$file = null )
staticprivate

Writes a message to a log file, using the buffer to reduce I/O.

The file path is resolved as follows:

  • If $file is provided by the caller it is used as-is.
  • Otherwise the default path is chosen based on $type (error log vs. main log), then overridden with the homepage log when not running in root context.
Parameters
mixed$dataThe message to log.
string$typeOne of the Log level constants.
string | null$fileExplicit file path, or null to use the default.

Definition at line 78 of file class.log.php.

79 {
81
82 // Safety check: if globals aren't initialised, fall back to error_log
83 if (!isset($bearsamppRoot) || !isset($bearsamppCore) || !isset($bearsamppConfig)) {
84 error_log('[' . $type . '] ' . $data);
85 return;
86 }
87
88 // Lazily register the shutdown handler if init() was not called explicitly
89 self::init();
90
91 // Resolve default file path only when the caller did not supply one
92 if ($file === null) {
93 $file = $type === self::ERROR
94 ? $bearsamppRoot->getErrorLogFilePath()
95 : $bearsamppRoot->getLogFilePath();
96
97 if (!$bearsamppRoot->isRoot()) {
98 $file = $bearsamppRoot->getHomepageLogFilePath();
99 }
100 }
101
102 $verbose = [];
103 $verbose[Config::VERBOSE_SIMPLE] = $type === self::ERROR || $type === self::WARNING;
104 $verbose[Config::VERBOSE_REPORT] = $verbose[Config::VERBOSE_SIMPLE] || $type === self::INFO;
105 $verbose[Config::VERBOSE_DEBUG] = $verbose[Config::VERBOSE_REPORT] || $type === self::DEBUG;
106 $verbose[Config::VERBOSE_TRACE] = $verbose[Config::VERBOSE_DEBUG] || $type === self::TRACE;
107
108 $writeLog = false;
109 if ($bearsamppConfig->getLogsVerbose() === Config::VERBOSE_SIMPLE && $verbose[Config::VERBOSE_SIMPLE]) {
110 $writeLog = true;
111 } elseif ($bearsamppConfig->getLogsVerbose() === Config::VERBOSE_REPORT && $verbose[Config::VERBOSE_REPORT]) {
112 $writeLog = true;
113 } elseif ($bearsamppConfig->getLogsVerbose() === Config::VERBOSE_DEBUG && $verbose[Config::VERBOSE_DEBUG]) {
114 $writeLog = true;
115 } elseif ($bearsamppConfig->getLogsVerbose() === Config::VERBOSE_TRACE && $verbose[Config::VERBOSE_TRACE]) {
116 $writeLog = true;
117 }
118
119 if ($writeLog) {
120 self::$logBuffer[] = [
121 'file' => $file,
122 'data' => $data,
123 'type' => $type,
124 'time' => time(),
125 ];
126 self::$logStats['buffered']++;
127
128 // Flush immediately for errors, or when the buffer is full
129 if ($type === self::ERROR || count(self::$logBuffer) >= self::$logBufferSize) {
130 self::flush();
131 }
132 }
133 }
const VERBOSE_REPORT
const VERBOSE_SIMPLE
const VERBOSE_TRACE
const VERBOSE_DEBUG
static flush()
global $bearsamppConfig
Definition homepage.php:41

References $bearsamppConfig, $bearsamppCore, $bearsamppRoot, flush(), init(), Config\VERBOSE_DEBUG, Config\VERBOSE_REPORT, Config\VERBOSE_SIMPLE, and Config\VERBOSE_TRACE.

Referenced by debug(), error(), info(), trace(), and warning().

Field Documentation

◆ $logBuffer

$logBuffer = []
staticprivate

Definition at line 37 of file class.log.php.

◆ $logBufferSize

$logBufferSize = 50
staticprivate

Definition at line 40 of file class.log.php.

◆ $logStats

$logStats
staticprivate
Initial value:
= [
'buffered' => 0,
'flushed' => 0,
'writes' => 0,
]

Definition at line 46 of file class.log.php.

◆ $shutdownRegistered

$shutdownRegistered = false
staticprivate

Definition at line 43 of file class.log.php.

◆ DEBUG

const DEBUG = 'DEBUG'

Definition at line 33 of file class.log.php.

◆ ERROR

const ERROR = 'ERROR'

Definition at line 30 of file class.log.php.

◆ INFO

const INFO = 'INFO'

Definition at line 32 of file class.log.php.

◆ TRACE

const TRACE = 'TRACE'

Definition at line 34 of file class.log.php.

◆ WARNING

const WARNING = 'WARNING'

Definition at line 31 of file class.log.php.


The documentation for this class was generated from the following file: