Skip to content

Commit

Permalink
Merge pull request #2 from ovidiuenache/LMH-1
Browse files Browse the repository at this point in the history
[LMH-1] Remove redundant files, update to PHP 8 and update to the new Monolog function structure
  • Loading branch information
ovidiuenache authored Aug 9, 2022
2 parents 18f99d5 + b4726f2 commit dc776e6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 81 deletions.
31 changes: 8 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
logentries-monolog-handler [![Build Status](https://travis-ci.org/logentries/logentries-monolog-handler.png)](https://travis-ci.org/logentries/logentries-monolog-handler)
===========================

*This plug in will no longer be officially supported or maintained by Logentries.<br>*


Current Version: 2.1

## Installation

Add the following to your composer.json and run `composer update`

```json
{
"require": {
"logentries/logentries-monolog-handler": "dev-master"
}
"ovidiuenache/logentries-monolog-handler": "v2.3"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/ovidiuenache/logentries-monolog-handler"
}
]
}
```

## Usage

```php
$monolog->pushHandler(new Logentries\Handler\LogentriesHandler('YOUR_TOKEN'));
```

#### Full example
```php
$monolog = new Logger('TestLog');
$monolog->pushHandler(new LogentriesHandler('YOUR_TOKEN'));
$monolog->addWarning('This is a warning logging message');
```

## Links
[logentries.com](https://logentries.com)
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
{
"name": "Mark Lacomber",
"email": "[email protected]"
},
{
"name": "Ioan Ovidiu Enache",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4",
"monolog/monolog": ">=1.4"
"php": ">=8.0" ,
"monolog/monolog": "^3.2"
},
"require-dev": {
"mockery/mockery": "dev-master"
Expand Down
32 changes: 16 additions & 16 deletions src/Logentries/Handler/LogentriesHandler.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
<?php namespace Logentries\Handler;
<?php

namespace Logentries\Handler;

use Logentries\Socket;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;

/**
* VERSION: 2.0
*/
class LogentriesHandler extends AbstractProcessingHandler
{
protected string $token;

protected Socket $socket;

/**
* @param string $token Token UUID for Logentries logfile
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($token, $level = Logger::DEBUG, $bubble = true, Socket $socket = null)
{
public function __construct(
string $token,
int|string|Level $level = Level::Debug,
bool $bubble = true,
Socket $socket = null
) {
$this->token = $token;
$this->socket = $socket ?? new Socket('data.logentries.com', 80);

parent::__construct($level, $bubble);
}

/**
* {@inheritDoc}
* @inheritDoc
*/
protected function write(array $record): void
protected function write(LogRecord $record): void
{
$data = $this->generateDataStream($record);

$this->socket->write($data);
}

/**
* {@inheritDoc}
* @inheritDoc
*/
public function close(): void
{
$this->socket->close();
}

private function generateDataStream(array $record): string
private function generateDataStream(LogRecord $record): string
{
return \sprintf("%s %s\n", $this->token, $record['formatted']);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Logentries/Socket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Logentries;
<?php

namespace Logentries;

class Socket
{
Expand All @@ -11,8 +13,8 @@ class Socket

public function __construct($url, $port)
{
$this->url = $url;
$this->port = $port;
$this->url = $url;
$this->port = $port;
$this->connectionTimeout = (float) \ini_get('default_socket_timeout');
}

Expand Down Expand Up @@ -102,6 +104,6 @@ private function streamGetMetadata(): array

private function isConnected(): bool
{
return \is_resource($this->resource) && !\feof($this->resource); // on TCP - other party can close connection.
return \is_resource($this->resource) && !\feof($this->resource);
}
}
36 changes: 0 additions & 36 deletions tests/Handler/LogentriesHandlerTest.php

This file was deleted.

0 comments on commit dc776e6

Please sign in to comment.