src/format/nginx.js
/**
 * Compile a Nginx log format into a log parser.
 *
 * Return a function that parses a line of log into an immutable Map.
 */
Compile a Nginx log format into a log parser.
Return a function that parses a line of log into an immutable Map.
Compile a Nginx log format into a log parser . Return a function that parses a line of log into an immutable Map .
function compileLineParser(format) {
  const parts = format.split(/\$([A-Za-z0-9_]+)/);
  const matchString = ([head, ...tail], text) => {
    if (!text.startsWith(head)) {
      throw new Error(
        `Syntax error. Was expecting the string ${head}. Got: ${text}`
      );
    }
    if (tail.length === 0) {
      return Map();
    }
    return matchVariable(tail, text.substring(head.length));
  };
  const matchVariable = ([head, ...tail], text) => {
    const len = text.indexOf(tail[0]);
    if (len === -1) {
      throw new Error(
        `Syntax error. Was expecting the variable ${head}. Got ${text}.`
      );
    }
    const value = text.substring(0, len);
    return matchString(tail, text.substring(len)).set(head, value);
  };
  return line => matchString(parts, line);
}
function compileLineParser ( format ) { const parts = format . split ( / \$([A-Za-z0-9_]+) / ) ; const matchString = ( [ head , ... tail ] , text ) => { if ( ! text . startsWith ( head ) ) { throw new Error ( ` ${ head } ${ text } ` ) ; } if ( tail . length === 0 ) { return Map ( ) ; } return matchVariable ( tail , text . substring ( head . length ) ) ; } ; const matchVariable = ( [ head , ... tail ] , text ) => { const len = text . indexOf ( tail [ 0 ] ) ; if ( len === - 1 ) { throw new Error ( ` ${ head } ${ text } ` ) ; } const value = text . substring ( 0 , len ) ; return matchString ( tail , text . substring ( len ) ) . set ( head , value ) ; } ; return line => matchString ( parts , line ) ; }
Compile a Nginx log format into a log parser.
Compile a Nginx log format into a log parser .