PHPackages                             borneo/logger - 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. borneo/logger

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

borneo/logger
=============

A lightweight PHP SDK for sending structured event logs to any HTTP ingest endpoint.

04PHP

Since Jun 11Pushed 1mo agoCompare

[ Source](https://github.com/EgiGrivandani/borneo-logger)[ Packagist](https://packagist.org/packages/borneo/logger)[ RSS](/packages/borneo-logger/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Borneo Logger
=============

[](#borneo-logger)

A lightweight PHP SDK for sending structured event logs to any HTTP ingest endpoint — ideal for audit trails, user activity tracking, and application monitoring.

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

[](#installation)

```
composer require borneo/logger
```

> Package belum stable release — install dengan:
>
> ```
> composer require borneo/logger:@dev
> ```

---

Dispatch Modes
--------------

[](#dispatch-modes)

BorneoLogger supports two modes, **auto-detected** based on configuration:

ModeHowLatencyRecommended**Mode 1 — File**Writes JSON to a local log file. A log shipper (e.g. [Vector](https://vector.dev)) tails and ships it.~0.01ms✅ Production**Mode 2 — HTTP**Fire-and-forget HTTP POST directly to the ingest endpoint.~0.5ms (blocking)Fallback / simple setups### Why Mode 1 for Production?

[](#why-mode-1-for-production)

Mode 2 uses `file_get_contents()` which is **synchronous and blocking** in PHP — even with a short timeout. If the monitoring server is down for maintenance, every log call will block until timeout:

```
User request → login → BorneoLogger::loginSuccess()
                           ↓
                       [waiting 0.5s for HTTP timeout...]
                           ↓
                       Response finally sent ← user feels delay

```

Mode 1 writes to a local file (~0.01ms) and does not care about network or monitoring server availability. A **Vector Agent** running on the same server reads the file in real-time and ships it.

```
App writes to file (~0.01ms) → Response sent immediately
         ↓ (async, decoupled)
Vector Agent reads file → forwards to monitoring server

```

---

Configuration
-------------

[](#configuration)

Call `configure()` once in your application bootstrap or config file:

```
use Borneo\BorneoLogger;

// Mode 1 — File (recommended for production)
BorneoLogger::configure(
    endpoint: 'https://your-ingest-endpoint.com/logs',
    apiKey:   'your-api-key',
    service:  'your-service-name',
    logFile:  '/var/log/borneo/your-service.log'
);

// Mode 2 — HTTP fallback (no logFile needed)
BorneoLogger::configure(
    endpoint: 'https://your-ingest-endpoint.com/logs',
    apiKey:   'your-api-key',
    service:  'your-service-name'
);
```

### Using Environment Variables (Recommended)

[](#using-environment-variables-recommended)

```
BorneoLogger::configure(
    endpoint: getenv('LOGGER_ENDPOINT') ?: '',
    apiKey:   getenv('LOGGER_API_KEY')  ?: '',
    service:  getenv('LOGGER_SERVICE')  ?: 'my-app',
    logFile:  getenv('LOGGER_LOG_FILE') ?: '', // empty = use HTTP mode
);
```

`.env` example:

```
# Borneo Logger
LOGGER_ENDPOINT=https://log.yourdomain.com/logs
LOGGER_API_KEY=your-vector-api-key
LOGGER_SERVICE=my-service-name
# Production: /var/log/borneo/my-service.log | Local dev: leave empty
LOGGER_LOG_FILE=
```

---

Usage
-----

[](#usage)

### Auth Events

[](#auth-events)

```
// Successful login
BorneoLogger::loginSuccess($userId);

// Failed login (great for brute-force detection)
BorneoLogger::loginFailed('wrong_password');
BorneoLogger::loginFailed('account_banned', ['metadata' => ['email' => $email]]);
BorneoLogger::loginFailed('invalid_otp', ['user_id' => $userId]);

// Logout
BorneoLogger::logout($userId);
```

### HTTP Request Logging

[](#http-request-logging)

Best placed in API Gateway or middleware, **after** the response is sent:

```
BorneoLogger::httpRequest(
    method:     'POST',
    path:       '/api/checkout',
    statusCode: 200,
    latencyMs:  143,
    userId:     $userId,
    traceId:    $traceId
);
```

Status is automatically derived: `success` (2xx), `failed` (4xx), `error` (5xx).

### Exception &amp; Error Logging

[](#exception--error-logging)

```
// Catch any Throwable (ideal in a global exception handler)
set_exception_handler(fn($e) => BorneoLogger::exception($e, $userId));

// Generic error without an exception object
BorneoLogger::error('Payment gateway timeout', ['gateway' => 'midtrans'], $userId);
```

### Custom Events

[](#custom-events)

```
BorneoLogger::log('payment.created', [
    'user_id'  => 123,
    'status'   => 'success',
    'metadata' => ['amount' => 50000, 'method' => 'transfer'],
]);
```

`metadata` accepts any array — it will be auto-encoded to a JSON string.

---

Log Payload Structure
---------------------

[](#log-payload-structure)

Each log entry is dispatched as a JSON object (one line per entry in Mode 1):

```
{
  "timestamp":  "2026-06-10T09:42:24.123Z",
  "service":    "your-service-name",
  "event_type": "user.login",
  "status":     "success",
  "user_id":    123,
  "ip":         "1.2.3.4",
  "user_agent": "Mozilla/5.0...",
  "metadata":   "{\"amount\":50000,\"method\":\"transfer\"}"
}
```

---

Framework Integration
---------------------

[](#framework-integration)

### CodeIgniter 3

[](#codeigniter-3)

**1. Create config file** `application/config/logger.php`:

```
