Skip to content

Commit 2a8df1c

Browse files
committed
2 parents d54ecc9 + 7e25eed commit 2a8df1c

File tree

8 files changed

+91
-10
lines changed

8 files changed

+91
-10
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
},
4040
"require": {
4141
"php": ">=5.6.6",
42-
"elasticsearch/elasticsearch": "^5.0",
42+
"elasticsearch/elasticsearch": "^5.0|^6.0",
4343
"illuminate/pagination": "*",
4444
"illuminate/support": "*",
45-
"symfony/var-dumper": "*"
45+
"symfony/var-dumper": "*",
46+
"monolog/monolog": "^1.23"
4647
},
4748
"require-dev": {
4849
"phpunit/phpunit": "5.7.0"

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ $connection = Connection::create([
126126
// 'handler' => new MyCustomHandler(),
127127

128128
'index' => 'my_index',
129+
130+
'logging' => [
131+
'enabled' => env('ELASTIC_LOGGING_ENABLED',false),
132+
'level' => env('ELASTIC_LOGGING_LEVEL','all'),
133+
'location' => env('ELASTIC_LOGGING_LOCATION',base_path('storage/logs/elasticsearch.log'))
134+
],
129135
]);
130136

131137

src/Connection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public static function create($config)
6464

6565
$clientBuilder->setHosts($config["servers"]);
6666

67+
$clientBuilder = self::configureLogging($clientBuilder,$config);
68+
6769
$query = new Query($clientBuilder->build());
6870

6971
if (array_key_exists("index", $config) and $config["index"] != "") {
@@ -103,6 +105,8 @@ function connection($name)
103105

104106
$clientBuilder->setHosts($config["servers"]);
105107

108+
$clientBuilder = self::configureLogging($clientBuilder,$config);
109+
106110
if (!empty($config['handler'])) {
107111
$clientBuilder->setHandler($config['handler']);
108112
}
@@ -121,6 +125,21 @@ function connection($name)
121125
}
122126

123127

128+
/**
129+
* @param ClientBuilder $clientBuilder
130+
* @param array $config
131+
* @return ClientBuilder
132+
*/
133+
public static function configureLogging(ClientBuilder $clientBuilder, array $config)
134+
{
135+
if (array_get($config,'logging.enabled')) {
136+
$logger = ClientBuilder::defaultLogger(array_get($config,'logging.location'), array_get($config,'logging.level','all'));
137+
$clientBuilder->setLogger($logger);
138+
}
139+
return $clientBuilder;
140+
}
141+
142+
124143
/**
125144
* route the request to the query class
126145
* @param $connection

src/ScoutEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected function filters(Builder $builder)
168168
*/
169169
public function map($results, $model)
170170
{
171-
if (count($results['hits']['total']) === 0) {
171+
if ($results['hits']['total'] === 0) {
172172
return Collection::make();
173173
}
174174

src/config/es.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
'servers' => [
3232

3333
[
34-
"host" => env("ELASTIC_HOST", "127.0.0.1"),
35-
"port" => env("ELASTIC_PORT", 9200),
34+
'host' => env('ELASTIC_HOST', '127.0.0.1'),
35+
'port' => env('ELASTIC_PORT', 9200),
3636
'user' => env('ELASTIC_USER', ''),
3737
'pass' => env('ELASTIC_PASS', ''),
3838
'scheme' => env('ELASTIC_SCHEME', 'http'),
@@ -44,6 +44,12 @@
4444

4545
// Elasticsearch handlers
4646
// 'handler' => new MyCustomHandler(),
47+
48+
'logging' => [
49+
'enabled' => env('ELASTIC_LOGGING_ENABLED',false),
50+
'level' => env('ELASTIC_LOGGING_LEVEL','all'),
51+
'location' => env('ELASTIC_LOGGING_LOCATION',base_path('storage/logs/elasticsearch.log'))
52+
],
4753
]
4854
],
4955

@@ -64,18 +70,18 @@
6470

6571
'my_index_1' => [
6672

67-
"aliases" => [
68-
"my_index"
73+
'aliases' => [
74+
'my_index'
6975
],
7076

7177
'settings' => [
72-
"number_of_shards" => 1,
73-
"number_of_replicas" => 0,
78+
'number_of_shards' => 1,
79+
'number_of_replicas' => 0,
7480
],
7581

7682
'mappings' => [
7783
'posts' => [
78-
"properties" => [
84+
'properties' => [
7985
'title' => [
8086
'type' => 'string'
8187
]

src/helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,18 @@ function is_callback_function($callback)
2828
}
2929
}
3030

31+
if (! function_exists('base_path')) {
32+
/**
33+
* Get the path to the base of the install.
34+
*
35+
* @param string $path
36+
* @return string
37+
*/
38+
function base_path($path = '')
39+
{
40+
return app()->basePath().($path ? '/'.$path : $path);
41+
}
42+
}
43+
44+
3145

src/storage/logs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/LoggingTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: mike
5+
* Date: 07/05/18
6+
* Time: 19:58
7+
*/
8+
9+
namespace Basemkhirat\Elasticsearch\Tests;
10+
11+
12+
use Basemkhirat\Elasticsearch\Connection;
13+
use Elasticsearch\ClientBuilder;
14+
use Monolog\Logger;
15+
16+
class LoggingTest extends \PHPUnit_Framework_TestCase
17+
{
18+
19+
public function testConfigureLogging()
20+
{
21+
$client = ClientBuilder::create();
22+
$newClientBuilder = Connection::configureLogging($client,[
23+
'logging'=>[
24+
'enabled'=>true,
25+
'level'=>'all',
26+
'location'=>'../src/storage/logs/elasticsearch.log'
27+
]
28+
]);
29+
30+
$this->assertInstanceOf(ClientBuilder::class,$newClientBuilder);
31+
$this->assertAttributeInstanceOf(Logger::class,'logger',$newClientBuilder);
32+
}
33+
}

0 commit comments

Comments
 (0)