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")
$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") {
Write-Host "=========================`n"
[int]$stat = 1
if ($arrService2.Status -eq "Running") {
$stat += 2
NET STOP $ServiceName2 # wampmysqld64
}
if ($arrService1.Status -eq "Running") {
$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")
$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")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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).
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")
$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") {
Write-Host "=========================`n"
[int]$stat = 1
if ($arrService2.Status -eq "Running") {
$stat += 2
NET STOP $ServiceName2 # wampmysqld64
}
if ($arrService1.Status -eq "Running") {
$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")
$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")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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).