1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php
declare(strict_types=1);
namespace OpenTelemetry\API\Behavior\Internal;
use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface;
use Psr\Log\LogLevel;
/**
* Logging utility functions for internal logging (of OpenTelemetry errors/warnings etc).
* This is not part of SDK configuration to avoid creating a dependency on SDK from any package which does logging.
* @todo this should be `@internal`, but deptrac is not happy with that.
*/
class Logging
{
private const OTEL_LOG_LEVEL = 'OTEL_LOG_LEVEL';
private const DEFAULT_LEVEL = LogLevel::INFO;
private const NONE = 'none';
private const LEVELS = [
LogLevel::DEBUG,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
LogLevel::ERROR,
LogLevel::CRITICAL,
LogLevel::ALERT,
LogLevel::EMERGENCY,
self::NONE, //highest priority so that nothing is logged
];
/**
* The minimum log level. Messages with lower severity than this will be ignored.
*/
private static ?int $logLevel = null;
private static ?LogWriterInterface $writer = null;
public static function setLogWriter(LogWriterInterface $writer): void
{
self::$writer = $writer;
}
public static function logWriter(): LogWriterInterface
{
self::$writer ??= (new LogWriterFactory())->create();
return self::$writer;
}
/**
* Get level priority from level name
*/
public static function level(string $level): int
{
$value = array_search($level, self::LEVELS);
return $value ?: 1; //'info'
}
/**
* Get defined OTEL_LOG_LEVEL, or default
*/
public static function logLevel(): int
{
self::$logLevel ??= self::getLogLevel();
return self::$logLevel;
}
private static function getLogLevel(): int
{
$level = array_key_exists(self::OTEL_LOG_LEVEL, $_SERVER)
? $_SERVER[self::OTEL_LOG_LEVEL]
: getenv(self::OTEL_LOG_LEVEL);
if (!$level) {
$level = ini_get(self::OTEL_LOG_LEVEL);
}
if (!$level) {
$level = self::DEFAULT_LEVEL;
}
return self::level($level);
}
public static function reset(): void
{
self::$logLevel = null;
self::$writer = null;
}
}
|