Skip to content

Commit

Permalink
Merge pull request #2 from lukassarfert/patch-1
Browse files Browse the repository at this point in the history
drop messages if remap loki level is set to false
  • Loading branch information
cebe authored May 17, 2023
2 parents a5759f1 + c63dc5d commit 89b97b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ Add the log target to your application config:
// yii category
'yii\web\HttpException:404' => [
// yii level => loki level
// set loki level to false, to drop messages with that category
'*' => 'info',
],
'yii\web\HttpException:401' => [
// yii level => loki level
// set loki level to false, to drop messages with that category
'*' => 'warning',
],
],
Expand Down
8 changes: 7 additions & 1 deletion src/LokiLogTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public function export()
{
$lokiMessages = [];
foreach ($this->messages as $key => $message) {
$lokiMessages[] = $this->formatMessage($message);
$lokiMessage = $this->formatMessage($message);
if ($lokiMessage) {
$lokiMessages[] = $lokiMessage;
}
}

// https://grafana.com/docs/loki/latest/api/#push-log-entries-to-loki
Expand All @@ -120,6 +123,9 @@ public function formatMessage($message)
list($text, $level, $category, $timestamp) = $message;
$level = Logger::getLevelName($level);
$level = $this->remapLevel($level, $category);
if ($level === false) {
return false;
}
if (!is_string($text)) {
// exceptions may not be serializable if in the call stack somewhere is a Closure
if ($text instanceof \Exception || $text instanceof \Throwable) {
Expand Down

0 comments on commit 89b97b0

Please sign in to comment.