Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A replica of the logrotate utility, except this also runs on Windows systems.

License

NotificationsYou must be signed in to change notification settings

theohbrothers/Log-Rotate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github-actionsgithub-releasepowershell-gallery-release

A replica of thelogrotate utility, except this also runs on Windows systems.

Install

Openpowershell orpwsh and type:

Install-Module-Name Log-Rotate-Repository PSGallery-Scope CurrentUser-Verbose

If prompted to trust the repository, hitY andenter.

Log-Rotate vslogrotate

Log-Rotate is an independent port oflogrotate. It's made to work exactly the same way as the originallogrotate, except it works in Powershell and especially Windows.

  • Same command line
  • Same config file format, meaning you can re-use your*nix configs
  • Same rotation logic
  • Runs on Powershell or Powershell core.

Who should use it?

  • Anyone with aWindows environment wheredocker is unavailable
  • Anyone who misses thatlogrotate on*nix
  • Anyone working withWindows and have trouble with managing tons of log files from various applications
  • Anyone who works a lot inPowershell automation, and love the fact you can pipe configs into a module.
  • Anyone who wants to perform aone-time rotation, but doesn't like thatlogrotate only accepts configs as a file and not just a string.

Usage

Windows

Import-Module Log-Rotate# Define your config# Double-quotes necessary only if there are spaces in the path$config=@'"C:\inetpub\logs\access.log" {    rotate 365    size 10M    postrotate        # My shell is powershell        Write-Host "Rotated $( $Args[1] )"    endscript}'@# Decide on a Log-Rotate state file that will be created by Log-Rotate$state='C:\var\Log-Rotate\Log-Rotate.status'# To check rotation logic without rotating files, use the -WhatIf switch (implies -Verbose)$config| Log-Rotate-State$state-WhatIf# You can either Pipe the config$config| Log-Rotate-State$state-Verbose# Or use the full CommandLog-Rotate-ConfigAsString$config-State$state-Verbose

*nix

Import-Module Log-Rotate# Define your config# Double-quotes necessary only if there are spaces in the path$config=@'"/var/log/httpd/access.log" {    rotate 365    size 10M    postrotate        # My shell is sh        /usr/bin/killall -HUP httpd        echo "Rotated ${1}"    endscript}'@# Decide on a Log-Rotate state file that will be created by Log-Rotate$state='/var/lib/Log-Rotate/Log-Rotate.status'# To check rotation logic without rotating files, use the -WhatIf switch (implies -Verbose)$config| Log-Rotate-State$state-WhatIf# You can either Pipe the config$config| Log-Rotate-State$state-Verbose# Or use the full CommandLog-Rotate-ConfigAsString$config-State$state-Verbose

Usage as a Scheduled Task or Cron job

Windows Scheduled Task

A main configC:\configs\Log-Rotate\Log-Rotate.conf:

include C:\configs\Log-Rotate.d\

Config files inC:\configs\Log-Rotate.d\:

C:\configs\logrotate.d\+-- iis.conf+-- apache.conf+-- minecraftserver.conf

Decide on a state fileC:\var\Log-Rotate\Log-Rotate.status.

Run the command with-WhatIf to simulate the rotation, making sure everything is working.

Import-Module Log-Rotate; Log-Rotate-Config C:\configs\Log-Rotate\Log-Rotate.conf-State C:\var\Log-Rotate\Log-Rotate.status-Verbose-WhatIf

Decide on a log fileC:\logs\Log-Rotate.log.

Scheduled Task Command line:

# Powershellpowershell-Command'Import-Module Log-Rotate; Log-Rotate -Config C:\configs\Log-Rotate\Log-Rotate.conf -State C:\var\Log-Rotate\Log-Rotate.status -Verbose'>> C:\logs\Log-Rotate.log# pwshpwsh-Command'Import-Module Log-Rotate; Log-Rotate -Config C:\configs\Log-Rotate\Log-Rotate.conf -State C:\var\Log-Rotate\Log-Rotate.status -Verbose'>> C:\logs\Log-Rotate.log

*nix cron

A Main config/etc/Log-Rotate.conf, with a singleinclude line :

include /etc/Log-Rotate.d/

Config files in/etc/Log-Rotate.d/:

/etc/Log-Rotate.d/+-- nginx.conf+-- apache.conf+-- syslog.conf

Decide on a state file/var/lib/Log-Rotate/Log-Rotate.status.

Run the command with-WhatIf to simulate the rotation, making sure everything is working.

pwsh-Command'Import-Module Log-Rotate; Log-Rotate -Config /etc/Log-Rotate.conf -State /var/lib/Log-Rotate/Log-Rotate.status -Verbose -WhatIf'

Decide on a log file/var/log/Log-Rotate.log.

Cron command line:

pwsh-Command'Import-Module Log-Rotate; Log-Rotate -Config /etc/Log-Rotate.conf -State /var/lib/Log-Rotate/Log-Rotate.status -Verbose'>>/var/log/Log-Rotate.log

Configuration

State

If-State is unspecified, by default aLog-Rotate.status state file is created in the working directory.

Configuration Options

The following discusses how to use certain config options.

OptionExamplesExplanation
compresscmdC:\Program Files\7-Zip\7z.exe,C:\Program Files\7-Zip\7z,7z.exe,7z,gzipBest to use afull path. If using aliases, ensure the binary is among thePATH environment variable
compressoptionsa -t7z,May be blank, in which case no parameters are sent along withcompresscmd

Missing options

A few less crucial options are left out forLog-Rotate v1. The option and their reasons are stated below:

OptionExplanation
mail,nomailThemail option isn't used very much, because the same can be achieved with greater flexibility by adding scripts to any of the following options:firstaction,lastaction,prerotate,postrotate,preremove .
suThe main reason for usingsu is to improve security and reduce chances of accidental renames, moves or deletions. Unlikenix systems, on Windows, SYSTEM and Adminitrator users cannotrunas another user without entering their credentials. Unless those credentials are stored inCredential Manager, it is impossible for a high privileged daemon to perform rotation operations (E.g. creating, moving, copying, deleting) via an external shell. In the case that thesu option is ever supported in the future because of the first reason, it wouldonly work for*nix platforms. The other reason for usingsu is to preserveownership andAccess Control Lists (ACLs) on rotated files. This however, can easily be achieved by appyingACLs onrotated files' container folders, so that the any rotated files (E.g. created, moved, renamed) would immediately inherit those attributes.
shred,noshred,shredcyclesThis option is not supported yet, because of external dependencies on Windows -sdelete.
minageunknown reason.

[8]ページ先頭

©2009-2025 Movatter.jp