npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

log-filter-dsl

v1.0.0

Published

A domain-specific language for filtering log lines

Downloads

9

Readme

Log Filter DSL

A domain-specific language (DSL) for filtering log lines, inspired by "Domain Languages" from The Pragmatic Programmer.

Installation

Install globally to use the log-filter command:

npm install -g log-filter-dsl

Or install locally in your project:

npm install log-filter-dsl

Quick Start

# Filter logs by level
log-filter 'level = ERROR' < app.log

# Filter with complex conditions
log-filter 'service = "auth" and (level = WARN or level = ERROR)' < app.log

Overview

This project implements a simple DSL that allows you to write expressive filters for log lines, similar to SQL WHERE clauses or search queries. The DSL supports comparison operations, text matching, and logical operators.

DSL Syntax

Comparison Operations

  • Equals: field = value

    level = ERROR
    service = "auth"
    count = 42
  • Contains: field contains "text"

    message contains "timeout"
    error contains "database"

Logical Operations

  • AND: expression and expression

    level = ERROR and service = "auth"
  • OR: expression or expression

    level = WARN or level = ERROR
  • NOT: not expression

    not level = DEBUG

Grouping

Use parentheses to group expressions and control precedence:

service = "auth" and (level = WARN or level = ERROR)

Operator Precedence

  1. Parentheses (highest)
  2. NOT
  3. AND
  4. OR (lowest)

Examples

Example 1: Filter by level and message

log-filter 'level = ERROR and message contains "timeout"' < app.log

Example 2: Filter by service and level

log-filter 'service = "auth" and (level = WARN or level = ERROR)' < app.log

Example 3: Exclude debug logs

log-filter 'not level = DEBUG' < app.log

Example 4: Complex filter

log-filter '(service = "api" or service = "auth") and level = ERROR' < app.log

Usage

CLI (Global Installation)

After installing globally with npm install -g log-filter-dsl, use the log-filter command:

log-filter "<filter expression>" < logfile.log

CLI (Local Installation)

If installed locally, use via npx:

npx log-filter-dsl "<filter expression>" < logfile.log

Or add to your package.json scripts:

{
  "scripts": {
    "filter-logs": "log-filter"
  }
}

Then run:

npm run filter-logs 'level = ERROR' < app.log

Supported Log Formats

The tool supports two log formats:

  1. JSON (one JSON object per line):

    {"timestamp":"2024-01-01T12:00:00Z","level":"ERROR","service":"auth","message":"request timeout"}
  2. Key=Value format:

    timestamp=2024-01-01T12:00:00Z level=ERROR service=auth message="request timeout"

Programmatic Usage

import { Lexer } from './lexer';
import { Parser } from './parser';
import { Evaluator } from './evaluator';

const filter = 'level = ERROR and message contains "timeout"';
const lexer = new Lexer(filter);
const tokens = lexer.tokenize();
const parser = new Parser(tokens);
const ast = parser.parse();

const evaluator = new Evaluator();
const record = { level: 'ERROR', message: 'request timeout occurred' };
const matches = evaluator.evaluate(ast, record); // true

Architecture

The DSL is implemented using a classic compiler architecture:

  1. Lexer (src/lexer.ts): Tokenizes the input string into tokens
  2. Parser (src/parser.ts): Builds an Abstract Syntax Tree (AST) from tokens using recursive descent parsing
  3. Evaluator (src/evaluator.ts): Evaluates the AST against log records

Token Types

  • Identifiers (field names)
  • String literals (quoted strings)
  • Number literals
  • Operators: =, contains, and, or, not
  • Grouping: (, )

AST Node Types

  • Binary operations: EQUALS, CONTAINS, AND, OR
  • Unary operations: NOT
  • Field access: FIELD
  • Literals: STRING, NUMBER

Testing

Run tests with:

npm test

Tests cover:

  • Lexer tokenization
  • Parser AST construction
  • Evaluator expression evaluation
  • Edge cases and error handling

Development

# Build TypeScript
npm run build

# Run tests
npm test

# Watch mode for tests
npm run test:watch

Connection to "Domain Languages"

This project demonstrates the concept of Domain Languages from The Pragmatic Programmer by Andy Hunt and Dave Thomas. The book emphasizes:

"When you have a problem, see if you can express the solution in the language of the problem domain."

Instead of writing complex filtering logic in a general-purpose language, we've created a small DSL that lets users express filters in a natural, domain-specific way. This makes the filters:

  • More readable: level = ERROR and message contains "timeout" is clearer than nested if-statements
  • More maintainable: Changes to filter logic don't require code changes
  • More accessible: Non-programmers can write filters
  • More expressive: The syntax matches the problem domain (log filtering)

License

MIT