# Native logs rotation in Windows with a simple PowerShell script

**URL:** <https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241>\
**Category:** getting started\
**Tags:** log-rotate, useful-tools, windows\
**Created:** [April 27, 2020, 7:12pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241 "2020-04-27T19:12:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 27, 2020, 7:12pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/1 "2020-04-27T19:12:41Z")

</div>

Hello,

Unfortunately, in the current windows version of the Storage Node there is no way to rotate or limit the size of the generated logs files. You cannot delete them from the GUI because they are locked by the services processes. Using **`Clear-Content`** in PowerShell to reset the files works but there is no easy solution if you want to keep a few days / weeks of logs.

I have written a small PowerShell script that can rotate the Storjs Logs without the need of Stop\>Start the services to release the files.

Simply save the code below in a .ps1 file and change the variables to you needs. Then every time you start the script (can be scheduled easily in a task) it will:

- Look for any \*.log files in **`$PathLogLive`**

- Copy all those logs to **`$PathLogArchive`** in a new file with a datetime suffix (you can change the format with **`$DateTimeFormat`** )

- Clear the existing logs

- Delete any \*.log file in the to **`$PathLogArchive`** that is older that **`$RententionInDays`**

It’s been running on my node on a scheduled task for a few days without issues. Hope this helps!

**PowerShell source code:**

```auto
#-----------------------------------------------
#-------------------- INFOS --------------------
#Script: StorjLogsRotation
#Version: 1.0.1.2
#Date: 27/04/2020
#Author: JDA
#------------------- Variables ------------------
$PathLogLive = 'S:\Storj\Storage Node\'
$PathLogArchive = 'S:\Storj\Storage Node\Logs\'
$DateTimeFormat = "yyyy-MM-dd HH'h'mm"
$RententionInDays = 8
#-----------------------------------------------

Function StorjLogsRotation{
 ForEach($L in (Get-ChildItem -LiteralPath $PathLogLive -Filter '*.log' -Recurse:$False)){
  RotateLog -Source $L.FullName -DestinationPath $PathLogArchive -RententionInDays $RententionInDays
 }
}

Function RotateLog([String]$Source, [String]$DestinationPath,[Int]$RententionInDays){
 $Format = '{0}_[{1}]{2}'

 If(Test-Path -LiteralPath $Source){
  $S = Get-Item -LiteralPath $Source
  $Destination = Join-Path -Path $DestinationPath -ChildPath ($Format -F $S.BaseName, ((Get-Date).ToString($DateTimeFormat)), $S.Extension)
 
  If(!(Test-Path -LiteralPath $DestinationPath)){$Null = New-Item -Path $DestinationPath -Type Directory -Force}
  Copy-Item -Path $Source -Destination $Destination -Force
  Clear-Content -LiteralPath $Source -Force

  Get-ChildItem -LiteralPath $DestinationPath -File -Filter ($Format -F $S.BaseName, '*',$S.Extension) | ? LastWriteTime -le ((Get-Date).AddDays(-$RententionInDays)) | Remove-Item -ErrorAction SilentlyContinue
 }
}

StorjLogsRotation

```

**PS:** Depending on your disk speed, every time the logs are rotated there is a small change you loose a few lines of logs.

---

<div class="post-metadata">

**Author:** ![nerdatwork](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/nerdatwork/32/1135_2.png) [@nerdatwork](https://forum.storj.io/u/nerdatwork)\
**Post date:** [April 27, 2020, 7:18pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/2 "2020-04-27T19:18:07Z")

</div>

> [@JDA](#):
>
> Copy all those logs to **`$PathLogArchive`** in a new file with a datetime suffix

If your copy takes too long, wouldn’t the copy contain older values than the current log file. Stopping makes sure no entries are lost.

---

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 27, 2020, 8:04pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/3 "2020-04-27T20:04:48Z")

</div>

> [@nerdatwork](#):
>
> If your copy takes too long, wouldn’t the copy contain older values than the current log file. Stopping makes sure no entries are lost.

Yes, it is not impossible, but stopping / restarting the node daily have disadvantages also. It hurt downtime and the node restart is heavy on disk if you store lots of data (probably some kind of health check going on). Logrotate does something similar, so the risk is the same, but unless your disks are really slow the risk is minimal.

For me node uptime and stability are far superior to a few lines of logs that could be missing, I’m just providing an alternative to something that should be implemented in the program. Log rotation is a basic function that any service should implement by himself (and that way it guaranties you don’t lose anything.)

**PS:** I have updated the post with a warning about it

---

<div class="post-metadata">

**Author:** ![direktorn](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/direktorn/32/69_2.png) [@direktorn](https://forum.storj.io/u/direktorn)\
**Post date:** [April 28, 2020, 12:02am UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/4 "2020-04-28T00:02:25Z")

</div>

> [@JDA](#):
>
> without the need of Stop\>Start the services.

doing that has zero effect on the log file

---

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 28, 2020, 6:49am UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/5 "2020-04-28T06:49:24Z")

</div>

> [@direktorn](#):
>
> doing that has zero effect on the log file

You cannot delete the logs without stopping the Services. You can zero them with a command line, but no from the GUI. I have updated the post to make it clearer about the why you could need this script.

---

<div class="post-metadata">

**Author:** ![BrightSilence](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/brightsilence/32/6048_2.png) [@BrightSilence](https://forum.storj.io/u/BrightSilence)\
**Post date:** [April 28, 2020, 1:06pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/6 "2020-04-28T13:06:07Z")

</div>

> [@JDA](#):
>
> Yes, it is not impossible, but stopping / restarting the node daily have disadvantages also. It hurt downtime and the node restart is heavy on disk if you store lots of data (probably some kind of health check going on)

Furthermore if a significant number of nodes restarts scheduled at midnight, that could hurt file availability. Please never schedule a restart at set moments.

---

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 28, 2020, 2:14pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/7 "2020-04-28T14:14:31Z")

</div>

> [@BrightSilence](#):
>
> Furthermore if a significant number of nodes restarts scheduled at midnight, that could hurt file availability. Please never schedule a restart at set moments.

Amen. 🙂

---

<div class="post-metadata">

**Author:** ![Vadim](https://storj.bcdn.literatehosting.com/letter_avatar/vadim/32/6_449c533f586939b6e5f246632a46421f.png) [@Vadim](https://forum.storj.io/u/Vadim)\
**Post date:** [April 28, 2020, 7:28pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/8 "2020-04-28T19:28:33Z")

</div>

I am doing this by hand, time to time, with my Toolbox.

> **[TonyTosol/Storj-Node-Toolbox](https://github.com/TonyTosol/Storj-Node-Toolbox/releases)**
>
> Contribute to TonyTosol/Storj-Node-Toolbox development by creating an account on GitHub.

done by 2 click, and it not delete it only archive

---

<div class="post-metadata">

**Author:** ![SGC](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/sgc/32/1132_2.png) [@SGC](https://forum.storj.io/u/SGC)\
**Post date:** [April 28, 2020, 7:56pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/9 "2020-04-28T19:56:15Z")

</div>

i take it that this is related to the windows storagenode software that doesn’t use docker?

---

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 28, 2020, 8:00pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/10 "2020-04-28T20:00:20Z")

</div>

Yes only tested on the native service version of Storage Node.  
I was usind docker on windows at first but not real reason to continue now that you have a service.

---

<div class="post-metadata">

**Author:** ![SGC](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/sgc/32/1132_2.png) [@SGC](https://forum.storj.io/u/SGC)\
**Post date:** [April 28, 2020, 8:15pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/11 "2020-04-28T20:15:12Z")

</div>

most likely faster that way anyways…

---

<div class="post-metadata">

**Author:** ![JDA](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/jda/32/1128_2.png) [@JDA](https://forum.storj.io/u/JDA)\
**Post date:** [April 28, 2020, 8:44pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/12 "2020-04-28T20:44:19Z")

</div>

Faster i dont know but it sure take less ressources and it’s way easier to install and maintain. 🙂

---

<div class="post-metadata">

**Author:** ![Vadim](https://storj.bcdn.literatehosting.com/letter_avatar/vadim/32/6_449c533f586939b6e5f246632a46421f.png) [@Vadim](https://forum.storj.io/u/Vadim)\
**Post date:** [April 28, 2020, 8:44pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/13 "2020-04-28T20:44:59Z")

</div>

Yeah it is only usable with GUI version. I not see any advantage to use docker in windows at all, it like emulate windows on windows.

---

<div class="post-metadata">

**Author:** ![SmalltalkJava](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/smalltalkjava/32/1134_2.png) [@SmalltalkJava](https://forum.storj.io/u/SmalltalkJava)\
**Post date:** [November 29, 2020, 3:41pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/14 "2020-11-29T15:41:43Z")

</div>

The script works great.

I did find that to run it as a scheduled task that I had to pass these parameters to powershell on windows. I had to bypass the execution policy for the script.

`-executionpolicy bypass -File C:\directoryName\logCleaner.ps1`

---

<div class="post-metadata">

**Author:** ![Michel](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/michel/32/10854_2.png) [@Michel](https://forum.storj.io/u/Michel)\
**Post date:** [May 19, 2022, 6:52pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/15 "2022-05-19T18:52:34Z")

</div>

> [@JDA](#):
>
> For me node uptime and stability are far superior to a few lines of logs that could be missing, I’m just providing an alternative to something that should be implemented in the program. **Log rotation is a basic function that any service should implement by himself** (and that way it guaranties you don’t lose anything.)

100% agree, thank you for the script  
my logfile got up to 12GB size as i was not aware that storj is missing logrotation.  
sometimes i wonder how they do “all that fancy stuff” without having such basic “no brainers” implemented

---

<div class="post-metadata">

**Author:** ![Alexey](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/alexey/32/41_2.png) [@Alexey](https://forum.storj.io/u/Alexey)\
**Post date:** [May 20, 2022, 5:36am UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/16 "2022-05-20T05:36:52Z")

</div>

Usually log rotation is integrated to the OS. Except Windows. Windows has log rotation for system events though, but not usual text logs.  
So, you need to use some additional tools like [LogRotateWin / Wiki / LogRotate](https://sourceforge.net/p/logrotatewin/wiki/LogRotate/) or such PowerShell scripts.

---

<div class="post-metadata">

**Author:** ![cache22](https://storj.bcdn.literatehosting.com/user_avatar/forum.storj.io/cache22/32/1129_2.png) [@cache22](https://forum.storj.io/u/cache22)\
**Post date:** [May 25, 2023, 9:02pm UTC](https://forum.storj.io/t/native-logs-rotation-in-windows-with-a-simple-powershell-script/6241/18 "2023-05-25T21:02:09Z")

</div>

Thank you! I just got done setting up multiple nodes, and this works great! I already had my nodes configured to keep each node’s log file in the same folder, originally so I could keep an eye on the log file sizes in one place - but I’m glad I set it up that way because after updating the 2 paths in your script, and then adding it to the task scheduler, it works perfectly for all of the log files!
