Skip to content

Commit b56a83c

Browse files
authored
Merge pull request #1 from imikemiller/query_logging
Query logging configuration
2 parents d342e28 + 1e680cb commit b56a83c

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"elasticsearch/elasticsearch": "^5.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"

src/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ function connection($name)
121121
}
122122

123123

124+
public static function configureLogging(ClientBuilder $clientBuilder,array $config)
125+
{
126+
if (array_get($config,'logging.enabled')) {
127+
$logger = ClientBuilder::defaultLogger(array_get($config,'logging.location'), array_get($config,'logging.level','all'));
128+
$clientBuilder->setLogger($logger);
129+
}
130+
return $clientBuilder;
131+
}
132+
133+
124134
/**
125135
* route the request to the query class
126136
* @param $connection

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)