blarsen / node-alpine

Alpine - the Apache Log Parser for Node.js
Apache License 2.0
16 stars 6 forks source link

Allow custom error handler on fail of parseLine #3

Open PradatiusD opened 5 years ago

PradatiusD commented 5 years ago

Great package!

I am using this and found that if one log line fails it kills the script.

I thought it would be better if the parseLine method had a catch in case for any reason one log line was poorly formatted and it doesn't affect the whole parsing operation. I think the best case would be to be able to pass a custom error handler when there is a message.

Just a small thought as an enhancement. Thank you so much!

    try {
        this.formatfields.forEach(function(field) {
            buf.skipSpaces();
            var val;
            if (field.isQuoted) {
                if (!(buf.lookingAt() === '"'))
                    throw new Error("Field defined as quoted was not quoted");
                buf.skip();
                val = buf.getUpto('"');
                buf.skip();
            } else if (field.isDate) {
                if (!(buf.lookingAt() === '['))
                    throw new Error("Time field is not enclosed in brackets");
                buf.skip();
                val = buf.getUpto(']');
                buf.skip();
            } else {
                val = buf.getUpto(' ');
            }
            result[field.name] = val;
        })
    } catch (e) {
        result.error = e.message;
    }