Skip to content

Commit 49a8065

Browse files
authored
Fix Options::setEnableLogs and add Options:: setBeforeSendLogCallback() (#1852)
1 parent c738238 commit 49a8065

2 files changed

Lines changed: 70 additions & 39 deletions

File tree

src/Options.php

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ public function getEnableTracing(): ?bool
155155
return $this->options['enable_tracing'];
156156
}
157157

158+
/**
159+
* Sets if logs should be enabled or not.
160+
*
161+
* @param bool|null $enableLogs Boolean if logs should be enabled or not
162+
*/
163+
public function setEnableLogs(?bool $enableLogs): self
164+
{
165+
$options = array_merge($this->options, ['enable_logs' => $enableLogs]);
166+
167+
$this->options = $this->resolver->resolve($options);
168+
169+
return $this;
170+
}
171+
172+
/**
173+
* Gets if logs is enabled or not.
174+
*/
175+
public function getEnableLogs(): bool
176+
{
177+
return $this->options['enable_logs'] ?? false;
178+
}
179+
158180
/**
159181
* Sets the sampling factor to apply to transactions. A value of 0 will deny
160182
* sending any transactions, and a value of 1 will send 100% of transactions.
@@ -613,6 +635,34 @@ public function setBeforeSendCheckInCallback(callable $callback): self
613635
return $this;
614636
}
615637

638+
/**
639+
* Gets a callback that will be invoked before an log is sent to the server.
640+
* If `null` is returned it won't be sent.
641+
*
642+
* @psalm-return callable(Log): ?Log
643+
*/
644+
public function getBeforeSendLogCallback(): callable
645+
{
646+
return $this->options['before_send_log'];
647+
}
648+
649+
/**
650+
* Sets a callable to be called to decide whether a log should
651+
* be captured or not.
652+
*
653+
* @param callable $callback The callable
654+
*
655+
* @psalm-param callable(Log): ?Log $callback
656+
*/
657+
public function setBeforeSendLogCallback(callable $callback): self
658+
{
659+
$options = array_merge($this->options, ['before_send_log' => $callback]);
660+
661+
$this->options = $this->resolver->resolve($options);
662+
663+
return $this;
664+
}
665+
616666
/**
617667
* Gets a callback that will be invoked before metrics are sent to the server.
618668
* If `null` is returned it won't be sent.
@@ -1153,39 +1203,6 @@ public function setTracesSampler(?callable $sampler): self
11531203
return $this;
11541204
}
11551205

1156-
/**
1157-
* Sets if logs should be enabled or not.
1158-
*
1159-
* @param bool|null $enableLogs Boolean if logs should be enabled or not
1160-
*/
1161-
public function setEnableLogs(?bool $enableLogs): self
1162-
{
1163-
$options = array_merge($this->options, ['enable_tracing' => $enableLogs]);
1164-
1165-
$this->options = $this->resolver->resolve($options);
1166-
1167-
return $this;
1168-
}
1169-
1170-
/**
1171-
* Gets if logs is enabled or not.
1172-
*/
1173-
public function getEnableLogs(): bool
1174-
{
1175-
return $this->options['enable_logs'] ?? false;
1176-
}
1177-
1178-
/**
1179-
* Gets a callback that will be invoked before an log is sent to the server.
1180-
* If `null` is returned it won't be sent.
1181-
*
1182-
* @psalm-return callable(Log): ?Log
1183-
*/
1184-
public function getBeforeSendLogCallback(): callable
1185-
{
1186-
return $this->options['before_send_log'];
1187-
}
1188-
11891206
/**
11901207
* Configures the options of the client.
11911208
*
@@ -1202,6 +1219,7 @@ private function configureOptions(OptionsResolver $resolver): void
12021219
'prefixes' => array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: '')),
12031220
'sample_rate' => 1,
12041221
'enable_tracing' => null,
1222+
'enable_logs' => false,
12051223
'traces_sample_rate' => null,
12061224
'traces_sampler' => null,
12071225
'profiles_sample_rate' => null,
@@ -1233,6 +1251,9 @@ private function configureOptions(OptionsResolver $resolver): void
12331251
'before_send_check_in' => static function (Event $checkIn): Event {
12341252
return $checkIn;
12351253
},
1254+
'before_send_log' => static function (Log $log): Log {
1255+
return $log;
1256+
},
12361257
/**
12371258
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
12381259
*/
@@ -1263,15 +1284,12 @@ private function configureOptions(OptionsResolver $resolver): void
12631284
'capture_silenced_errors' => false,
12641285
'max_request_body_size' => 'medium',
12651286
'class_serializers' => [],
1266-
'enable_logs' => false,
1267-
'before_send_log' => static function (Log $log): Log {
1268-
return $log;
1269-
},
12701287
]);
12711288

12721289
$resolver->setAllowedTypes('prefixes', 'string[]');
12731290
$resolver->setAllowedTypes('sample_rate', ['int', 'float']);
12741291
$resolver->setAllowedTypes('enable_tracing', ['null', 'bool']);
1292+
$resolver->setAllowedTypes('enable_logs', 'bool');
12751293
$resolver->setAllowedTypes('traces_sample_rate', ['null', 'int', 'float']);
12761294
$resolver->setAllowedTypes('traces_sampler', ['null', 'callable']);
12771295
$resolver->setAllowedTypes('profiles_sample_rate', ['null', 'int', 'float']);
@@ -1290,6 +1308,7 @@ private function configureOptions(OptionsResolver $resolver): void
12901308
$resolver->setAllowedTypes('server_name', 'string');
12911309
$resolver->setAllowedTypes('before_send', ['callable']);
12921310
$resolver->setAllowedTypes('before_send_transaction', ['callable']);
1311+
$resolver->setAllowedTypes('before_send_log', 'callable');
12931312
$resolver->setAllowedTypes('ignore_exceptions', 'string[]');
12941313
$resolver->setAllowedTypes('ignore_transactions', 'string[]');
12951314
$resolver->setAllowedTypes('trace_propagation_targets', ['null', 'string[]']);
@@ -1313,8 +1332,6 @@ private function configureOptions(OptionsResolver $resolver): void
13131332
$resolver->setAllowedTypes('capture_silenced_errors', 'bool');
13141333
$resolver->setAllowedTypes('max_request_body_size', 'string');
13151334
$resolver->setAllowedTypes('class_serializers', 'array');
1316-
$resolver->setAllowedTypes('enable_logs', 'bool');
1317-
$resolver->setAllowedTypes('before_send_log', 'callable');
13181335

13191336
$resolver->setAllowedValues('max_request_body_size', ['none', 'never', 'small', 'medium', 'always']);
13201337
$resolver->setAllowedValues('dsn', \Closure::fromCallable([$this, 'validateDsnOption']));

tests/OptionsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public static function optionsDataProvider(): \Generator
9696
'setSampleRate',
9797
];
9898

99+
yield [
100+
'enable_logs',
101+
true,
102+
'getEnableLogs',
103+
'setEnableLogs',
104+
];
105+
99106
yield [
100107
'traces_sample_rate',
101108
0.5,
@@ -264,6 +271,13 @@ static function (): void {},
264271
'setBeforeSendCheckInCallback',
265272
];
266273

274+
yield [
275+
'before_send_log',
276+
static function (): void {},
277+
'getBeforeSendLogCallback',
278+
'setBeforeSendLogCallback',
279+
];
280+
267281
yield [
268282
'before_send_metrics',
269283
static function (): void {},

0 commit comments

Comments
 (0)