LogRotate.ps1 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # all logging settins are here on top
  2. $logFile = "log-${env:computername}.log"
  3. $logLevel = "DEBUG" # ("DEBUG","INFO","WARN","ERROR","FATAL")
  4. $logSize = "100b"
  5. $logCount = 3
  6. # end of settings
  7. function Write-Log-Line ($line) {
  8. Add-Content $logFile -Value $Line
  9. Write-Host $Line
  10. }
  11. Function Set-Log {
  12. param([string]$fileName, [int64]$filesize = 1mb , [int] $logcount = 5)
  13. $script:logFile = $fileName
  14. $script:logSize = $filesize
  15. $script:logCount = $logcount
  16. Reset-Log -fileName $fileName -filesize $filesize -logcount $logcount
  17. }
  18. # http://stackoverflow.com/a/38738942
  19. Function Write-Log {
  20. [CmdletBinding()]
  21. Param(
  22. [Parameter(Mandatory=$True)]
  23. [string]
  24. $Message,
  25. [Parameter(Mandatory=$False)]
  26. [String]
  27. $Level = "DEBUG"
  28. )
  29. $levels = ("DEBUG","INFO","WARN","ERROR","FATAL")
  30. $logLevelPos = [array]::IndexOf($levels, $logLevel)
  31. $levelPos = [array]::IndexOf($levels, $Level)
  32. $Stamp = Get-Date -Format "o"
  33. if ($logLevelPos -lt 0){
  34. Write-Log-Line "$Stamp ERROR Wrong logLevel configuration [$logLevel]"
  35. }
  36. if ($levelPos -lt 0){
  37. Write-Log-Line "$Stamp ERROR Wrong log level parameter [$Level]"
  38. }
  39. # if level parameter is wrong or configuration is wrong I still want to see the
  40. # message in log
  41. if ($levelPos -lt $logLevelPos -and $levelPos -ge 0 -and $logLevelPos -ge 0){
  42. return
  43. }
  44. $Line = "$Stamp $Level $Message"
  45. Write-Log-Line $Line
  46. }
  47. # https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-to-Roll-a96ec7d4
  48. function Reset-Log
  49. {
  50. # function checks to see if file in question is larger than the paramater specified
  51. # if it is it will roll a log and delete the oldes log if there are more than x logs.
  52. param([string]$fileName, [int64]$filesize = 1mb , [int] $logcount = 5)
  53. $logRollStatus = $true
  54. if(test-path $filename)
  55. {
  56. $file = Get-ChildItem $filename
  57. if((($file).length) -ige $filesize) #this starts the log roll
  58. {
  59. $fileDir = $file.Directory
  60. #this gets the name of the file we started with
  61. $fn = $file.name
  62. $files = Get-ChildItem $filedir | ?{$_.name -like "$fn*"} | Sort-Object lastwritetime
  63. #this gets the fullname of the file we started with
  64. $filefullname = $file.fullname
  65. #$logcount +=1 #add one to the count as the base file is one more than the count
  66. for ($i = ($files.count); $i -gt 0; $i--)
  67. {
  68. #[int]$fileNumber = ($f).name.Trim($file.name) #gets the current number of
  69. # the file we are on
  70. $files = Get-ChildItem $filedir | ?{$_.name -like "$fn*"} | Sort-Object lastwritetime
  71. $operatingFile = $files | ?{($_.name).trim($fn) -eq $i}
  72. if ($operatingfile)
  73. {$operatingFilenumber = ($files | ?{($_.name).trim($fn) -eq $i}).name.trim($fn)}
  74. else
  75. {$operatingFilenumber = $null}
  76. if(($operatingFilenumber -eq $null) -and ($i -ne 1) -and ($i -lt $logcount))
  77. {
  78. $operatingFilenumber = $i
  79. $newfilename = "$filefullname.$operatingFilenumber"
  80. $operatingFile = $files | ?{($_.name).trim($fn) -eq ($i-1)}
  81. write-host "moving to $newfilename"
  82. move-item ($operatingFile.FullName) -Destination $newfilename -Force
  83. }
  84. elseif($i -ge $logcount)
  85. {
  86. if($operatingFilenumber -eq $null)
  87. {
  88. $operatingFilenumber = $i - 1
  89. $operatingFile = $files | ?{($_.name).trim($fn) -eq $operatingFilenumber}
  90. }
  91. write-host "deleting " ($operatingFile.FullName)
  92. remove-item ($operatingFile.FullName) -Force
  93. }
  94. elseif($i -eq 1)
  95. {
  96. $operatingFilenumber = 1
  97. $newfilename = "$filefullname.$operatingFilenumber"
  98. write-host "moving to $newfilename"
  99. move-item $filefullname -Destination $newfilename -Force
  100. }
  101. else
  102. {
  103. $operatingFilenumber = $i +1
  104. $newfilename = "$filefullname.$operatingFilenumber"
  105. $operatingFile = $files | ?{($_.name).trim($fn) -eq ($i-1)}
  106. write-host "moving to $newfilename"
  107. move-item ($operatingFile.FullName) -Destination $newfilename -Force
  108. }
  109. }
  110. }
  111. else
  112. { $logRollStatus = $false}
  113. }
  114. else
  115. {
  116. $logrollStatus = $false
  117. }
  118. $LogRollStatus
  119. }