I’ve been seeing a lot of posts lately about people wanting to eliminate certain events from their events.log. This posting is to show that doing so is not only a bad idea but unnecessary.
First, I’ll apologize to the Windows users out there. I don’t know the right commands to do this on Windows without installing MinGW, Cygwin, or Linux Subsystem for Windows. Below I include both Posix (will work for Linux, iOS, BSD, and those addons to Windows listed to the left) and the Windows Powershell equivalents. Note though that the Powershell Get-Content is really slow. You may want to install MinGW or the like anyway.
Anyway, we will be using tail -f
to follow the log file as it is being written. Note that this may not work on log files over a SAMBA share.
Finally, these are naive examples. There are ways to construct equivalent command lines that will run more efficiently and require fewer characters to write. I opted for less efficient and longer command lines in order to utilize fewer new commands or concepts.
In the below examples, I will use an environment variable to represent the log file path. You can replace the variable with the actual path to the file or set an environment variable. This is an exercise left to the student.
Posix
$OH_EVENTS for an apt-get installed OH /var/log/openhab2/events.log
$OH_LOG for an apt-get installed OH /var/log/openhab2/openhab.log
Windows
$Env:OH_EVENTS c:\openhab2\userdata\logs\events.log
$Env:OH_LOG c:\openhab2\userdata\logs\openhab.log
Only shows log entries that contain MyItem
Posix
tail -F $OH_EVENTS | grep MyItem
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -match "MyItem" }
Only shows log entries that do not contain MyItem
Posix
tail -F $OH_EVENTS | grep -v MyItem
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -notmatch "MyItem" }
Shows those log entries that contain MyItem1 OR MyItem2
Posix
tail -F $OH_EVENTS | grep -E "MyItem1|MyItem2"`
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -match "MyItem1|MyItem2" }
Shows only log entries that contain MyItem1 and strips off the date time and class fields.
Posix
tail -F $OH_EVENTS | grep MyItem1 | cut -d '-' -f 4
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -match "MyItem" } | ForEach-Object { $_.split("-")[3] }
Shows only log entries showing where MyItem1 changed state
Posix
tail -F $OH_EVENTS | grep MyItem1 | grep changed
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -match "MyItem" } | where { $_ -match "changed" }
Shows only log entries from logName (e.g. the first argument to a logInfo in your Rules)
Posix
tail -F $OH_LOG | grep logName
Windows
Get-Content $Env:OH_LOG - Wait | where { $_ -match "logName" }
Shows only those log entries where MyItem1 received an ON command
Posix
tail -F $OH_EVENTS | grep MyItem1 | grep "received command ON"
Windows
Get-Content $Env:OH_EVENTS -Wait | where { $_ -match "MyItem1" } | where { $_ -match "received command ON" }
I could go on with more examples but I hope these will be enough for you to manipulate your log file viewing to filter them down to just view what you care about at any given moment.
Edit 1: Removed wrong OR example, changed the tail to use -F so it follows the logs when they happen to become rotated
Edit 2: Added back a working OR example that will work with regular grep.
Edit 3: Added Windows Powershell equivalent commands