Skip to content

Commit

Permalink
Add Read-CouchDBLog cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Jun 26, 2019
1 parent f97adb7 commit b18ade9
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 7 deletions.
6 changes: 4 additions & 2 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@
"Find-CouchDBDocuments",
"Write-CouchDBFullCommit",
"Export-CouchDBDatabase",
"Import-CouchDBDatabase"
"Import-CouchDBDatabase",
"Read-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 @@ -234,7 +235,8 @@
"ecdb",
"exportdb",
"icdb",
"importdb"
"importdb",
"rdblog"
)

# DSC resources to export from this module
Expand Down
128 changes: 128 additions & 0 deletions PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ New-Alias -Name "ecdb" -Value Export-CouchDBDatabase -Option ReadOnly
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

# Native Powershell CouchDB class
class PSCouchDBQuery {
Expand Down Expand Up @@ -5377,3 +5378,130 @@ function Import-CouchDBDatabase () {
$Data = "{ `"docs`" : $(($docs | ConvertFrom-Json) | ConvertTo-Json -Depth 99)}"
Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization -Ssl:$Ssl
}

function Read-CouchDBLog () {
<#
.SYNOPSIS
Read or tail log.
.DESCRIPTION
Read, tail and follow CouchDB log (couch.log).
.PARAMETER Server
The CouchDB server name. Default is localhost.
.PARAMETER Port
The CouchDB server port. Default is 5984.
.PARAMETER Path
The path of log file. Default, is C:\CouchDB\couch.log on Windows and /var/log/couchdb/couch.log on Unix.
.PARAMETER Level
Select level of log. Default is "info".
Available levels:
debug: Detailed debug logging.
info: Informative logging. Includes HTTP requests headlines, startup of an external processes etc.
notice
warning: Warning messages are alerts about edge situations that may lead to errors.
error: Error level includes only things that go wrong, like crash reports and HTTP error responses (5xx codes).
critical
alert
emergency
.PARAMETER Follow
Output appended data as the file grows.
.PARAMETER Tail
The last n lines of log. Default is 10.
.EXAMPLE
Read-CouchDBLog -Level warning -Follow
Append and wait new warning entry on default log.
.EXAMPLE
Read-CouchDBLog -Path /custom/couchdb/log/couch.log -Level error | Out-File error_couchdb.log
Read only error,critical,alert,emergency log in a custom path and redirect to a new file.
.LINK
https://pscouchdb.readthedocs.io/en/latest/server.html#read-the-log
#>
[CmdletBinding()]
param(
[string] $Server,
[int] $Port,
[string] $Path,
[ValidateSet("debug", "info", "notice", "warning", "error", "critical", "alert", "emergency")]
[Parameter(ValueFromPipeline = $true)]
[string] $Level = "info",
[switch] $Follow,
[int] $Tail
)
# 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)) {
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
}
# Set level
$Levels = [PSCustomObject]@{
debug = @{
color = "DarkYellow"
level = "debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"
}
info = @{
color = "DarkGray"
level = "info", "notice", "warning", "error", "critical", "alert", "emergency"
}
notice = @{
color = "Gray"
level = "notice", "warning", "error", "critical", "alert", "emergency"
}
warning = @{
color = "Yellow"
level = "warning", "error", "critical", "alert", "emergency"
}
error = @{
color = "Red"
level = "error", "critical", "alert", "emergency"
}
critical = @{
color = "DarkRed"
level = "critical", "alert", "emergency"
}
alert = @{
color = "DarkMagenta"
level = "alert", "emergency"
}
emergency = @{
color = "Magenta"
level = "emergency"
}
}
# Set option of Get-Content cmdlet
$Parameters = @{
Path = $Path
}
if ($Follow.IsPresent) {
if (-not($Tail)) { $Parameters.Add("Tail", 10) }
$Parameters.Add("Wait", $true)
}
if ($Tail) {
$Parameters.Add("Tail", $Tail)
}
# Run Get-Content
Get-Content @Parameters | ForEach-Object {
foreach ($lev in $Levels.$level.level) {
if ($_ -match "\[$lev\]") { Write-Host -ForegroundColor $Levels.$lev.color $_ }
}
}
} else {
Write-Error -Message "$Path not found!"
}
}
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.
9 changes: 8 additions & 1 deletion docs/build/html/_sources/cmdlets.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ Server
New-CouchDBUuids [[-Server] <String>] [[-Port] <Int32>] [[-Count] <Int32>] [[-Authorization] <String>] [-Ssl] [<CommonParameters>]
**Read-CouchDBLog**

.. code-block:: powershell
Read-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
Replication
***********

Expand Down Expand Up @@ -595,4 +601,5 @@ _______
ecdb -> Export-CouchDBDatabase
exportdb -> Export-CouchDBDatabase
icdb -> Export-CouchDBDatabase
importdb -> Export-CouchDBDatabase
importdb -> Export-CouchDBDatabase
rdblog -> Read-CouchDBLog
69 changes: 69 additions & 0 deletions docs/build/html/_sources/server.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,75 @@ To get one or more Universally Unique Identifiers (UUIDs) from the CouchDB insta
New-CouchDBUuids
Read the log
____________

To read entire log.

.. code-block:: powershell
Read-CouchDBLog
.. note::
The default path on Windows is ``C:\CouchDB\couch.log``, while on Unix it is ``/var/log/couchdb/couch.log``.
Otherwise, if the log is in a custom path, specify the path using the ``-Path`` parameter.

This example is used to read only the last 15 lines of log.

.. code-block:: powershell
Read-CouchDBLog -Tail 15
Instead this to stay in append on the log for the level of warning.

.. code-block:: powershell
Read-CouchDBLog -Level warning -Follow
Level
*****

Each entry in the log has its own color, so as to identify the line of interest "at a glance".

.. role:: goldenrod
.. role:: dimgray
.. role:: gray
.. role:: yellow
.. role:: red
.. role:: darkred
.. role:: darkmagenta
.. role:: magenta

.. raw:: html

<style type="text/css"><!--
.goldenrod {color: goldenrod;}
.dimgray {color: dimgray;}
.gray {color: gray;}
.yellow {color: yellow;}
.red {color: red;}
.darkred {color: darkred;}
.darkmagenta {color: darkmagenta;}
.magenta {color: magenta;}
--></style>
:goldenrod:`debug` : Detailed debug logging.
:dimgray:`info` : Informative logging. Includes HTTP requests headlines, startup of an external processes etc.
:gray:`notice`
:yellow:`warning` : Warning messages are alerts about edge situations that may lead to errors.
:red:`error` : Error level includes only things that go wrong, like crash reports and HTTP error responses (5xx codes).
:darkred:`critical`
:darkmagenta:`alert`
:magenta:`emergency`
Replication
===========
Expand Down
5 changes: 5 additions & 0 deletions docs/build/html/cmdlets.html
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,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">New-CouchDBUuids</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">-Count</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">-Authorization</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">-Ssl</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
<p><strong>Read-CouchDBLog</strong></p>
<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>
</div>
<div class="section" id="replication">
<h3>Replication<a class="headerlink" href="#replication" title="Permalink to this headline"></a></h3>
Expand Down Expand Up @@ -605,6 +609,7 @@ <h2>Aliases<a class="headerlink" href="#aliases" title="Permalink to this headli
<span class="n">exportdb</span> <span class="p">-&gt;</span> <span class="nb">Export-CouchDBDatabase</span>
<span class="n">icdb</span> <span class="p">-&gt;</span> <span class="nb">Export-CouchDBDatabase</span>
<span class="n">importdb</span> <span class="p">-&gt;</span> <span class="nb">Export-CouchDBDatabase</span>
<span class="n">rdblog</span> <span class="p">-&gt;</span> <span class="nb">Read-CouchDBLog</span>
</pre></div>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ <h1>Welcome to PSCouchDB’s documentation!<a class="headerlink" href="#welcome-
<li class="toctree-l2"><a class="reference internal" href="permission.html#reset-admin-password">Reset admin password</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="server.html">Server operation</a></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>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="server.html#replication">Replication</a><ul>
<li class="toctree-l2"><a class="reference internal" href="server.html#get-replica">Get replica</a></li>
<li class="toctree-l2"><a class="reference internal" href="server.html#create-replica">Create replica</a></li>
Expand Down
Loading

0 comments on commit b18ade9

Please sign in to comment.