Skip to content

Commit d22e55b

Browse files
authored
Merge pull request #2 from logdash-io/mleczakm-patch-2
Fix invalid syntax for php 8.1, introduce linter, CS fixes, local dev environment based on docker
2 parents b5406e5 + 8f5c1b8 commit d22e55b

19 files changed

Lines changed: 168 additions & 66 deletions

.docker/php/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM php:8.1-cli-alpine AS php-base
2+
3+
# Install composer
4+
ENV COMPOSER_ALLOW_SUPERUSER 1
5+
ENV COMPOSER_HOME /var/www/.composer
6+
7+
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
8+
9+
RUN apk add --update --no-cache --virtual .build-deps linux-headers $PHPIZE_DEPS && \
10+
pecl install xdebug && docker-php-ext-enable xdebug && \
11+
apk del .build-deps \

.gitattributes

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/.docker export-ignore
2+
/.gitattributes export-ignore
3+
/.github export-ignore
4+
/.gitignore export-ignore
5+
/compose.yaml export-ignore
6+
/README.md export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/tests export-ignore
9+
/QUICKSTART.md export-ignore
10+
/phpstan.neon export-ignore
11+
/Makefile export-ignore
12+
/phpcs.xml export-ignore
13+
/CHANGELOG.md export-ignore
14+
/examples.php export-ignore
15+
/qodana.yaml export-ignore

compose.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
php:
3+
build:
4+
dockerfile: .docker/php/Dockerfile
5+
context: .
6+
environment:
7+
COMPOSER_HOME: /.composer
8+
volumes:
9+
- .:/app
10+
- ~/.composer:/.composer
11+
user: 1000:1000
12+
working_dir: /app
13+
command: tail -f /dev/null

composer.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
"require-dev": {
2525
"phpunit/phpunit": "^10.0",
2626
"phpstan/phpstan": "^1.10",
27-
"squizlabs/php_codesniffer": "^3.7"
27+
"squizlabs/php_codesniffer": "^3.7",
28+
"php-parallel-lint/php-parallel-lint": "^1.4"
2829
},
2930
"autoload": {
3031
"psr-4": {
3132
"Logdash\\": "src/"
32-
}
33+
},
34+
"files": [
35+
"src/Sync/CreateLogSync.php",
36+
"src/Metrics/CreateMetrics.php"
37+
]
3338
},
3439
"autoload-dev": {
3540
"psr-4": {
@@ -40,6 +45,15 @@
4045
"test": "phpunit",
4146
"phpstan": "phpstan analyse",
4247
"cs": "phpcs",
43-
"cbf": "phpcbf"
48+
"cbf": "phpcbf",
49+
"qa": {
50+
"Codestyle": "@cs",
51+
"Lint": "parallel-lint src tests",
52+
"Static analyse": "@phpstan",
53+
"Tests": "@test"
54+
},
55+
"qa:fix": {
56+
"Codestyle": "@cbf"
57+
}
4458
}
4559
}

qodana.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#-------------------------------------------------------------------------------#
2+
# Qodana analysis is configured by qodana.yaml file #
3+
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
4+
#-------------------------------------------------------------------------------#
5+
version: "1.0"
6+
7+
#Specify inspection profile for code analysis
8+
profile:
9+
name: qodana.starter
10+
11+
#Enable inspections
12+
#include:
13+
# - name: <SomeEnabledInspectionId>
14+
15+
#Disable inspections
16+
#exclude:
17+
# - name: <SomeDisabledInspectionId>
18+
# paths:
19+
# - <path/where/not/run/inspection>
20+
21+
php:
22+
version: 8.1 #(Applied in CI/CD pipeline)
23+
24+
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
25+
#bootstrap: sh ./prepare-qodana.sh
26+
27+
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
28+
#plugins:
29+
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)
30+
31+
#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
32+
linter: jetbrains/qodana-php:2025.1

src/Logdash.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
use Logdash\Logger\Logger;
88
use Logdash\Metrics\BaseMetrics;
99
use Logdash\Types\InitializationParams;
10-
use Logdash\Types\RequiredInitializationParams;
11-
12-
require_once __DIR__ . '/Types/LogLevel.php';
13-
require_once __DIR__ . '/Logger/InternalLogger.php';
14-
require_once __DIR__ . '/Metrics/CreateMetrics.php';
15-
require_once __DIR__ . '/Sync/CreateLogSync.php';
10+
use Logdash\Types\LogLevel;
1611

1712
use function Logdash\Metrics\createMetrics;
1813
use function Logdash\Sync\createLogSync;
@@ -30,6 +25,9 @@ private function __construct(
3025
$this->metrics = $metrics;
3126
}
3227

28+
/**
29+
* @param array{apiKey?:string, host?:string, verbose?:bool}|null $params
30+
*/
3331
public static function create(?array $params = null): self
3432
{
3533
$initParams = new InitializationParams(

src/Logger/InternalLogger.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
namespace Logdash\Logger;
66

7-
use Logdash\LogLevel;
7+
use Logdash\Types\LogLevel;
88

99
class InternalLogger
1010
{
11+
private static self $instance;
1112
private const LOG_LEVEL_COLORS = [
12-
LogLevel::ERROR->value => [231, 0, 11],
13-
LogLevel::WARN->value => [254, 154, 0],
14-
LogLevel::INFO->value => [21, 93, 252],
15-
LogLevel::HTTP->value => [0, 166, 166],
16-
LogLevel::VERBOSE->value => [0, 166, 0],
17-
LogLevel::DEBUG->value => [0, 166, 0],
18-
LogLevel::SILLY->value => [80, 80, 80],
13+
'error' => [231, 0, 11],
14+
'warning' => [254, 154, 0],
15+
'info' => [21, 93, 252],
16+
'http' => [0, 166, 166],
17+
'verbose' => [0, 166, 0],
18+
'debug' => [0, 166, 0],
19+
'silly' => [80, 80, 80],
1920
];
2021

2122
public function log(string ...$data): void
@@ -39,17 +40,17 @@ private function internalLog(LogLevel $level, string $message): void
3940
$color[2],
4041
strtoupper($level->value) . ' '
4142
);
42-
43-
echo "{$datePrefix} {$levelPrefix}{$message}" . PHP_EOL;
43+
44+
file_put_contents(
45+
'php://stdout',
46+
"{$datePrefix} {$levelPrefix}{$message}" . PHP_EOL
47+
);
4448
}
45-
}
4649

47-
// Create a global instance function
48-
function getInternalLogger(): InternalLogger
49-
{
50-
static $instance = null;
51-
if ($instance === null) {
52-
$instance = new InternalLogger();
50+
public static function getInternalLogger(): InternalLogger
51+
{
52+
return self::$instance ??= new self();
5353
}
54-
return $instance;
5554
}
55+
56+
// Create a global instance function

src/Logger/Logger.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
namespace Logdash\Logger;
66

7-
use Logdash\LogLevel;
7+
use Logdash\Types\LogLevel;
88

99
class Logger
1010
{
1111
private const LOG_LEVEL_COLORS = [
12-
LogLevel::ERROR->value => [231, 0, 11],
13-
LogLevel::WARN->value => [254, 154, 0],
14-
LogLevel::INFO->value => [21, 93, 252],
15-
LogLevel::HTTP->value => [0, 166, 166],
16-
LogLevel::VERBOSE->value => [0, 166, 0],
17-
LogLevel::DEBUG->value => [0, 166, 0],
18-
LogLevel::SILLY->value => [80, 80, 80],
12+
'error' => [231, 0, 11],
13+
'warning' => [254, 154, 0],
14+
'info' => [21, 93, 252],
15+
'http' => [0, 166, 166],
16+
'verbose' => [0, 166, 0],
17+
'debug' => [0, 166, 0],
18+
'silly' => [80, 80, 80],
1919
];
2020

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

103-
$logMethod = $this->logMethod ?? function(string $msg): void {
104-
echo $msg . PHP_EOL;
103+
$logMethod = $this->logMethod ?? function (string $msg): void {
104+
file_put_contents(
105+
'php://stdout',
106+
$msg . PHP_EOL
107+
);
105108
};
106109

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

126+
/**
127+
* @param array<mixed|array|object> $data
128+
* @return array<string>
129+
*/
123130
private function convertToStrings(array $data): array
124131
{
125132
return array_map(function ($item): string {

src/Metrics/BaseMetrics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
interface BaseMetrics
88
{
99
public function set(string $key, float $value): void;
10-
10+
1111
public function mutate(string $key, float $value): void;
1212
}

src/Metrics/MetricOperation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Logdash\Metrics;
4+
5+
enum MetricOperation: string
6+
{
7+
case SET = 'set';
8+
case CHANGE = 'change';
9+
}

0 commit comments

Comments
 (0)