Skip to content

Commit 6e31047

Browse files
authored
Merge pull request #9 from joergbasedow/master
Allow overriding of system name from config
2 parents 4a2e324 + 643c61c commit 6e31047

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ return [
6565
// configuration value
6666
'name' => 'my-custom-name',
6767

68+
// This optional option determines the system name sent with the
69+
// message in the 'source' field. When Forgotten or set to null,
70+
// the current hostname is used.
71+
'system_name' => null,
72+
6873
// This optional option determines the host that will receive the
6974
// gelf log messages. Default is 127.0.0.1
7075
'host' => '127.0.0.1',

src/GelfLoggerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __invoke(array $config): Logger
6363

6464
$handler = new GelfHandler(new Publisher($transport), $this->level($config));
6565

66-
$handler->setFormatter(new GelfMessageFormatter(null, null, null));
66+
$handler->setFormatter(new GelfMessageFormatter($config['system_name'] ?? null, null, null));
6767

6868
foreach ($this->parseProcessors($config) as $processor) {
6969
$handler->pushProcessor(new $processor);

tests/GelfLoggerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,32 @@ public function it_should_not_have_any_processor_if_the_config_does_not_have_pro
5858

5959
$handler->popProcessor();
6060
}
61+
62+
/** @test */
63+
public function it_should_set_system_name_to_current_hostname_if_system_name_is_null(): void
64+
{
65+
$this->app['config']->set('logging.channels.gelf', [
66+
'system_name' => null,
67+
'driver' => 'custom',
68+
'via' => GelfLoggerFactory::class
69+
]);
70+
71+
$logger = Log::channel('gelf');
72+
73+
$this->assertAttributeEquals(gethostname(), 'systemName', $logger->getHandlers()[0]->getFormatter());
74+
}
75+
76+
/** @test */
77+
public function it_should_set_system_name_to_custom_value_if_system_name_config_is_provided(): void
78+
{
79+
$this->app['config']->set('logging.channels.gelf', [
80+
'system_name' => 'my-system-name',
81+
'driver' => 'custom',
82+
'via' => GelfLoggerFactory::class
83+
]);
84+
85+
$logger = Log::channel('gelf');
86+
87+
$this->assertAttributeEquals('my-system-name', 'systemName', $logger->getHandlers()[0]->getFormatter());
88+
}
6189
}

0 commit comments

Comments
 (0)