logpare

compressText()

Compress a multiline string of log data — a thin wrapper over compress() that splits on newlines.

Compress a multiline string of log data. This is a convenience wrapper around compress() that splits the text on newlines.

Signature

function compressText(
  text: string,
  options?: CompressOptions
): CompressionResult

Parameters

text

  • Type: string
  • Required: Yes

Multiline string containing log data. Split on /\r?\n/, so CRLF logs work unchanged.

Blank and whitespace-only lines are skipped and are not counted in stats.inputLines, so a trailing newline does not change the reported figures.

options

  • Type: CompressOptions
  • Required: No

Same options as compress().

Return Value

Returns a CompressionResult object, same as compress().

Examples

Basic Usage

import { compressText } from 'logpare';

const logs = `
ERROR Connection failed
ERROR Connection timeout
INFO Request completed
INFO Request completed
`;

const result = compressText(logs);

console.log(result.formatted);
// === Log Compression Summary ===
// Input: 4 lines → 3 templates (25.0% reduction)
//
// Top templates by frequency:
// 1. [2x] INFO Request completed
// 2. [1x] ERROR Connection failed
// 3. [1x] ERROR Connection timeout

Reading from File

import { readFileSync, writeFileSync } from 'node:fs';
import { compressText } from 'logpare';

const logFile = readFileSync('app.log', 'utf-8');
const result = compressText(logFile, {
  format: 'detailed',
  maxTemplates: 20,
});

console.log(result.formatted);

// Write compressed output
writeFileSync('compressed.txt', result.formatted);

Whole-File Read with Tuning

compressText() takes a complete string, so the file is read in full first. For genuine streaming — feeding lines in as they arrive — use createDrain() instead.

import { compressText } from 'logpare';
import { readFileSync } from 'node:fs';

// Read entire file
const content = readFileSync('/var/log/syslog', 'utf-8');

// Compress
const result = compressText(content, {
  format: 'json',
  drain: {
    depth: 5,
    simThreshold: 0.4,
  },
});

// Output as JSON
console.log(JSON.stringify(result, null, 2));

With Progress Tracking

const result = compressText(largeLogFile, {
  drain: {
    onProgress: (event) => {
      const percent = event.percentComplete ?? 0;
      process.stdout.write(`\rProcessing: ${percent.toFixed(1)}%`);
    },
  },
});

console.log('\nDone!');

Processing Template Results

const result = compressText(logs);

// Group by severity
const bySeverity = {
  error: result.templates.filter(t => t.severity === 'error'),
  warning: result.templates.filter(t => t.severity === 'warning'),
  info: result.templates.filter(t => t.severity === 'info'),
};

console.log(`Errors: ${bySeverity.error.length} templates`);
console.log(`Warnings: ${bySeverity.warning.length} templates`);
console.log(`Info: ${bySeverity.info.length} templates`);

Implementation Details

compressText() is equivalent to:

function compressText(text: string, options?: CompressOptions) {
  const lines = text.split(/\r?\n/);
  return compress(lines, options);
}

Blank-line filtering happens further down, inside the Drain instance, rather than here.

When to Use

Use compressText() when:

  • Reading log files directly as strings
  • Processing multiline log data from APIs or databases
  • Working with concatenated log output

Use compress() when:

  • You already have an array of lines
  • You need fine-grained control over line filtering
  • Processing streaming data incrementally

See Also