Skip to content

Commit

Permalink
#6: Enabling additional .log-removal directories
Browse files Browse the repository at this point in the history
  • Loading branch information
lat9 committed Oct 18, 2023
1 parent 798a60d commit d3e4706
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
69 changes: 49 additions & 20 deletions YOUR_ADMIN/includes/init_includes/init_log_manager.php
Expand Up @@ -53,39 +53,68 @@
$keep_until_date = date(DATE_FORMAT . ' H:m:i', $keep_until);

// -----
// Loop through all files present in the /logs directory ...
// Loop through all files present in the /logs and, for zc158 and later, the
// /app/storage/logs, sub-directories as well as any additional directories
// that might be supplied in an optional constant definition.
//
$log_manager_dirs = [
DIR_FS_LOGS,
];
if (is_dir(DIR_FS_CATALOG . 'app/storage/logs')) {
$log_manager_dirs[] = DIR_FS_CATALOG . 'app/storage/logs';
}

// -----
// To remove .log files from other directories, too, use an /admin/extra_datafiles file to
// define the following constant. The constant's definition would look like the following
// to also remove log-files from /includes/modules/payment/paypay/logs and /logs/edit_orders:
//
// define('LOG_MANAGER_EXTRA_DIRECTORIES', DIR_FS_CATALOG . 'includes/modules/payment/paypay/logs' . ';' . DIR_FS_LOGS . '/edit_orders');
//
if (defined('LOG_MANAGER_EXTRA_DIRECTORIES')) {
$log_manager_extra_dirs = explode(';', str_replace(' ', '', LOG_MANAGER_EXTRA_DIRECTORIES));
foreach ($log_manager_extra_dirs as $log_manager_extra) {
if (is_dir($log_manager_extra)) {
$log_manager_dirs[] = $log_manager_extra;
} else {
trigger_error('Unknown directory ($log_manager_extra) identified in LOG_MANAGER_EXTRA_DIRECTORIES.', E_USER_NOTICE);
}
}
unset($log_manager_extra_dirs, $log_manager_extra);
}

$files_removed = 0;
if ($dir = dir(DIR_FS_LOGS)) {
while (($current_file = $dir->read()) !== false) {
// -----
// ... looking for a ".log" file with a name that isn't in the "keep-these" list ...
//
if (strpos($current_file, '.log') !== false && $match_string != '' && !preg_match($match_string, $current_file)) {
foreach ($log_manager_dirs as $log_manager_dir) {
if ($dir = dir($log_manager_dir)) {
while (($current_file = $dir->read()) !== false) {
// -----
// ... that was created prior to the keep-until date ...
// ... looking for a ".log" file with a name that isn't in the "keep-these" list ...
//
$modified = filemtime(DIR_FS_LOGS . DIRECTORY_SEPARATOR . $current_file);
if ($modified !== false && $modified < $keep_until) {
if (strpos($current_file, '.log') !== false && $match_string !== '' && preg_match($match_string, $current_file) !== 1) {
// -----
// ... and remove it.
//
// I'm not a big fan of inhibiting error-reports, but for this case it's possible that multiple
// admins have "hit" the site concurrently and are all causing this script to run.
// ... that was last modified prior to the keep-until date ...
//
@unlink(DIR_FS_LOGS . DIRECTORY_SEPARATOR . $current_file);
$files_removed++;
$current_file = $log_manager_dir . DIRECTORY_SEPARATOR . $current_file;
$modified = is_file($current_file) ? filemtime($current_file) : false;
if ($modified !== false && $modified < $keep_until) {
// -----
// ... remove it.
//
unlink($current_file);
$files_removed++;
}
}
}
$dir->close();
}
$dir->close();
}

// -----
// If one or more files was removed, let the admin know (via message) and log the removal action.
// If one or more .log files was removed, let the admin know (via message) and log the removal action.
//
if ($files_removed != 0) {
$logMessage = sprintf(LOG_MANAGER_FILES_MESSAGE_FORMAT, $files_removed, $keep_until_date);
if ($files_removed !== 0) {
$log_directories = implode($log_manager_dirs, ', ');
$logMessage = sprintf(LOG_MANAGER_FILES_MESSAGE_FORMAT, $files_removed, '.log', $log_directories, $keep_until_date);
$messageStack->add($logMessage, 'success');
error_log(date(PHP_DATE_TIME_FORMAT) . ', ' . $_SESSION['admin_id'] . ": $logMessage" . PHP_EOL, 3, DIR_FS_LOGS . '/log_manager_removal.log');
}
Expand Down
@@ -1,12 +1,14 @@
<?php
// -----
// Part of the Log Manager plugin, created by lat9 (lat9@vinosdefrutastropicales.com)
// Copyright (c) 2017, Vinos de Frutas Tropicales.
// Copyright (c) 2017-2023, Vinos de Frutas Tropicales.
//
// -----
// This message, issued by the plugin's initialization script, is issued if one or more .log file has been removed:
//
// %1$u ... The number of files removed
// %2$s ... The date/time that was used during the check.
// %2$s ... The file prefix (for site-specific files) and extension for the files removed.
// %3$s ... The directory from which the files were removed.
// %4$s ... The date/time that was used during the check.
//
define ('LOG_MANAGER_FILES_MESSAGE_FORMAT', 'Removed %1$u .log file(s) from the /logs directory that were dated prior to %2$s.');
define('LOG_MANAGER_FILES_MESSAGE_FORMAT', 'Removed %1$u %2$s file(s) from these directories (%3$s) that were dated prior to %4$s.');
19 changes: 14 additions & 5 deletions docs/log_manager/readme.html
Expand Up @@ -21,8 +21,6 @@
H1, H2, H3, H4 { font-weight: bold; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
pre, code { padding-left: 25px; font-family: Courier; font-size: 12pt; }
pre.no-pad, code.no-pad { padding: 0; margin: 0; }
ol li, ul li, ul li ul li { margin: 5px 0; padding: 0; }
img { margin: 0 20px; float: left; border: 1px solid #666; }
img.wide { width: 95%; float: none; margin: 0 2.5%; }
Expand All @@ -38,7 +36,7 @@
td, th { padding: 5px; }
th { background-color: #eee; }
.smaller { font-size: smaller; }

.mb-0 { margin-bottom: 0; }
.etabs { margin: 0; padding: 0; }
.tab { display: inline-block; zoom:1; *display:inline; background: #eee; border: solid 1px #999; border-bottom: none; border-radius: 4px 4px 0 0; margin: 0; }
.tab a { font-size: 14px; line-height: 2em; display: block; padding: 0 10px; outline: none; }
Expand All @@ -48,7 +46,7 @@
.tab-container .panel-container { background: #fff; border: solid #666 1px; padding: 10px; border-radius: 0 4px 4px 4px; }
.panel-container { margin-bottom: 10px; }
</style>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
<script src="../jquery-1.7.1.min.js"></script>
<script src="../jquery.easytabs.min.js"></script>
<script>
Expand All @@ -75,8 +73,18 @@ <h2>What it does</h2>
<p>Logs are good things, providing information that allows an after-the-fact analysis if something goes wrong &hellip; but they can start filling up your store's <var>/logs</var> directory if you don't keep a watch on them!</p>
<p>The <em>Log Manager</em> enables you to control how long you keep those <var>.log</var> files, inspecting each file's last-modified date, and then automatically removing (i.e. deleting) any files that are past their &quot;expiration&quot;. There's also a setting to enable you to identify logs that you want to keep (e.g. <em>zcInstall</em> logs).</p>
<p>Once configured, <em>Log Manager</em> inspects your store's <var>/logs</var> directory, starting with v1.1.0, each time an admin successfully logs in to see if there are any candidates for removal &hellip; and, if found, removes them. Starting with v1.2.0, the plugin also creates and updates the file <var>/logs/log_manager_removal.log</var> to record its processing.</p>
<p class="mb-0">v1.3.0 introduces the site-specific capability to identify additional directories from which <var>.log</var> files can be automatically removed. To enable this feature, create a .php file in the <var>/admin/includes/extra_datafiles</var> sub-directory containing:</p>
<pre class="mb-0">
&lt;?php
define('LOG_MANAGER_EXTRA_DIRECTORIES', 'directories-separated-by-a-semi-colon');
</pre>
<p class="mb-0">For example, to also remove .log files from the <var>/includes/modules/payment/paypal/logs</var> and <var>/logs/edit_orders</var>, you'd set the definition as:</p>
<pre>
&lt;?php
define('LOG_MANAGER_EXTRA_DIRECTORIES', DIR_FS_CATALOG . 'includes/modules/payment/paypay/logs' . ';' . DIR_FS_LOGS . '/edit_orders');
</pre>
</div>

<div id="installation">
<p>This section identifies the procedure you'll use to install or remove the plugin for your Zen Cart.</p>

Expand Down Expand Up @@ -140,6 +148,7 @@ <h2>What it does</h2>
<ul>
<li>v1.3.0, 2023-10-18:<ul>
<li>CHANGE: Defer all processing until an admin has logged in.</li>
<li>CHANGE: Enable <var>.log</var> files to be removed from directories in addition to <var>/logs</var>.</li>
</ul></li>
<li>v1.2.0, 2017-08-07:<ul>
<li>CHANGE: Log removal check moved to a $_SESSION variable for the currently-logged-in admin.</li>
Expand Down

0 comments on commit d3e4706

Please sign in to comment.