PHPackages                             benmorel/apache-log-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. benmorel/apache-log-parser

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

benmorel/apache-log-parser
==========================

PHP library to parse Apache log files

0.2.0(5y ago)266.6k↓40.8%6MITPHPPHP ^7.2 || ^8.0CI passing

Since May 30Pushed 1y ago2 watchersCompare

[ Source](https://github.com/BenMorel/apache-log-parser)[ Packagist](https://packagist.org/packages/benmorel/apache-log-parser)[ RSS](/packages/benmorel-apache-log-parser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Apache Log Parser
=================

[](#apache-log-parser)

A PHP library to parse Apache logs.

[![Build Status](https://github.com/BenMorel/apache-log-parser/workflows/CI/badge.svg)](https://github.com/BenMorel/apache-log-parser/actions)[![Coverage Status](https://camo.githubusercontent.com/4f287a4e96695670875f5aa0971e283b6955d498aee58341429df012151bcafd/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f42656e4d6f72656c2f6170616368652d6c6f672d7061727365722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/BenMorel/apache-log-parser?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/364a1554ff633d6eee59ac689431a7e94e137939eb1f3afd26bad26c37154e27/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f6170616368652d6c6f672d7061727365722f762f737461626c65)](https://packagist.org/packages/benmorel/apache-log-parser)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

Installation
------------

[](#installation)

This library is installable via [Composer](https://getcomposer.org/). Just run:

```
composer require benmorel/apache-log-parser
```

Requirements
------------

[](#requirements)

This library requires PHP 7.1 or later.

Project status &amp; release process
------------------------------------

[](#project-status--release-process)

This library is under development.

The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `0.1.*`.

If you need to upgrade to a newer release cycle, check the [release history](https://github.com/BenMorel/apache-log-parser/releases)for a list of changes introduced by each further `0.x.0` version.

Package contents
----------------

[](#package-contents)

This library provides a single class, `Parser`.

Quick start
-----------

[](#quick-start)

First construct a `Parser` object with the `LogFormat` defined in the httpd.conf file of the server that generated the log file:

```
use BenMorel\ApacheLogParser\Parser;

$logFormat = "%h %l %u %t \"%{Host}i\" \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"";
$parser = new Parser($logFormat);
```

The library converts every [format string](https://httpd.apache.org/docs/current/en/mod/mod_log_config.html#formats) of your log format to a field name; the list of fields can be accessed through the `getFieldNames()` method:

```
var_export(
    $parser->getFieldNames()
);
```

```
array (
  0 => 'remoteHostname',
  1 => 'remoteLogname',
  2 => 'remoteUser',
  3 => 'time',
  4 => 'requestHeader:Host',
  5 => 'firstRequestLine',
  6 => 'status',
  7 => 'responseSize',
  8 => 'requestHeader:Referer',
  9 => 'requestHeader:User-Agent',
)
```

You're then ready to parse a single line of your log file: the `parse()` method accepts the log line, and a boolean to indicate whether you want the results as a numeric array, whose keys match the ones of the field names array:

```
$line = '1.2.3.4 - - [30/May/2018:15:00:23 +0200] "www.example.com" "GET / HTTP/1.0" 200 1234 "-" "Mozilla/5.0';

var_export(
    $parser->parse($line, false)
);
```

```
array (
  0 => '1.2.3.4',
  1 => '-',
  2 => '-',
  3 => '30/May/2018:15:00:23 +0200',
  4 => 'www.example.com',
  5 => 'GET / HTTP/1.0',
  6 => '200',
  7 => '1234',
  8 => '-',
  9 => 'Mozilla/5.0',
)
```

Or as an associative array, with the field names as keys:

```
var_export(
    $parser->parse($line, true)
);
```

```
array (
  'remoteHostname'           => '1.2.3.4',
  'remoteLogname'            => '-',
  'remoteUser'               => '-',
  'time'                     => '30/May/2018:15:00:23 +0200',
  'requestHeader:Host'       => 'www.example.com',
  'firstRequestLine'         => 'GET / HTTP/1.0',
  'status'                   => '200',
  'responseSize'             => '1234',
  'requestHeader:Referer'    => '-',
  'requestHeader:User-Agent' => 'Mozilla/5.0',
)
```

If a line cannot be parsed, an `InvalidArgumentException` is thrown. Be sure to wrap your `parse()` calls in a try-catch block:

```
try {
    $parser->parse($line, true)
} catch (\InvalidArgumentException $e) {
    // ...
}
```

Field names returned by the library
-----------------------------------

[](#field-names-returned-by-the-library)

This table shows how [format strings](https://httpd.apache.org/docs/current/en/mod/mod_log_config.html#formats) are mapped to field names by the library:

Format stringField name`%a`clientIp`%{c}a`clientIp:c`%A`localIp`%B`responseSize`%b`responseSize`%{VARNAME}C`cookie:VARNAME`%D`responseTime`%{VARNAME}e`env:VARNAME`%f`filename`%h`remoteHostname`%H`requestProtocol`%{VARNAME}i`requestHeader:VARNAME`%k`keepaliveRequests`%l`remoteLogname`%L`requestLogId`%m`requestMethod`%{VARNAME}n`note:VARNAME`%{VARNAME}o`responseHeader:VARNAME`%p`canonicalPort`%{FORMAT}p`canonicalPort:FORMAT`%P`processId`%{FORMAT}P`processId:FORMAT`%q`queryString`%r`firstRequestLine`%R`handler`%s`status`%t`time`%{FORMAT}t`time:FORMAT`%T`timeToServe`%{UNIT}T`timeToServe:UNIT`%u`remoteUser`%U`urlPath`%v`serverName`%V`serverName`%X`connectionStatus`%I`bytesReceived`%O`bytesSent`%S`bytesTransferred`%{VARNAME}^ti`requestTrailerLine:VARNAME`%{VARNAME}^to`responseTrailerLine:VARNAMEIf two or more format strings yield the same field name, the second one will get a `:2` suffix, the third one a `:3` suffix, etc.

Performance notes
-----------------

[](#performance-notes)

You can expect to parse more than 250,000 records per second (&gt; 50 MiB/s) when reading logs from a file on a modern server with an SSD drive.

Returning records as an associative array comes with a small performance penalty of about 6%.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~916 days

Total

2

Last Release

1993d ago

PHP version history (2 changes)0.1.0PHP &gt;=7.1

0.2.0PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (29 commits)")

---

Tags

logparserparseapache

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/benmorel-apache-log-parser/health.svg)

```
[![Health](https://phpackages.com/badges/benmorel-apache-log-parser/health.svg)](https://phpackages.com/packages/benmorel-apache-log-parser)
```

###  Alternatives

[monolog/monolog

Sends your logs to files, sockets, inboxes, databases and various web services

21.4k964.9M7.0k](/packages/monolog-monolog)[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[symfony/monolog-bundle

Symfony MonologBundle

2.9k249.1M1.6k](/packages/symfony-monolog-bundle)[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k45.4M309](/packages/spatie-laravel-activitylog)[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k227.1M273](/packages/sentry-sentry)[sentry/sentry-laravel

Laravel SDK for Sentry (https://sentry.io)

1.3k114.3M154](/packages/sentry-sentry-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
