Skip to content

Commit

Permalink
Add Clear-CouchDBLog cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Jul 8, 2019
1 parent b18ade9 commit 88759a7
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 4 deletions.
6 changes: 4 additions & 2 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@
"Write-CouchDBFullCommit",
"Export-CouchDBDatabase",
"Import-CouchDBDatabase",
"Read-CouchDBLog"
"Read-CouchDBLog",
"Clear-CouchDBLog"
)

# Cmdlet s to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down Expand Up @@ -236,7 +237,8 @@
"exportdb",
"icdb",
"importdb",
"rdblog"
"rdblog",
"cdblog"
)

# DSC resources to export from this module
Expand Down
61 changes: 60 additions & 1 deletion PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ New-Alias -Name "exportdb" -Value Export-CouchDBDatabase -Option ReadOnly
New-Alias -Name "icdb" -Value Import-CouchDBDatabase -Option ReadOnly
New-Alias -Name "importdb" -Value Import-CouchDBDatabase -Option ReadOnly
New-Alias -Name "rdblog" -Value Read-CouchDBLog -Option ReadOnly
New-Alias -Name "cdblog" -Value Clear-CouchDBLog -Option ReadOnly

# Native Powershell CouchDB class
class PSCouchDBQuery {
Expand Down Expand Up @@ -5440,7 +5441,7 @@ function Read-CouchDBLog () {
$root = "/var/log"
}
# ...and if not exists, search it
if (-not(Test-Path -Path $Path)) {
if (-not(Test-Path -Path $Path -ErrorAction SilentlyContinue)) {
Write-Warning -Message "Default log path $Path not exists!"
Write-Host "Search couch.log in the $root path..."
$Path = (Get-ChildItem -Path $root -Recurse | Where-Object {$_.Name -like "couch.log"} | Select-Object FullName).FullName
Expand Down Expand Up @@ -5505,3 +5506,61 @@ function Read-CouchDBLog () {
Write-Error -Message "$Path not found!"
}
}

function Clear-CouchDBLog () {
<#
.SYNOPSIS
Clear log.
.DESCRIPTION
Clear and rotate CouchDB log (couch.log).
.PARAMETER Path
The path of log file. Default, is C:\CouchDB\couch.log on Windows and /var/log/couchdb/couch.log on Unix.
.PARAMETER Rotate
Rotate an existing log. Copy before delete in this format couch.log.t-i-m-e_s_t_a_m_p
.EXAMPLE
Clear-CouchDBLog -Rotate
Clear default couch.log and save a copy before delete it.
.EXAMPLE
Clear-CouchDBLog -Path /custom/couchdb/log/couch.log
Clear custom log /custom/couchdb/log/couch.log.
.LINK
https://pscouchdb.readthedocs.io/en/latest/server.html#clear-the-log
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true)]
[string] $Path,
[switch] $Rotate
)
# Check if $Path is empty
if (-not($Path)) {
# Set default path...
$Windows = ([bool](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue))
# Windows platform
if ($Windows) {
$Path = "C:\CouchDB\couch.log"
$root = "C:\CouchDB"
# Unix platform
} else {
$Path = "/var/log/couchdb/couch.log"
$root = "/var/log"
}
# ...and if not exists, search it
if (-not(Test-Path -Path $Path -ErrorAction SilentlyContinue)) {
Write-Warning -Message "Default log path $Path not exists!"
Write-Host "Search couch.log in the $root path..."
$Path = (Get-ChildItem -Path $root -Recurse | Where-Object {$_.Name -like "couch.log"} | Select-Object FullName).FullName
if (-not(Test-Path -Path $Path)) {
throw "couch.log not found! Specify the `$Path parameter or check configuration!"
}
Write-Host
}
# Clear log
if ($Rotate.IsPresent) {
Copy-Item -Path $Path -Destination "$Path.$(Get-Date -Format 'MM-dd-yyyy_HH_mm_ss')" -Force
Clear-Content -Path $Path -Force
} else {
Clear-Content -Path $Path -Force
}
}
}
Binary file modified docs/build/doctrees/cmdlets.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/server.doctree
Binary file not shown.
6 changes: 6 additions & 0 deletions docs/build/html/_sources/cmdlets.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ Server
Read-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
**Clear-CouchDBLog**

.. code-block:: powershell
Clear-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [-Rotate] [<CommonParameters>]
Replication
***********

Expand Down
9 changes: 9 additions & 0 deletions docs/build/html/_sources/server.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ Each entry in the log has its own color, so as to identify the line of interest
:magenta:`emergency`
Clear the log
_____________
Clear entire and rotate (save a copy in the same folder) log.
.. code-block:: powershell
Clear-CouchDBLog -Rotate
Replication
===========
Expand Down
4 changes: 4 additions & 0 deletions docs/build/html/cmdlets.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ <h3>Server<a class="headerlink" href="#server" title="Permalink to this headline
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Read-CouchDBLog</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Level</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Follow</span><span class="p">]</span> <span class="p">[[</span><span class="n">-Tail</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
<p><strong>Clear-CouchDBLog</strong></p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Clear-CouchDBLog</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Rotate</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
</div>
<div class="section" id="replication">
<h3>Replication<a class="headerlink" href="#replication" title="Permalink to this headline"></a></h3>
Expand Down
1 change: 1 addition & 0 deletions docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ <h1>Welcome to PSCouchDB’s documentation!<a class="headerlink" href="#welcome-
</li>
<li class="toctree-l1"><a class="reference internal" href="server.html">Server operation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="server.html#read-the-log">Read the log</a></li>
<li class="toctree-l2"><a class="reference internal" href="server.html#clear-the-log">Clear the log</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="server.html#replication">Replication</a><ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/searchindex.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions docs/build/html/server.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<li class="toctree-l3"><a class="reference internal" href="#level">Level</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#clear-the-log">Clear the log</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="#replication">Replication</a><ul>
Expand Down Expand Up @@ -266,6 +267,13 @@ <h3>Level<a class="headerlink" href="#level" title="Permalink to this headline">
<p><span class="magenta">emergency</span></p>
</div>
</div>
<div class="section" id="clear-the-log">
<h2>Clear the log<a class="headerlink" href="#clear-the-log" title="Permalink to this headline"></a></h2>
<p>Clear entire and rotate (save a copy in the same folder) log.</p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Clear-CouchDBLog</span> <span class="n">-Rotate</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="replication">
<h1>Replication<a class="headerlink" href="#replication" title="Permalink to this headline"></a></h1>
Expand Down
6 changes: 6 additions & 0 deletions docs/source/cmdlets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ Server
Read-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
**Clear-CouchDBLog**

.. code-block:: powershell
Clear-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [-Rotate] [<CommonParameters>]
Replication
***********

Expand Down
9 changes: 9 additions & 0 deletions docs/source/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ Each entry in the log has its own color, so as to identify the line of interest
:magenta:`emergency`
Clear the log
_____________
Clear entire and rotate (save a copy in the same folder) log.
.. code-block:: powershell
Clear-CouchDBLog -Rotate
Replication
===========
Expand Down

0 comments on commit 88759a7

Please sign in to comment.