Skip to content

Commit 6e4d973

Browse files
Provide backwards-compatible Psr3 loggers when running under composer.
1 parent e3005fb commit 6e4d973

File tree

10 files changed

+176
-50
lines changed

10 files changed

+176
-50
lines changed

.phan/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
// This is useful for excluding hopelessly unanalyzable
305305
// files that can't be removed for whatever reason.
306306
'exclude_file_list' => [
307+
'src/API/Common/Compatibility/Psr3.php',
307308
'vendor/composer/composer/src/Composer/InstalledVersions.php',
308309
],
309310

.php-cs-fixer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
$finder = PhpCsFixer\Finder::create()
66
->in('examples/')
77
->in('tests/')
8-
->in('src/');
8+
->in('src/')
9+
->exclude('src/API/Common/Compatibility');
910

1011
$config = new PhpCsFixer\Config();
1112

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
},
6464
"files": [
6565
"src/Context/fiber/initialize_fiber_handler.php",
66+
"src/API/_bootstrap.php",
6667
"src/API/Trace/functions.php",
6768
"src/Contrib/Otlp/_register.php",
6869
"src/Contrib/Grpc/_register.php",

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ parameters:
1010
- ./tests
1111
# - ./examples TODO: Uncomment this once examples are updated
1212
excludePaths:
13+
- src/API/Common/Compatability
1314
- tests/TraceContext/W3CTestService
1415
- tests/Unit/Config/SDK/Configuration/ExampleSdk
1516
ignoreErrors:

psalm.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<directory name="./examples"/>
1515
<ignoreFiles>
1616
<directory name="./examples/traces/demo/"/>
17+
<directory name="src/API/Common/Compatibility/"/>
1718
<directory name="tests/Unit/Config/SDK/Configuration/ExampleSdk"/>
1819
<directory name="tests/TraceContext/W3CTestService"/>
1920
<directory name="vendor"/>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/php-fig/log/blob/1.0.0/Psr/Log/AbstractLogger.php
5+
* @see https://github.com/php-fig/log/blob/1.0.0/Psr/Log/NullLogger.php
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Psr\Log;
11+
12+
/**
13+
* This is a simple Logger implementation that other Loggers can inherit from.
14+
*
15+
* It simply delegates all log-level-specific methods to the `log` method to
16+
* reduce boilerplate code that a simple Logger that does the same thing with
17+
* messages regardless of the error level has to implement.
18+
*/
19+
abstract class AbstractLogger implements LoggerInterface
20+
{
21+
/**
22+
* System is unusable.
23+
*
24+
* @param string $message
25+
* @param array $context
26+
* @return null
27+
*/
28+
public function emergency($message, array $context = [])
29+
{
30+
$this->log(LogLevel::EMERGENCY, $message, $context);
31+
}
32+
33+
/**
34+
* Action must be taken immediately.
35+
*
36+
* Example: Entire website down, database unavailable, etc. This should
37+
* trigger the SMS alerts and wake you up.
38+
*
39+
* @param string $message
40+
* @param array $context
41+
* @return null
42+
*/
43+
public function alert($message, array $context = [])
44+
{
45+
$this->log(LogLevel::ALERT, $message, $context);
46+
}
47+
48+
/**
49+
* Critical conditions.
50+
*
51+
* Example: Application component unavailable, unexpected exception.
52+
*
53+
* @param string $message
54+
* @param array $context
55+
* @return null
56+
*/
57+
public function critical($message, array $context = [])
58+
{
59+
$this->log(LogLevel::CRITICAL, $message, $context);
60+
}
61+
62+
/**
63+
* Runtime errors that do not require immediate action but should typically
64+
* be logged and monitored.
65+
*
66+
* @param string $message
67+
* @param array $context
68+
* @return null
69+
*/
70+
public function error($message, array $context = [])
71+
{
72+
$this->log(LogLevel::ERROR, $message, $context);
73+
}
74+
75+
/**
76+
* Exceptional occurrences that are not errors.
77+
*
78+
* Example: Use of deprecated APIs, poor use of an API, undesirable things
79+
* that are not necessarily wrong.
80+
*
81+
* @param string $message
82+
* @param array $context
83+
* @return null
84+
*/
85+
public function warning($message, array $context = [])
86+
{
87+
$this->log(LogLevel::WARNING, $message, $context);
88+
}
89+
90+
/**
91+
* Normal but significant events.
92+
*
93+
* @param string $message
94+
* @param array $context
95+
* @return null
96+
*/
97+
public function notice($message, array $context = [])
98+
{
99+
$this->log(LogLevel::NOTICE, $message, $context);
100+
}
101+
102+
/**
103+
* Interesting events.
104+
*
105+
* Example: User logs in, SQL logs.
106+
*
107+
* @param string $message
108+
* @param array $context
109+
* @return null
110+
*/
111+
public function info($message, array $context = [])
112+
{
113+
$this->log(LogLevel::INFO, $message, $context);
114+
}
115+
116+
/**
117+
* Detailed debug information.
118+
*
119+
* @param string $message
120+
* @param array $context
121+
* @return null
122+
*/
123+
public function debug($message, array $context = [])
124+
{
125+
$this->log(LogLevel::DEBUG, $message, $context);
126+
}
127+
}
128+
129+
/**
130+
* This Logger can be used to avoid conditional log calls
131+
*
132+
* Logging should always be optional, and if no logger is provided to your
133+
* library creating a NullLogger instance to have something to throw logs at
134+
* is a good way to avoid littering your code with `if ($this->logger) { }`
135+
* blocks.
136+
*/
137+
class NullLogger extends AbstractLogger
138+
{
139+
/**
140+
* Logs with an arbitrary level.
141+
*
142+
* @param mixed $level
143+
* @param string $message
144+
* @param array $context
145+
* @return null
146+
*/
147+
public function log($level, $message, array $context = [])
148+
{
149+
// noop
150+
}
151+
}

src/API/_bootstrap.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API;
6+
7+
use function class_exists;
8+
use Composer\InstalledVersions;
9+
use Composer\Semver\VersionParser;
10+
11+
if (
12+
// Provide a backwards-compatible Psr3 implementation when running under composer.
13+
class_exists(InstalledVersions::class, false)
14+
&& InstalledVersions::satisfies(new VersionParser(), 'composer/composer', '~2.0')
15+
) {
16+
require_once __DIR__ . '/Common/Compatibility/Psr3.php';
17+
}

src/API/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"OpenTelemetry\\API\\": "."
3131
},
3232
"files": [
33+
"_bootstrap.php",
3334
"Trace/functions.php"
3435
]
3536
},

src/SDK/Common/Util/ComposerHandler.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/SDK/_autoload.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,4 @@
22

33
declare(strict_types=1);
44

5-
/**
6-
* If OTEL_PHP_AUTOLOAD_ENABLED=true, there may be compatability issues
7-
* if the version of PSR-3 installed in ./vendor conflicts with that of the
8-
* packaged composer PSR-3 library.
9-
*
10-
* If COMPOSER_DEV_MODE is present, then we can assume that a composer script
11-
* is running, and we can prevent the PSR-3 compatability issues by disabling
12-
* the SDK from activating.
13-
*
14-
* @see https://github.com/open-telemetry/opentelemetry-php/issues/1673
15-
*/
16-
if (\OpenTelemetry\SDK\Common\Util\ComposerHandler::isRunning() === false) {
17-
\OpenTelemetry\SDK\SdkAutoloader::autoload();
18-
}
5+
\OpenTelemetry\SDK\SdkAutoloader::autoload();

0 commit comments

Comments
 (0)