OH 3 Correct way to filter logs in Karaf Consol

  • Platform information:
    • Hardware: AMD64
    • OS: Windows 10/10.0 (amd64)
    • Java Runtime Environment: 11.0.9.1 (Zulu11.43+55-CA)
    • openHAB version: openHAB 3.1.0 Release Build

Hi All

I have been battling to debug some rules - which I eventually got to work, but really battled with the logging and being able to filter for the events I am looking for in the Karaf Console. Cant be difficult and take plenty of more time when the entries are “lost” in all the other entries.

Example of what I am looking for:

15:08:12.395 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Charging Updated 
15:08:12.395 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Status Updated

Which is generated by the rule:

rule "Battery State to Charging"
when
    Item ShellyUNIshellyunie8db84d6b3e510163199230_VoltageADC changed 
then
{
    var upsBattVoltageNew = newState as Number
    var upsBattVoltagePrevious = previousState as Number  
    if (upsBattVoltageNew >27.70){ 
        if (ShellyUNI_Battery_Charging.state == OFF){
            var strMessage = "UPS Battery has started charging. New Voltage: "
            logDebug("shelly_uni", "UPS Batt Voltage Was: " + upsBattVoltagePrevious.toString())
            logDebug("shelly_uni", "UPS Batt Voltage Now: " + upsBattVoltageNew.toString())
            sendNotification("vangelder.mark@gmail.com", strMessage + upsBattVoltageNew.toString() )
            logDebug("shelly_uni", "Battery Charging Sent! " )
            ShellyUNI_Battery_Charging.postUpdate(ON)
            ShellyUNI_Battery_Idling.postUpdate(OFF)
            ShellyUNI_Battery_Critical.postUpdate(OFF)
            logDebug("shelly_uni", "Battery Charging Set " )
        }
        else {
            //return
            ShellyUNI_Battery_Charging.postUpdate(ON)
            logDebug("shelly_uni", "Battery Charging Updated " )    
        }
    }
}    
end

I have set log:set TRACE org.openhab.core.model.script.shelly_uni

Have tried log:tail |grep shelly_uni which just gives me the full log (no grep). Tried other combinations

Have tried modifying log4j2.xml to redirect the logs to a different file - the file gets created but remains empty.

Changes added are:

		<!-- ShellyUNI appender -->
		<RollingRandomAccessFile append="true" fileName="${sys:openhab.logdir}/shellyuni.log" filePattern="${sys:openhab.logdir}/shellyuni.log.%i" immediateFlush="true" name="SHELLYUNI">
				<PatternLayout>
						<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n</Pattern>
				</PatternLayout>
				<Policies>
						<SizeBasedTriggeringPolicy size="16 MB"/>
				</Policies>
				<DefaultRolloverStrategy max="8"/>
		</RollingRandomAccessFile>
		<!-- ShellyUNI -->	

and

       <Logger additivity="false" level="TRACE" name="org.openhab.core.model.script.shelly_uni">
               <AppenderRef ref="SHELLYUNI"/>

Any suggestions on making the filtering more accessible?

Thanks
Mark

EDIT: Just found this, which I have missed every time I searched before:

So no grep is required. :slight_smile: Would still love advise of redirecting the entries though.

Looks like the output is written directly to the console and ignoring pipes / STDOUT / …
You can try to run log:display | grep shelly_uni which seems to work but it does not continuously update the output.

The karaf console isn’t a full POSIX environment so I can’t comment on how grep and piping works there.

However, all of these are also logged to files, namely openhab.log and events.log. See How to watch and look through logging for how to do it with “real” grep (or PowerShell if you are on Windows).

Hi Guys
Thanks for the responses. Really do appreciate them.
As per my edit the syntax (From here) :

log:tail shelly_uni

Does actually work:

19:48:34.137 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Charging Updated
19:48:34.137 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Status Updated
19:48:52.318 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Status Updated
19:48:52.318 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Charging Updated
19:49:13.455 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Charging Updated
19:49:13.471 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Status Updated
19:49:28.590 [DEBUG] [.openhab.core.model.script.shelly_uni] - Battery Charging Updated

So the grep seems to be “built in”?

However would like to understand why my redirect config does not work?

Thanks again.