GithubHelp home page GithubHelp logo

theohbrothers / log-rotate Goto Github PK

View Code? Open in Web Editor NEW
31.0 1.0 5.0 222 KB

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

License: GNU General Public License v3.0

PowerShell 100.00%
powershell module logrotate log-rotation log-management log-administration pwsh log logs

log-rotate's Introduction

Log-Rotate

github-actions github-release powershell-gallery-release

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

Install

Open powershell or pwsh and type:

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

If prompted to trust the repository, hit Y and enter.

Log-Rotate vs logrotate

Log-Rotate is an independent port of logrotate. It's made to work exactly the same way as the original logrotate, 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 a Windows environment where docker is unavailable
  • Anyone who misses that logrotate on *nix
  • Anyone working with Windows and have trouble with managing tons of log files from various applications
  • Anyone who works a lot in Powershell automation, and love the fact you can pipe configs into a module.
  • Anyone who wants to perform a one-time rotation, but doesn't like that logrotate 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 Command
Log-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 Command
Log-Rotate -ConfigAsString $config -State $state -Verbose

Usage as a Scheduled Task or Cron job

Windows Scheduled Task

A main config C:\configs\Log-Rotate\Log-Rotate.conf:

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

Config files in C:\configs\Log-Rotate.d\:

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

Decide on a state file C:\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 file C:\logs\Log-Rotate.log.

Scheduled Task Command line:

# Powershell
powershell -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
# pwsh
pwsh -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 single include 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 a Log-Rotate.status state file is created in the working directory.

Configuration Options

The following discusses how to use certain config options.

Option Examples Explanation
compresscmd C:\Program Files\7-Zip\7z.exe, C:\Program Files\7-Zip\7z, 7z.exe, 7z, gzip Best to use a full path. If using aliases, ensure the binary is among the PATH environment variable
compressoptions a -t7z, May be blank, in which case no parameters are sent along withcompresscmd

Missing options

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

Option Explanation
mail, nomail The mail 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 .
su The main reason for using su is to improve security and reduce chances of accidental renames, moves or deletions. Unlike nix systems, on Windows, SYSTEM and Adminitrator users cannot runas another user without entering their credentials. Unless those credentials are stored in Credential 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 the su option is ever supported in the future because of the first reason, it would only work for *nix platforms. The other reason for using su is to preserve ownership and Access Control Lists (ACLs) on rotated files. This however, can easily be achieved by appying ACLs on rotated files' container folders, so that the any rotated files (E.g. created, moved, renamed) would immediately inherit those attributes.
shred, noshred, shredcycles This option is not supported yet, because of external dependencies on Windows - sdelete.
minage unknown reason.

log-rotate's People

Contributors

alexanderrydberg avatar leojonathanoh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

log-rotate's Issues

Monthly setting doesn't consider changes in year

Using a setting of monthly in the config fails to rotate after the end of the year in which the task is deployed. Once December comes and is processed, the if ($_now_dt.Month -gt $_last_dt.Month) will always return false as the last run that was successful is always 12.

copytruncate failing because file is being used by another process

I have a log file constantly being written to by an application, and have attempted to use copytruncate so that the original log file can continue to be used. I receive this error in the Log-Rotate.log

Truncating C:\utils\stunnel\stunnel.log
Process-Local-Block : Exception calling "RotateMainOnly" with "0" argument(s): "The running command stopped because 
the preference variable "ErrorActionPreference" or common parameter is set to Stop: The process cannot access the file 
'C:\utils\stunnel\stunnel.log' because it is being used by another process."
At C:\Program Files\WindowsPowerShell\Modules\Log-Rotate\1.5.3\public\Log-Rotate.ps1:207 char:13
+             Process-Local-Block -block $block @blockoptions
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], MethodInvocationException
    + FullyQualifiedErrorId : ScriptMethodRuntimeException,Process-Local-Block

My stunnel-log.conf file is:

"C:\utils\stunnel\stunnel.log" {
    rotate 31
    notifempty
    missingok
    dateext
    dateformat -%Y%m%d-%s
    copytruncate
    compress
    compresscmd C:\Program Files\7-Zip\7z.exe
}

"create" directive does not seem to work

Hey all,

is seems that the create directive inside a configuration file does not work.
It should create a new file with the original name after rotation, but that does not seem to work...
Here's my (simplified) config file:

C:\ProgramData\Update-EveryModule\Logs\Update-EveryModule_PowerShellCore.log {
rotate 10
daily
create
}

Am I doing something wrong?
I somebody else using "create" ?

BTW: thanks for the great tool. It is helping me a lot...

Add unit tests

There should be unit tests across all powershell versions including powershell core.

truncate does not work on file opened by another process

The way truncating is implemented does not work when file is open by another process.

VERBOSE: Truncating C:\sauce-connect\logs\sc.log
Process-Local-Block : Exception calling "RotateMainOnly" with "0" argument(s): "Exception calling "WriteAllText" with "2" argument(s
): "Proces nie może uzyskać dostępu do pliku „C:\sauce-connect\logs\sc.log”, ponieważ jest on używany przez inny proces.""
At C:\Program Files\WindowsPowerShell\Modules\Log-Rotate\1.6.1\public\Log-Rotate.ps1:207 char:13
+             Process-Local-Block -block $block @blockoptions
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], MethodInvocationException
    + FullyQualifiedErrorId : ScriptMethodRuntimeException,Process-Local-Block

createolddir

whenever olddir is specified and not existing, i get this error:

Process-Local-Block : Skipping over processing log C:\logs\log.log. At C:\Program Files\WindowsPowerShell\Modules\Log-Rotate\public\Log-Rotate.ps1:207 char:13 Process-Local-Block -block $block @blockoptions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Process-Local-Block

createolddir does nothing, adding firstaction..mkdir..endscript doesnt work because firstaction seems to be executed after that error message.
Can someone please shed some light on whats happening here and how I can create olddir if its not existing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.