Skip to content

Instantly share code, notes, and snippets.

@roblogic
Last active April 27, 2017 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblogic/aaec888c6c9d1e100e29092c77dcc6f2 to your computer and use it in GitHub Desktop.
Save roblogic/aaec888c6c9d1e100e29092c77dcc6f2 to your computer and use it in GitHub Desktop.
Extracts XML from application log to a file.
#!/bin/sh
[ $1 ] || { echo "Usage: getxml <redacted-app-logfile>" ; exit 1 ; }
[ -f $1 ] || { ls $1 ; exit 1 ; }
eventlog=$1
# determine input type and output file name
string0="LoggerMessageProcessor - Outbound message before validation: "
string1="LoggerMessageProcessor - CustomerProfileEvent Message: "
baseline_events=`zgrep -m 1 "$string0" $eventlog|wc -l|awk '{print $1}'`
parallel_events=`zgrep -m 1 "$string1" $eventlog|wc -l|awk '{print $1}'`
tag=""
[ $baseline_events -gt 0 ] && { matcher="$string0" ; tag="base" ; }
[ $parallel_events -gt 0 ] && { matcher="$string1" ; tag="test" ; }
[ "$tag" == "" ] && { echo "nothing to extract -- exiting" ; exit 1 ; }
outputfile="$eventlog.$tag.$$.xml"
# namespace string to replace if the test application munges it
# nsa=baseline, nsb=backwards version
nsa='xmlns:ns2="http://types.mycompany.com/BusinessEventMessage/Customer/CustomerProfileEvent/1/0" xmlns:ns3="http://types.mycompany.com/BusinessEventMessage/Common/BEMMetaTypes/1/0"'
nsb='xmlns:ns3="http://types.mycompany.com/BusinessEventMessage/Common/BEMMetaTypes/1/0" xmlns:ns2="http://types.mycompany.com/BusinessEventMessage/Customer/CustomerProfileEvent/1/0"'
# Now extract the XML
# Tweak output slightly to improve matching between Baseline and Test XML
zgrep -e "$matcher" $eventlog | grep "\[INFO\]" \
| awk -F"$matcher" 'NF>1 {print $2}' \
| sed 's?'"${nsb}"'?'"${nsa}"'?' \
| awk -F'ns3:BusinessEventId' '{print $1 FS substr($2,1,20) "....</" FS $3 }' \
| awk -F'ns3:MessageTime' '{print $1 FS substr($2,1,11) "....</" FS $3 }' \
| sort > $outputfile
# EXPLANATION of the above
# grep: only want INFO messages from the log
# awk: get the XML part, after $matcher string
# sed: swaps $nsb back to $nsa, with quoting to handle (/") chars
# awk: truncate BusinessEventId
# awk: truncate MessageTime
# sort: helps XML matching
echo "XML extracted to $outputfile"
@roblogic
Copy link
Author

roblogic commented Apr 27, 2017

The resulting XML files are used to compare baseline vs. test versions of the application, using a simple diff comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment