Clean Log Files manually
Posted by: bnicer (---.vodafonexdsl.co.uk)
Date: June 03, 2022 04:43AM

Large log files really slow down performance, but deleting them automatically when you close down WampServer may not be ideal either. At times you want to study the logs to track a recurrent error, for instance.

To do this you could manually backup log files once they exceed a certain size.

I wrote a Powershell script that I would like to share on this forum.

It's reasonably short.

First instructions:

1) Copy, paste the code in a text file, saving as "backup logs.ps1".
2) Create a shortcut link to the file. (On Powershell shortcuts, see below.)
3) Go into your logs folder and create a subfolder called "backup".
4) Right-click the "backup logs" shortcut and select "Run as administrator".

The rest is automatic. Logs over 500kb are backed up. You can edit the size limit to suit your needs.

Don't forget to turn off "Automatic Cleaning" under "Wamp Settings":

– Clean log files automatically
– Clean tmp directory automically


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CODE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##Requires -RunAsAdministrator # disabled here is "#Requires -RunAsAdministrator"

$admin = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544"winking smiley
$source = "F:\wamp\logs\"
$backup = "F:\wamp\logs\backup\"
$access = "access.log"
$apache_error = "apache_error.log"
$custom = "custom.log"
$err = "error.log"
$mariadb = "mariadb.log"
$mysql = "mysql.log"
$php_error = "php_error.log"
$ssl_access = "ssl_access.log"
$ssl_error = "ssl_error.log"
$ssl_request = "ssl_request.log"
$wamptrace = "wamptrace.log"
$xdebug = "xdebug.log"
$ServiceName1 = 'wampapache64'
$ServiceName2 = 'wampmysqld64'

function Backup-WAMPLogs($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) {
Write-Host "`n`nBackup-WAMPLogs started at:- $(Get-date)`n"

# find out if files exist
$aa = Test-Path $source$a
$bb = Test-Path $source$b
$cc = Test-Path $source$c
$dd = Test-Path $source$d
$ee = Test-Path $source$e
$ff = Test-Path $source$f
$gg = Test-Path $source$g
$hh = Test-Path $source$h
$ii = Test-Path $source$i
$jj = Test-Path $source$j
$kk = Test-Path $source$k
$ll = Test-Path $source$l

# create missing file (silent)
if (-not ($aa)) { New-Item $source$a -ItemType 'file' -force | Out-Null }
if (-not ($bb)) { New-Item $source$b -ItemType 'file' -force | Out-Null }
if (-not ($cc)) { New-Item $source$c -ItemType 'file' -force | Out-Null }
if (-not ($dd)) { New-Item $source$d -ItemType 'file' -force | Out-Null }
if (-not ($ee)) { New-Item $source$e -ItemType 'file' -force | Out-Null }
if (-not ($ff)) { New-Item $source$f -ItemType 'file' -force | Out-Null }
if (-not ($gg)) { New-Item $source$g -ItemType 'file' -force | Out-Null }
if (-not ($hh)) { New-Item $source$h -ItemType 'file' -force | Out-Null }
if (-not ($ii)) { New-Item $source$i -ItemType 'file' -force | Out-Null }
if (-not ($jj)) { New-Item $source$j -ItemType 'file' -force | Out-Null }
if (-not ($kk)) { New-Item $source$k -ItemType 'file' -force | Out-Null }
if (-not ($ll)) { New-Item $source$l -ItemType 'file' -force | Out-Null }


# check file sizes
# small files won't need to be backed up
$aaa = (Get-Item $source$a).length -gt 500kb
$bbb = (Get-Item $source$b).length -gt 500kb
$ccc = (Get-Item $source$c).length -gt 500kb
$ddd = (Get-Item $source$d).length -gt 500kb
$eee = (Get-Item $source$e).length -gt 500kb
$fff = (Get-Item $source$f).length -gt 500kb
$ggg = (Get-Item $source$g).length -gt 500kb
$hhh = (Get-Item $source$h).length -gt 500kb
$iii = (Get-Item $source$i).length -gt 500kb
$jjj = (Get-Item $source$j).length -gt 500kb
$kkk = (Get-Item $source$k).length -gt 500kb
$lll = (Get-Item $source$l).length -gt 500kb


# file size
$aaaa = fsize $a
$bbbb = fsize $b
$cccc = fsize $c
$dddd = fsize $d
$eeee = fsize $e
$ffff = fsize $f
$gggg = fsize $g
$hhhh = fsize $h
$iiii = fsize $i
$jjjj = fsize $j
$kkkk = fsize $k
$llll = fsize $l

# units (kb or bytes)
$au = units $a
$bu = units $b
$cu = units $c
$du = units $d
$eu = units $e
$fu = units $f
$gu = units $g
$hu = units $h
$iu = units $i
$ju = units $j
$ku = units $k
$lu = units $l

# to string
$aaaaa = "$aaaa " + $au
$bbbbb = "$bbbb " + $bu
$ccccc = "$cccc " + $cu
$ddddd = "$dddd " + $du
$eeeee = "$eeee " + $eu
$fffff = "$ffff " + $fu
$ggggg = "$gggg " + $gu
$hhhhh = "$gggg " + $hu
$iiiii = "$gggg " + $iu
$jjjjj = "$gggg " + $ju
$kkkkk = "$gggg " + $ku
$lllll = "$gggg " + $lu

# stop wamp server
if ($aaa -or $bbb -or $ccc -or $ddd -or $eee -or $fff -or $ggg -or $hhh -or $iii -or $jjj -or $kkk -or $lll)
{
$arrService1 = Get-Service -Name $ServiceName1
$arrService2 = Get-Service -Name $ServiceName2
if ($arrService1.Status -eq "Running" -or $arrService2.Status -eq "Running"winking smiley {
Write-Host "=========================`n"
[int]$stat = 1
if ($arrService2.Status -eq "Running"winking smiley {
$stat += 2
NET STOP $ServiceName2 # wampmysqld64
}
if ($arrService1.Status -eq "Running"winking smiley {
$stat += 1
NET STOP $ServiceName1 # wampapache64
}
Write-Host "=========================`n"
}
}

# backup logs
if ($aaa) { backup $a $aaaaa }
else { output $a $aaaaa }
if ($bbb) { backup $b $bbbbb }
else { output $b $bbbbb }
if ($ccc) { backup $c $ccccc }
else { output $c $ccccc }
if ($ddd) { backup $d $ddddd }
else { output $d $ddddd }
if ($eee) { backup $e $eeeee }
else { output $e $eeeee }
if ($fff) { backup $f $fffff }
else { output $f $fffff }
if ($ggg) { backup $g $ggggg }
else { output $g $ggggg }
if ($hhh) { backup $h $hhhhh }
else { output $g $hhhhh }
if ($iii) { backup $h $iiiii }
else { output $i $iiiii }
if ($jjj) { backup $j $jjjjj }
else { output $j $jjjjj }
if ($kkk) { backup $k $kkkkk }
else { output $k $kkkkk }
if ($lll) { backup $l $lllll }
else { output $l $lllll }


# restart wamp server
if ($stat -gt 1) { Write-Host "=========================`n" }
if ($stat -eq 2 -or $stat -eq 4) {
NET START $ServiceName1
}
if ($stat -eq 3 -or $stat -eq 4) {
NET START $ServiceName2
}
if ($stat -gt 1) { Write-Host "=========================`n" }

Write-Host "Backup-WAMPLogs finished at:- $(Get-date)"
}

function fsize ($s) {
[int]$fs = (Get-Item $source$s).length
if ($fs -gt 1023) { $fs = $fs/1024 }
return $fs
}

function units ($s) {
[int]$fs = (Get-Item $source$s).length
if ($fs -gt 1023) { $u = 'kb' }
else { $u = 'bytes' }
return $u
}

function output ($s, $sssss) {
Write-Host "Backup-WAMPLogs:- $(Get-date) -`nThe log file '" -nonewline
Write-Host "$source$s" -foreground red -nonewline
Write-Host "' [" -nonewline
Write-Host "$sssss" -foreground green -nonewline
Write-Host "] does not require rotation yet.`n"
}

function backup($s, $sssss) {
try
{
$lastWriteTime = (Get-Item $source$s).LastWriteTime.ToString("yyyy-MM-dd-HH-mm-ss"winking smiley
$dest = $backup+$lastWriteTime+'-'+$s
Move-Item $source$s $dest -force
New-Item $source$s -ItemType 'file' -force | Out-Null # silent
# Check for errors
if ($?)
{
Write-Host "Backup-WAMPLogs:- $(Get-date) -`nThe log file '" -nonewline
Write-Host "$source$s" -foreground red -nonewline
Write-Host "' [" -nonewline
Write-Host "$sssss" -foreground green -nonewline
Write-Host "] has been backed up to path`n'" -nonewline
Write-Host "$dest" -foreground red -nonewline
Write-Host "'.`n"
}
else
{
Write-Host "Backup-WAMPLogs:- $(Get-date) -`nThe log file '" -nonewline
Write-Host "$source$s" -foreground red -nonewline
Write-Host "' could not be rotated.`n"
}
}
# Catch exceptions
catch
{
Write-Host "System.Exception on:- $(Get-date) - $($Error[0].Exception.Message)"
}
}

if (-not ($admin))
{
Write-Host "`n##############################################################
## The current Windows PowerShell session is not running as ##
## Administrator. Start Windows PowerShell by using the Run ##
## as Administrator option, and then try running the script ##
## again. Sorry. ##
##############################################################`n`n"
}
else
{
$title = "##############################
### Back up WAMP log files ###
##############################`n`n"
$notitle= "`n"
$message = "Do you wish to continue?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"If WAMP server is running, the services may be stopped and restarted."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Abort this script.`n`n"

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
{
0 {
Backup-WAMPLogs $access $apache_error $mysql $php_error $ssl_access $ssl_error $ssl_request
Write-Host "`n`n Directory: $source"
Get-ChildItem $source -Filter "*.log" | format-table LastWriteTime, Length, Name
Write-Host " Directory: $backup"
Get-ChildItem $backup -Filter "*.log" | format-table LastWriteTime, Length, Name
}
1 {"You selected No."}
}
}

Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown"winking smiley

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END CODE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If necessary on your computer, apply a signature to the Powershell file to run.

If you use the shortcut method, edit the target in the Properties dialog:

Target: "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe " -ExecutionPolicy Bypass -File "[Directory]\backup logs.ps1" -NoProfile

Start in: [Directory]

You can put the shortcut anywhere. Pinned to Start, taskbar, desktop, Wampserver Start Menu ...

Good luck!

Oh, and 'winking smileys' please convert to ). (Closing parenthesis/round bracket).



Edited 1 time(s). Last edit at 06/03/2022 07:01AM by bnicer.

Options: ReplyQuote
Re: Clean Log Files manually
Posted by: Otomatic (Moderator)
Date: June 03, 2022 10:19AM

Hi,

- There is already a way to set up the cleaning of the log files!
Right-Click -> Wamp settings -> Automatic Cleaming

- There is already a way to empty all log files!
Right-Click -> Restart from zero

See Right-Click -> Help -> Help Refresh - Restart from zero

There is also a method for Apache logs, it is mod_log_rotate: [www.apachelounge.com]


I would not add your Powershell script in Wampserver, but everyone is free to do so.

> but deleting them automatically when you close down WampServer
Small precision:
It is at the startup of Wampserver that the sizes of the log files are checked and those possibly cleaned.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Clean Log Files manually
Posted by: bnicer (---.vodafonexdsl.co.uk)
Date: June 03, 2022 03:16PM

You say 'clean', I say 'delete'. I noticed that 'Restart from zero' also 'cleans' the logs. Maybe it shouldn't?

I'm in a situation where currently I'm trying to establish why Apache v.2.4.38a runs a Perl module while v.2.4.51 does not. Perl is much harder to debug than PHP, at least in this example. It loads into the 'htm' page as a snippet.

<!--#include virtual="cgi-bin/gb.cgi"-->


So I'm not even sure what module yet. Image::Magick or Authen::Captcha?

Looking at logs is helpful.


You probably can't help me, but for the sake of the argument, 'access.log' shows this on v.2.4.51:


::1 - - [03/Jun/2022:12:44:36 +0100] "-" 408 -


Right now I'm hoping to reproduce it on v.2.4.38a. I pressed 'Restart from zero' (unfortunately). I think there's some nonsense going on with a Perl module that surreptitiously loads 'google.ad.js' or something similar. I can't find it again. Briefly I saw it before pressing 'Restart from zero'. It won't happen again. "winking smiley

The lesson is this, be careful when you clean/delete logs. They exist for a reason.

Wampserver 3.2.9 is a good job, by the way, in every other respect.

Options: ReplyQuote
Re: Clean Log Files manually
Posted by: bnicer (---.vodafonexdsl.co.uk)
Date: June 03, 2022 04:12PM

FWIW -- I found the error comparing "F:\wamp\bin\apache\apache2.4.51\conf\httpd.conf" and "F:\wamp\bin\apache\apache2.4.38a\conf\httpd.conf".

ScriptAlias /cgi-bin/ "${SRVROOT}/cgi-bin/


SRVROOT was defined further down. My mistake.

There were these errors in 'ssl_error.log':

[Fri Jun 03 14:02:55.643815 2022] [cgi:error] [pid 11296:tid 1292] [client ::1:65482] AH02811: script not found or unable to stat: F:/wamp/bin/apache/apache2.4.51/cgi-bin/gb.cgi, referer: [www.XXXXXXXXXXXX]
[Fri Jun 03 14:02:55.643815 2022] [include:error] [pid 11296:tid 1292] [client ::1:65482] unable to include "cgi-bin/gb.cgi" in parsed file F:/wamp - 303/www/pid3.htm, subrequest returned 404, referer: [www.XXXXXXXXXXXX]

Fixed, thanks to error log.

Options: ReplyQuote


Sorry, only registered users may post in this forum.