Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM php:8.1-cli-alpine AS php-base

# Install composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /var/www/.composer

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN apk add --update --no-cache --virtual .build-deps linux-headers $PHPIZE_DEPS && \
pecl install xdebug && docker-php-ext-enable xdebug && \
apk del .build-deps \
15 changes: 15 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/.docker export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/compose.yaml export-ignore
/README.md export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/QUICKSTART.md export-ignore
/phpstan.neon export-ignore
/Makefile export-ignore
/phpcs.xml export-ignore
/CHANGELOG.md export-ignore
/examples.php export-ignore
/qodana.yaml export-ignore
13 changes: 13 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
php:
build:
dockerfile: .docker/php/Dockerfile
context: .
environment:
COMPOSER_HOME: /.composer
volumes:
- .:/app
- ~/.composer:/.composer
user: 1000:1000
working_dir: /app
command: tail -f /dev/null
20 changes: 17 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
"require-dev": {
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.7",
"php-parallel-lint/php-parallel-lint": "^1.4"
},
"autoload": {
"psr-4": {
"Logdash\\": "src/"
}
},
"files": [
"src/Sync/CreateLogSync.php",
"src/Metrics/CreateMetrics.php"
]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -40,6 +45,15 @@
"test": "phpunit",
"phpstan": "phpstan analyse",
"cs": "phpcs",
"cbf": "phpcbf"
"cbf": "phpcbf",
"qa": {
"Codestyle": "@cs",
"Lint": "parallel-lint src tests",
"Static analyse": "@phpstan",
"Tests": "@test"
},
"qa:fix": {
"Codestyle": "@cbf"
}
}
}
32 changes: 32 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#-------------------------------------------------------------------------------#
# Qodana analysis is configured by qodana.yaml file #
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
#-------------------------------------------------------------------------------#
version: "1.0"

#Specify inspection profile for code analysis
profile:
name: qodana.starter

#Enable inspections
#include:
# - name: <SomeEnabledInspectionId>

#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>

php:
version: 8.1 #(Applied in CI/CD pipeline)

#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh

#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
#plugins:
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)

#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
linter: jetbrains/qodana-php:2025.1
10 changes: 4 additions & 6 deletions src/Logdash.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
use Logdash\Logger\Logger;
use Logdash\Metrics\BaseMetrics;
use Logdash\Types\InitializationParams;
use Logdash\Types\RequiredInitializationParams;

require_once __DIR__ . '/Types/LogLevel.php';
require_once __DIR__ . '/Logger/InternalLogger.php';
require_once __DIR__ . '/Metrics/CreateMetrics.php';
require_once __DIR__ . '/Sync/CreateLogSync.php';
use Logdash\Types\LogLevel;

use function Logdash\Metrics\createMetrics;
use function Logdash\Sync\createLogSync;
Expand All @@ -30,6 +25,9 @@ private function __construct(
$this->metrics = $metrics;
}

/**
* @param array{apiKey?:string, host?:string, verbose?:bool}|null $params
*/
public static function create(?array $params = null): self
{
$initParams = new InitializationParams(
Expand Down
37 changes: 19 additions & 18 deletions src/Logger/InternalLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace Logdash\Logger;

use Logdash\LogLevel;
use Logdash\Types\LogLevel;

class InternalLogger
{
private static self $instance;
private const LOG_LEVEL_COLORS = [
LogLevel::ERROR->value => [231, 0, 11],
LogLevel::WARN->value => [254, 154, 0],
LogLevel::INFO->value => [21, 93, 252],
LogLevel::HTTP->value => [0, 166, 166],
LogLevel::VERBOSE->value => [0, 166, 0],
LogLevel::DEBUG->value => [0, 166, 0],
LogLevel::SILLY->value => [80, 80, 80],
'error' => [231, 0, 11],
'warning' => [254, 154, 0],
'info' => [21, 93, 252],
'http' => [0, 166, 166],
'verbose' => [0, 166, 0],
'debug' => [0, 166, 0],
'silly' => [80, 80, 80],
];

public function log(string ...$data): void
Expand All @@ -39,17 +40,17 @@ private function internalLog(LogLevel $level, string $message): void
$color[2],
strtoupper($level->value) . ' '
);

echo "{$datePrefix} {$levelPrefix}{$message}" . PHP_EOL;

file_put_contents(
'php://stdout',
"{$datePrefix} {$levelPrefix}{$message}" . PHP_EOL
);
}
}

// Create a global instance function
function getInternalLogger(): InternalLogger
{
static $instance = null;
if ($instance === null) {
$instance = new InternalLogger();
public static function getInternalLogger(): InternalLogger
{
return self::$instance ??= new self();
}
return $instance;
}

// Create a global instance function
27 changes: 17 additions & 10 deletions src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Logdash\Logger;

use Logdash\LogLevel;
use Logdash\Types\LogLevel;

class Logger
{
private const LOG_LEVEL_COLORS = [
LogLevel::ERROR->value => [231, 0, 11],
LogLevel::WARN->value => [254, 154, 0],
LogLevel::INFO->value => [21, 93, 252],
LogLevel::HTTP->value => [0, 166, 166],
LogLevel::VERBOSE->value => [0, 166, 0],
LogLevel::DEBUG->value => [0, 166, 0],
LogLevel::SILLY->value => [80, 80, 80],
'error' => [231, 0, 11],
'warning' => [254, 154, 0],
'info' => [21, 93, 252],
'http' => [0, 166, 166],
'verbose' => [0, 166, 0],
'debug' => [0, 166, 0],
'silly' => [80, 80, 80],
];

private readonly mixed $logMethod;
Expand Down Expand Up @@ -100,8 +100,11 @@ private function internalLog(LogLevel $level, string $message): void
);
$formattedMessage = "{$datePrefix} {$prefix}{$message}";

$logMethod = $this->logMethod ?? function(string $msg): void {
echo $msg . PHP_EOL;
$logMethod = $this->logMethod ?? function (string $msg): void {
file_put_contents(
'php://stdout',
$msg . PHP_EOL
);
};

$logMethod($formattedMessage);
Expand All @@ -120,6 +123,10 @@ private function getPrefix(LogLevel $level): string
return strtoupper($level->value) . ' ';
}

/**
* @param array<mixed|array|object> $data
* @return array<string>
*/
private function convertToStrings(array $data): array
{
return array_map(function ($item): string {
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/BaseMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
interface BaseMetrics
{
public function set(string $key, float $value): void;

public function mutate(string $key, float $value): void;
}
9 changes: 9 additions & 0 deletions src/Metrics/MetricOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Logdash\Metrics;

enum MetricOperation: string
{
case SET = 'set';
case CHANGE = 'change';
}
13 changes: 4 additions & 9 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Logdash\Logger\InternalLogger;
use Logdash\Types\RequiredInitializationParams;

enum MetricOperation: string
{
case SET = 'set';
case CHANGE = 'change';
}

class Metrics implements BaseMetrics
{
private Client $httpClient;
Expand All @@ -27,7 +22,7 @@ public function __construct(
public function set(string $name, float $value): void
{
if ($this->params->verbose) {
\Logdash\Logger\getInternalLogger()->verbose("Setting metric {$name} to {$value}");
InternalLogger::getInternalLogger()->verbose("Setting metric {$name} to {$value}");
}

$this->sendMetric($name, $value, MetricOperation::SET);
Expand All @@ -36,7 +31,7 @@ public function set(string $name, float $value): void
public function mutate(string $name, float $value): void
{
if ($this->params->verbose) {
\Logdash\Logger\getInternalLogger()->verbose("Mutating metric {$name} by {$value}");
InternalLogger::getInternalLogger()->verbose("Mutating metric {$name} by {$value}");
}

$this->sendMetric($name, $value, MetricOperation::CHANGE);
Expand All @@ -59,7 +54,7 @@ private function sendMetric(string $name, float $value, MetricOperation $operati
]);
} catch (GuzzleException $e) {
if ($this->params->verbose) {
\Logdash\Logger\getInternalLogger()->verbose("Failed to send metric: " . $e->getMessage());
InternalLogger::getInternalLogger()->verbose("Failed to send metric: " . $e->getMessage());
}
// Fail silently in production
}
Expand Down
3 changes: 2 additions & 1 deletion src/Sync/CreateLogSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
function createLogSync(RequiredInitializationParams $params): LogSync
{
if (empty($params->apiKey)) {
\Logdash\Logger\getInternalLogger()->log(
\Logdash\Logger\InternalLogger::getInternalLogger()->log(
// phpcs:ignore
'Api key was not provided in the InitializationParams when calling Logdash::create(), using only local logger.'
);
return new NoopLogSync();
Expand Down
5 changes: 3 additions & 2 deletions src/Sync/HttpLogSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Logdash\LogLevel;
use Logdash\Logger\InternalLogger;
use Logdash\Types\LogLevel;
use Logdash\Types\RequiredInitializationParams;

class HttpLogSync implements LogSync
Expand Down Expand Up @@ -44,7 +45,7 @@ public function send(string $message, LogLevel $level, string $createdAt): void
]);
} catch (GuzzleException $e) {
if ($this->params->verbose) {
\Logdash\Logger\getInternalLogger()->verbose("Failed to send log: " . $e->getMessage());
InternalLogger::getInternalLogger()->verbose("Failed to send log: " . $e->getMessage());
}
// Fail silently in production
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sync/LogSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Logdash\Sync;

use Logdash\LogLevel;
use Logdash\Types\LogLevel;

interface LogSync
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sync/NoopLogSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Logdash\Sync;

use Logdash\LogLevel;
use Logdash\Types\LogLevel;

class NoopLogSync implements LogSync
{
Expand Down
12 changes: 2 additions & 10 deletions src/Types/InitializationParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public function __construct(
public readonly ?string $apiKey = null,
public readonly string $host = 'https://api.logdash.io',
public readonly bool $verbose = false
) {}
) {
}

public function toRequired(): RequiredInitializationParams
{
Expand All @@ -21,12 +22,3 @@ public function toRequired(): RequiredInitializationParams
);
}
}

class RequiredInitializationParams
{
public function __construct(
public readonly string $apiKey,
public readonly string $host,
public readonly bool $verbose
) {}
}
2 changes: 1 addition & 1 deletion src/Types/LogLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Logdash;
namespace Logdash\Types;

enum LogLevel: string
{
Expand Down
13 changes: 13 additions & 0 deletions src/Types/RequiredInitializationParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Logdash\Types;

class RequiredInitializationParams
{
public function __construct(
public readonly string $apiKey,
public readonly string $host,
public readonly bool $verbose
) {
}
}
Loading