Skip to content

Commit dc96f2c

Browse files
authored
adding in socket transport
adding in socket transport
1 parent 73b4e67 commit dc96f2c

5 files changed

Lines changed: 85 additions & 71 deletions

File tree

README.md

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
stackify-api-php
2-
================
3-
41
[![PHP version](https://badge.fury.io/ph/stackify%2Flogger.svg)](http://badge.fury.io/ph/stackify%2Flogger)
52

6-
Common libraries for [Stackify Monolog handler](https://github.com/stackify/stackify-log-monolog) and [Stackify log4php appender](https://github.com/stackify/stackify-log-log4php).
7-
This package also includes a standalone [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) compatible logger that can be used without third-party libraries.
8-
9-
Errors and Logs Overview:
3+
# Stackify PHP Logger
104

11-
http://support.stackify.com/errors-and-logs-overview/
5+
Standalone Stackify PHP [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) Logger.
126

13-
Smarter Errors & Logs for PHP:
7+
* **Errors and Logs Overview:** http://support.stackify.com/errors-and-logs-overview/
8+
* **Sign Up for a Trial:** http://www.stackify.com/sign-up/
149

15-
http://stackify.com/smarter-errors-logs-php/
10+
## PHP Logging Framework Integrations
1611

17-
Sign Up for a Trial:
12+
* **Monolog** Handler: https://github.com/stackify/stackify-log-monolog
13+
* **log4php** Appender: https://github.com/stackify/stackify-log-log4php
1814

19-
http://www.stackify.com/sign-up/
2015

2116
## Installation
2217

2318
Install the latest version with `composer require stackify/logger`
2419

25-
There are three different transport options that can be configured to send data to Stackify. Below will show how to implement the different transport options.
20+
### Installation with Linux Agent
21+
22+
This is the suggested installation option, offering the best
23+
logging performance.
24+
25+
```php
26+
use Stackify\Log\Standalone\Logger;
27+
28+
$logger = new Logger('application_name', 'environment_name');
29+
```
30+
31+
### Installation without Linux Agent
2632

27-
### ExecTransport
28-
ExecTransport does not require a Stackify agent to be installed because it sends data directly to Stackify services. It collects log entries in batches, calls curl using the ```exec``` function, and sends data to the background immediately [```exec('curl ... &')```]. This will affect the performance of your application minimally, but it requires permissions to call ```exec``` inside the PHP script and it may cause silent data loss in the event of any network issues. This transport method does not work on Windows. To configure ExecTransport you need to pass the environment name and API key (license key):
33+
This option does not require the Stackify Agent to be installed because it sends data directly to Stackify services. It collects log entries in batches, calls curl using the ```exec``` function, and sends data to the background immediately [```exec('curl ... &')```]. This will affect the performance of your application minimally, but it requires permissions to call ```exec``` inside the PHP script and it may cause silent data loss in the event of any network issues. This transport method does not work on Windows. To configure ExecTransport you need to pass the environment name and API key (license key):
2934

3035
```php
3136
use Stackify\Log\Transport\ExecTransport;
@@ -37,9 +42,8 @@ $logger = new Logger('application_name', 'environment_name', $transport);
3742

3843
#### Optional Settings
3944

40-
4145
**Proxy**
42-
- ExecTransport supports data delivery through proxy. Specify proxy using [libcurl format](http://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html): <[protocol://][user:password@]proxyhost[:port]>
46+
- ExecTransport supports data delivery through proxy. Specify proxy using [libcurl format](http://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html): `<[protocol://][user:password@]proxyhost[:port]>`
4347
```php
4448
$transport = new ExecTransport($apiKey, ['proxy' => 'https://55.88.22.11:3128']);
4549
```
@@ -58,55 +62,8 @@ system environment variables; do not enable if sensitive information such as pas
5862
$logger = new Logger('application_name', 'environment_name', $transport, true);
5963
```
6064

61-
62-
### CurlTransport
63-
CurlTransport does not require a Stackify agent to be installed and it also sends data directly to Stackify services. It collects log entries in a single batch and sends data using native [PHP cURL](http://php.net/manual/en/book.curl.php) functions. This way is a blocking one, so it should not be used on production environments. To configure CurlTransport you need to pass environment name and API key (license key):
64-
```php
65-
use Stackify\Log\Transport\CurlTransport;
66-
use Stackify\Log\Standalone\Logger;
67-
68-
$transport = new CurlTransport('api_key');
69-
$logger = new Logger('application_name', 'environment_name', $transport);
70-
```
71-
72-
#### Optional Settings
73-
74-
**Proxy**
75-
- CurlTransport supports data delivery through proxy. Specify proxy using [libcurl format](http://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html): <[protocol://][user:password@]proxyhost[:port]>
76-
```php
77-
$transport = new CurlTransport($apiKey, ['proxy' => 'https://55.88.22.11:3128']);
78-
```
79-
80-
**Log Server Environment Variables**
81-
- Server environment variables can be added to error log message metadata. **Note:** This will log all
82-
system environment variables; do not enable if sensitive information such as passwords or keys are stored this way.
83-
84-
```php
85-
$logger = new Logger('application_name', 'environment_name', $transport, true);
86-
```
87-
88-
### AgentTransport
89-
90-
AgentTransport does not require additional configuration in your PHP code because all data is passed to the [Stackify agent](http://support.stackify.com/hc/en-us/articles/205419575). The agent must be installed on the same machine. Local TCP socket on port 10515 is used, so performance of your application is affected minimally.
91-
```php
92-
use Stackify\Log\Standalone\Logger;
93-
94-
$logger = new Logger('application_name', 'environment_name');
95-
```
96-
97-
You will need to enable the TCP listener by checking the "PHP App Logs (Agent Log Collector)" in the server settings page in Stackify. See [Log Collectors Page](http://support.stackify.com/hc/en-us/articles/204719709) for more details.
98-
99-
#### Optional Settings
100-
101-
**Log Server Environment Variables**
102-
- Server environment variables can be added to error log message metadata. **Note:** This will log all
103-
system environment variables; do not enable if sensitive information such as passwords or keys are stored this way.
104-
105-
```php
106-
$logger = new Logger('application_name', 'environment_name', null, true);
107-
```
108-
109-
## Troubleshooting
65+
66+
#### Troubleshooting
11067

11168
If transport does not work, try looking into ```vendor\stackify\logger\src\Stackify\debug\log.log``` file (if it is available for writing). Errors are also written to global PHP [error_log](http://php.net/manual/en/errorfunc.configuration.php#ini.error-log).
11269
Note that ExecTransport does not produce any errors at all, but you can switch it to debug mode:
@@ -116,7 +73,7 @@ $transport = new ExecTransport($apiKey, ['debug' => true]);
11673

11774
## License
11875

119-
Copyright 2018 Stackify, LLC.
76+
Copyright 2019 Stackify, LLC.
12077

12178
Licensed under the Apache License, Version 2.0 (the "License");
12279
you may not use this file except in compliance with the License.

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"license": "Apache-2.0",
88
"require": {
99
"php": ">=5.3.0",
10-
"psr/log": "~1.0"
10+
"psr/log": "~1.0",
11+
"guzzlehttp/guzzle": "^6.3",
12+
"guzzlehttp/psr7": "^1.6",
13+
"php-http/socket-client": "^1.4",
14+
"php-http/message": "^1.8"
1115
},
1216
"autoload": {
1317
"psr-4": {

src/Stackify/Log/Standalone/Logger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Stackify\Log\Builder\MessageBuilder;
66
use Stackify\Log\Transport\TransportInterface;
7-
use Stackify\Log\Transport\AgentTransport;
7+
use Stackify\Log\Transport\AgentSocketTransport;
88

99
use Psr\Log\AbstractLogger;
1010

@@ -20,7 +20,7 @@ public function __construct($appName, $environmentName = null, TransportInterfac
2020
{
2121
$messageBuilder = new MessageBuilder('Stackify PHP Logger v.1.0', $appName, $environmentName, $logServerVariables);
2222
if (null === $transport) {
23-
$transport = new AgentTransport();
23+
$transport = new AgentSocketTransport();
2424
}
2525
$transport->setMessageBuilder($messageBuilder);
2626
$this->transport = $transport;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Stackify\Log\Transport;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Http\Client\Socket\Client;
7+
use Http\Message\MessageFactory\GuzzleMessageFactory;
8+
use Stackify\Log\Transport\Config\Agent as Config;
9+
10+
/**
11+
* This transport requires Stackify Agent to be installed and running on the same machine
12+
* Transport writes data over domain socket.
13+
* Agent aggregates log entries and sends data to API.
14+
*/
15+
class AgentSocketTransport extends AbstractApiTransport
16+
{
17+
const ERROR_WRITE = 'Cannot write to domain socket at %s. Is Stackify Agent installed and running?';
18+
const ERROR_CONNECT = 'Cannot connect to domain socket at %s. Is Stackify Agent installed and running? [Error code: %d] [Error message: %s]';
19+
20+
public function __construct(array $options = array())
21+
{
22+
parent::__construct('NONE', $options);
23+
}
24+
25+
protected function getTransportName()
26+
{
27+
return 'AgentSocketTransport';
28+
}
29+
30+
protected function send($data)
31+
{
32+
try {
33+
if (!empty($data)) {
34+
$messageFactory = new GuzzleMessageFactory();
35+
$client = new Client($messageFactory, array('remote_socket' => 'unix://' . Config::DOMAIN_SOCKET_PATH));
36+
37+
$request = $messageFactory->createRequest('POST', 'http://log',
38+
array('Content-Type' => 'application/json', 'Content-Length' => strlen($data)),
39+
$data);
40+
41+
$response = $client->sendRequest($request);
42+
43+
if ($response->getStatusCode() != 200) {
44+
$this->logError(self::ERROR_WRITE, Config::DOMAIN_SOCKET_PATH);
45+
}
46+
}
47+
48+
} catch (HttpClientException $e) {
49+
$this->logError(self::ERROR_CONNECT, Config::DOMAIN_SOCKET_PATH, $e->getCode(), $e->getMessage());
50+
}
51+
}
52+
}

src/Stackify/Log/Transport/Config/Agent.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class Agent
1010
const SOCKET_TIMEOUT_CONNECT = 1;
1111
const SOCKET_TIMEOUT_WRITE = 1;
1212
const SOCKET_MAX_CONNECT_ATTEMPTS = 2;
13-
}
13+
const DOMAIN_SOCKET_PATH = '/usr/local/stackify/stackify.sock';
14+
}

0 commit comments

Comments
 (0)