Skip to content

Commit 8fa25e5

Browse files
committed
Add max_length to config
Close #15
1 parent bb5360e commit 8fa25e5

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ return [
6666
'name' => 'my-custom-name',
6767

6868
// This optional option determines the system name sent with the
69-
// message in the 'source' field. When Forgotten or set to null,
69+
// message in the 'source' field. When forgotten or set to null,
7070
// the current hostname is used.
7171
'system_name' => null,
7272

@@ -81,6 +81,12 @@ return [
8181
// This optional option determines the port on which the gelf
8282
// receiver host is listening. Default is 12201
8383
'port' => 12201,
84+
85+
// This optional option determines the maximum length per message
86+
// field. When forgotten or set to null, the default value of
87+
// \Monolog\Formatter\GelfMessageFormatter::DEFAULT_MAX_LENGTH is
88+
// used (currently this value is 32766)
89+
'max_length' => null,
8490
],
8591
],
8692
];

src/GelfLoggerFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ public function __invoke(array $config): Logger
6666

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

69-
$handler->setFormatter(new GelfMessageFormatter($config['system_name'] ?? null, null, ''));
69+
$handler->setFormatter(
70+
new GelfMessageFormatter(
71+
$systemName = $config['system_name'] ?? null,
72+
$extraPrefix = null,
73+
$contextPrefix = '',
74+
$maxLength = $config['max_length'] ?? null
75+
)
76+
);
7077

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

tests/GelfLoggerTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,56 @@ public function it_should_call_the_udp_transport_method_when_nothing_is_provided
133133
$this->assertInstanceOf(UdpTransport::class, $transport);
134134
}
135135

136+
/** @test */
137+
public function it_should_set_max_length_if_max_length_is_provided(): void
138+
{
139+
$this->app['config']->set('logging.channels.gelf', [
140+
'driver' => 'custom',
141+
'via' => GelfLoggerFactory::class,
142+
'max_length' => 9999
143+
]);
144+
145+
$logger = Log::channel('gelf');
146+
147+
$this->assertSame(
148+
9999,
149+
$this->getAttribute($logger->getHandlers()[0]->getFormatter(), 'maxLength')
150+
);
151+
}
152+
153+
/** @test */
154+
public function it_should_use_default_max_length_when_max_length_is_not_provided(): void
155+
{
156+
$this->app['config']->set('logging.channels.gelf', [
157+
'driver' => 'custom',
158+
'via' => GelfLoggerFactory::class
159+
]);
160+
161+
$logger = Log::channel('gelf');
162+
163+
$this->assertSame(
164+
GelfMessageFormatter::DEFAULT_MAX_LENGTH,
165+
$this->getAttribute($logger->getHandlers()[0]->getFormatter(), 'maxLength')
166+
);
167+
}
168+
169+
/** @test */
170+
public function it_should_use_default_max_length_when_max_length_is_null(): void
171+
{
172+
$this->app['config']->set('logging.channels.gelf', [
173+
'driver' => 'custom',
174+
'via' => GelfLoggerFactory::class,
175+
'max_length' => null
176+
]);
177+
178+
$logger = Log::channel('gelf');
179+
180+
$this->assertSame(
181+
GelfMessageFormatter::DEFAULT_MAX_LENGTH,
182+
$this->getAttribute($logger->getHandlers()[0]->getFormatter(), 'maxLength')
183+
);
184+
}
185+
136186
/**
137187
* Get protected or private attribute from an object.
138188
* NOTICE: This method is for testing purposes only.

0 commit comments

Comments
 (0)