Search code examples
javaxml-parsingxsdmetadatafile-format

file descriptor written in xml


I am looking to parse log files (text files usually delimited by certain characters), and I want to make the job easier by making a parser that can take in an xml file which describes the format of the log file coming in. This way I can feed my parser any type of log file and with my xml description my parser and extract the useful information. I am not sure how to generically describe the log file format in xml. Also I am unsure about how to then match lines in my log file with the descriptions given. I would greatly appreciate any help and guidance on this. Thank you very much to anyone who replies.


Solution

  • <logfile location="/usr/local/logs/logfile.log">
        <delimiter>#</delimiter>
        <format>
            <field index="1" length="10">
                <target>Date</target>
            </field>
            <field index="2">
                <target>Message</target>
            </field>
            <field index="3" length="20">
                <target>Component</target>
            </field>
            <field index="5" length="20">
                <target>Details</target>
            </field>
        </format>
    </logfile>
    

    Your code will read this XML and parse each line in the log file dynamically according to the rules defined here. The program will then store the values in appropriate 'target' fields.

    log file:

    2012/12/12 10.10.10#Critical application state#Error in component XYZ#Useless information#Useful information
    

    Output:

    Date: 2012/12/12
    Message: Critical application state
    Component: Error in component XYZ
    Details: Useful information