Windows – How to get the log path of Tomcat, using PowerShell

powershelltomcattomcat8windows

I have a number of Windows servers running various versions of Tomcat 8, And I need to be able to acquire the Tomcat log path in a PowerShell script in order to perform administrative functions with this script.

Tomcat allegedly provides a logging API containing this information, but I haven't had any success accessing it with PowerShell (and suspect this is far from the easiest way to get what I want in any event). However, I've noticed that, at least for my Tomcat servers, the log path is a subfolder of the application's install path, such as:

C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs

So, how can I programmatically get this log path from my PowerShell script?

Best Answer

While unsuccessfully searching for a config file or registry setting that might contain the log directory path, I stumbled upon a registry key that contains the installation path of the Tomcat app itself, and combined with the knowledge that the log folder I'm after is in a subfolder of that, named logs, I have enough information for my script to create the logging path.

enter image description here

This registry key is going to be in a predictable location in the registry, and the "InstallPath" name/property is unique within that location, so I can simply do a recursive search and match "InstallPath" to extract the installation folder for Tomcat, and append \logs to get my logging folder.

After adding a basic error handling check, that looks like:

$FoundRegKey = $null
$ApacheRegKeyExists = (Test-Path "HKLM:\Software\Apache Software Foundation")

If ($ApacheRegKeyExists)
{
    Get-ChildItem "HKLM:\Software\Apache Software Foundation" -Recurse -ErrorAction SilentlyContinue | 
    ForEach-Object
    {
        If ($_.Property -match "InstallPath") 
        {$FoundRegKey = Get-ItemProperty $_.pspath | Select InstallPath}
    }
}
Else
{
    Write-Host "Can't find Tomcat software keys in registry, exiting."
    Exit
}

If ($FoundRegKey)
    {
    $logfolder=($FoundRegKey.InstallPath+"\logs")
    }
Else
    {
    Write-Host "Can't find Tomcat install path in registry, exiting."
    Exit
    }