ALB Log Parser
alb-log-parser is a utility library for Node.js designed to parse Application Load Balancer (ALB) access logs generated by AWS. It provides a function to convert raw ALB log strings into structured JavaScript objects, mapping all fields defined by AWS, including recent additions like `target_group_arn`, `trace_id`, and `classification`. The current stable version is 0.0.8, with the last publish date being July 11, 2021. As a fork of `elb-log-parser`, its release cadence is infrequent, primarily focused on adapting to changes in AWS ALB log formats and improving parsing robustness, such as correctly handling `-1` as a numeric value. It differentiates itself by offering comprehensive field support specifically for ALB logs, making it suitable for serverless architectures (e.g., AWS Lambda) that process logs from S3 buckets for analysis in systems like Elasticsearch.
Common errors
-
TypeError: parse is not a function
cause Attempting to destructure a default export, e.g., `const { parse } = require('alb-log-parser');` or `import { parse } from 'alb-log-parser';`, when the module exports the function directly as the default export.fixUse `const parse = require('alb-log-parser');` for CommonJS or `import parse from 'alb-log-parser';` for ESM to correctly import the default exported function. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES Module (`.mjs` file or `"type": "module"` in `package.json`) context.fixSwitch to ESM import syntax: `import parse from 'alb-log-parser';` if your project is configured for ES Modules.
Warnings
- gotcha This package is currently in `0.0.x` versioning, indicating a non-stable API. Minor version updates may introduce breaking changes without strictly adhering to semantic versioning guidelines.
- gotcha As a fork of `elb-log-parser`, this package's maintenance and feature development are independent. It may not keep pace with future changes to AWS ALB log formats or new features in the original `elb-log-parser` or other, more actively maintained ALB log parsers in different languages.
- breaking In `v0.0.8`, the parser was updated to interpret fields with the value `"-1"` as a numeric `-1` instead of a string `"-"`. While intended as a correction, this changes the data type and value for these specific fields, potentially affecting existing data processing pipelines or schema expectations.
Install
-
npm install alb-log-parser -
yarn add alb-log-parser -
pnpm add alb-log-parser
Imports
- parse
const { parse } = require('alb-log-parser');const parse = require('alb-log-parser'); - parse
import { parse } from 'alb-log-parser';import parse from 'alb-log-parser';
Quickstart
npm install alb-log-parser
// or npm install -g alb-log-parser for global CLI usage
const parse = require('alb-log-parser');
const albLogLine = 'http 2020-08-27T16:35:00.166351Z app/my-loadbalancer/50dc6c495c0c9188 192.168.131.39:2817 192.168.201.251:80 0.000 0.440 0.000 200 200 1107 11912 "GET http://example.com:80/path?foo=bar&baz=bak HTTP/1.1" "Fake/1.0.0 (Linux)" - - arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-tg/ffffffffffffffff "Self=1-00000000-111111111111111111111111;Root=1-00000000-222222222222222222222222" "-" "-" 0 2020-08-27T16:34:59.725000Z "forward" "-" "-" "192.168.201.251:80" "200" "-" "-"';
const parsedLog = parse(albLogLine);
console.log(JSON.stringify(parsedLog, null, 2));
// Example for accessing specific fields
console.log(`
Client IP: ${parsedLog.client}`)
console.log(`Request Method: ${parsedLog.request_method}`)
console.log(`Target Status Code: ${parsedLog.target_status_code}`)