Skip to content

Commit 01ebeae

Browse files
author
Dmitry Tarasov
committed
Improve internal logging
1 parent cfc5ad6 commit 01ebeae

5 files changed

Lines changed: 113 additions & 95 deletions

File tree

src/Stackify/Log/Transport/AbstractApiTransport.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ abstract class AbstractApiTransport extends AbstractTransport
1616
);
1717
protected $apiKey;
1818
protected $proxy;
19-
protected $debug = false;
2019

2120
public function __construct($apiKey, array $options = array())
2221
{

src/Stackify/Log/Transport/AbstractTransport.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,48 @@ abstract class AbstractTransport implements TransportInterface
1111
* @var \Stackify\Log\MessageBuilder
1212
*/
1313
protected $messageBuilder;
14-
private $errorLogPath;
14+
private $debugLogPath;
15+
protected $debug = false;
1516

1617
public function __construct()
1718
{
1819
$ds = DIRECTORY_SEPARATOR;
19-
$this->errorLogPath = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
20+
$this->debugLogPath = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
2021
}
2122

2223
public function setMessageBuilder(MessageBuilder $messageBuilder)
2324
{
2425
$this->messageBuilder = $messageBuilder;
2526
}
2627

27-
protected function logInternal($message)
28+
protected abstract function getTransportName();
29+
30+
protected function logError($message)
31+
{
32+
$this->log($message, func_get_args(), false);
33+
}
34+
35+
protected function logDebug($message)
2836
{
29-
$args = array_slice(func_get_args(), 1);
30-
$template = "[Stackify Log] $message [{$this->getTransportName()}]";
31-
$formatted = preg_replace('/\r\n/', '', vsprintf($template, $args));
37+
if (!$this->debug) {
38+
return;
39+
}
40+
$this->log($message, func_get_args(), true);
41+
}
42+
43+
private function log($message, $args, $success = true)
44+
{
45+
$replacements = array_slice($args, 1);
46+
$prefix = $success ? 'Stackify Log' : 'Stackify Error';
47+
$template = "[$prefix] $message [{$this->getTransportName()}]";
48+
$formatted = preg_replace('/\r\n/', '', vsprintf($template, $replacements));
3249
// first option - write to local file if possible
3350
// this can be not available because of file permissions
34-
@file_put_contents($this->errorLogPath, "$formatted\n", FILE_APPEND);
35-
// second option - send to default PHP error log
36-
error_log($formatted);
51+
@file_put_contents($this->debugLogPath, "$formatted\n", FILE_APPEND);
52+
if (!$success) {
53+
// second option - send to default PHP error log
54+
error_log($formatted);
55+
}
3756
}
3857

39-
protected abstract function getTransportName();
40-
4158
}

src/Stackify/Log/Transport/AgentTransport.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function finish()
3333
// agent trasport does not use queues
3434
if ($this->connected) {
3535
if (false === @fclose($this->socket)) {
36-
$this->logInternal(self::ERROR_CLOSE);
36+
$this->logError(self::ERROR_CLOSE);
3737
}
3838
}
3939
}
@@ -48,7 +48,7 @@ private function send($data)
4848
$this->connect();
4949
if ($this->connected) {
5050
if (false === @fwrite($this->socket, $data)) {
51-
$this->logInternal(self::ERROR_WRITE);
51+
$this->logError(self::ERROR_WRITE);
5252
}
5353
}
5454
}
@@ -63,7 +63,7 @@ private function connect()
6363
if ($this->connected) {
6464
stream_set_timeout($this->socket, Config::SOCKET_TIMEOUT_WRITE);
6565
} else {
66-
$this->logInternal(self::ERROR_CONNECT, $remote, $errno, $errstr);
66+
$this->logError(self::ERROR_CONNECT, $remote, $errno, $errstr);
6767
}
6868
}
6969
}

src/Stackify/Log/Transport/CurlTransport.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
class CurlTransport extends AbstractApiTransport
1212
{
1313

14-
const ERROR_CURL = 'Curl returned an error. [Error no: %d] [HTTP code: %d] [Message: "%s"]';
15-
const SUCCESS_CURL = 'Curl sent data successfully. [HTTP code: %d] [Message: "%s"]';
14+
const ERROR_CURL = 'Curl returned an error. [Error no: %d] [HTTP code: %d] [Message: "%s"] [Response: "%s"]';
15+
const SUCCESS_CURL = 'Curl sent data successfully. [HTTP code: %d] [Response: "%s"]';
1616

1717
public function __construct($apiKey, array $options = array())
1818
{
@@ -52,10 +52,10 @@ protected function send($data)
5252
$errorNo = curl_errno($handle);
5353
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
5454
$error = curl_error($handle);
55-
if (0 !== $errorNo) {
56-
$this->logInternal(self::ERROR_CURL, $errorNo, $code, $error);
57-
} elseif ($this->debug) {
58-
$this->logInternal(self::SUCCESS_CURL, $code, $response);
55+
if (0 !== $errorNo || 200 !== $code) {
56+
$this->logError(self::ERROR_CURL, $errorNo, $code, $error, $response);
57+
} else {
58+
$this->logDebug(self::SUCCESS_CURL, $code, $response);
5959
}
6060
curl_close($handle);
6161
}
Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,77 @@
1-
<?php
2-
3-
namespace Stackify\Log\Transport;
4-
5-
use Stackify\Log\Transport\Config\Api;
6-
7-
/**
8-
* This transport collects log data until the end of processing.
9-
* It sends data executing shell curl and sending it to background.
10-
*/
11-
class ExecTransport extends AbstractApiTransport
12-
{
13-
14-
protected $curlPath = 'curl';
15-
16-
const ERROR_CURL = 'Command returned an error. [Command: "%s"] [Return code: %d] [Message: "%s"]';
17-
const SUCCESS_CURL = 'Command sent. [Command: "%s"]';
18-
19-
public function __construct($apiKey, array $options = array())
20-
{
21-
parent::__construct($apiKey, $options);
22-
// @TODO windows support
23-
}
24-
25-
protected function getAllowedOptions()
26-
{
27-
return array(
28-
'curlPath' => '/.+/',
29-
);
30-
}
31-
32-
protected function getTransportName()
33-
{
34-
return 'ExecTransport';
35-
}
36-
37-
protected function send($data)
38-
{
39-
$url = Api::API_BASE_URL . Api::API_CALL_LOGS;
40-
$cmd = "$this->curlPath -X POST";
41-
foreach ($this->getApiHeaders() as $name => $value) {
42-
$cmd .= " --header \"$name: $value\"";
43-
}
44-
$escapedData = $this->escapeArg($data);
45-
$maxTime = Api::API_MAX_TIME;
46-
$cmd .= " --data '$escapedData' '$url' --max-time $maxTime";
47-
if ($this->proxy) {
48-
$cmd .= " --proxy '$this->proxy'";
49-
}
50-
if ($this->debug) {
51-
$cmd .= ' --verbose';
52-
} else {
53-
// return immediately while curl will run in the background
54-
$cmd .= ' > /dev/null 2>&1 &';
55-
}
56-
$output = array();
57-
$r = exec($cmd, $output, $result);
58-
if ($this->debug) {
59-
if ($result !== 0) {
60-
// curl returned some error
61-
$this->logInternal(self::ERROR_CURL, $cmd, $result, implode(' ', $output));
62-
} else {
63-
$this->logInternal(self::SUCCESS_CURL, $cmd);
64-
}
65-
}
66-
}
67-
68-
private function escapeArg($string)
69-
{
70-
// @TODO test special chars
71-
// http://stackoverflow.com/a/1250279/871861
72-
return str_replace("'", "'\"'\"'", $string);
73-
}
74-
1+
<?php
2+
3+
namespace Stackify\Log\Transport;
4+
5+
use Stackify\Log\Transport\Config\Api;
6+
7+
/**
8+
* This transport collects log data until the end of processing.
9+
* It sends data executing shell curl and sending it to background.
10+
*/
11+
class ExecTransport extends AbstractApiTransport
12+
{
13+
14+
protected $curlPath = 'curl';
15+
16+
const ERROR_CURL = 'Command returned an error. [Command: "%s"] [Return code: %d] [Message: "%s"]';
17+
const SUCCESS_CURL = 'Command sent. [Command: "%s"]';
18+
19+
public function __construct($apiKey, array $options = array())
20+
{
21+
parent::__construct($apiKey, $options);
22+
// @TODO windows support
23+
}
24+
25+
protected function getAllowedOptions()
26+
{
27+
return array(
28+
'curlPath' => '/.+/',
29+
);
30+
}
31+
32+
protected function getTransportName()
33+
{
34+
return 'ExecTransport';
35+
}
36+
37+
protected function send($data)
38+
{
39+
$url = Api::API_BASE_URL . Api::API_CALL_LOGS;
40+
$cmd = "$this->curlPath -X POST";
41+
foreach ($this->getApiHeaders() as $name => $value) {
42+
$cmd .= " --header \"$name: $value\"";
43+
}
44+
$escapedData = $this->escapeArg($data);
45+
$maxTime = Api::API_MAX_TIME;
46+
$cmd .= " --data '$escapedData' '$url' --max-time $maxTime";
47+
if ($this->proxy) {
48+
$cmd .= " --proxy '$this->proxy'";
49+
}
50+
if ($this->debug) {
51+
$cmd .= ' --verbose';
52+
} else {
53+
// return immediately while curl will run in the background
54+
$cmd .= ' > /dev/null 2>&1 &';
55+
}
56+
$output = array();
57+
$r = exec($cmd, $output, $result);
58+
// if debug mode is off, it makes no sense to check result,
59+
// because command is send to background
60+
if ($this->debug) {
61+
if ($result !== 0) {
62+
// curl returned some error
63+
$this->logError(self::ERROR_CURL, $cmd, $result, implode(' ', $output));
64+
} else {
65+
$this->logDebug(self::SUCCESS_CURL, $cmd);
66+
}
67+
}
68+
}
69+
70+
private function escapeArg($string)
71+
{
72+
// @TODO test special chars
73+
// http://stackoverflow.com/a/1250279/871861
74+
return str_replace("'", "'\"'\"'", $string);
75+
}
76+
7577
}

0 commit comments

Comments
 (0)