class SimplePsrFileLogger
Hierarchy
- class \OpenTelemetry\SDK\Logs\SimplePsrFileLogger implements \Psr\Log\LoggerInterface uses \Psr\Log\LoggerTrait
Expanded class hierarchy of SimplePsrFileLogger
File
-
vendor/
open-telemetry/ sdk/ Logs/ SimplePsrFileLogger.php, line 14
Namespace
OpenTelemetry\SDK\LogsView source
class SimplePsrFileLogger implements LoggerInterface {
use LoggerTrait;
private const DEFAULT_LOGGER_NAME = 'otel';
private static ?array $logLevels = null;
public function __construct(string $filename, string $loggerName = self::DEFAULT_LOGGER_NAME) {
}
/**
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function log($level, $message, array $context = []) : void {
$level = strtolower((string) $level);
if (!in_array($level, self::getLogLevels(), true)) {
throw new InvalidArgumentException(sprintf('Invalid Log level: "%s"', $level));
}
file_put_contents($this->filename, $this->formatLog((string) $level, (string) $message, $context), FILE_APPEND);
}
/**
* @return string
*/
private function formatLog(string $level, string $message, array $context = []) : string {
try {
$encodedContext = json_encode($context, JSON_THROW_ON_ERROR);
} catch (Throwable $t) {
$encodedContext = sprintf('(Could not encode context: %s)', $t->getMessage());
}
return sprintf('[%s] %s %s: %s %s%s', date(DATE_RFC3339_EXTENDED), $this->loggerName, $level, $message, $encodedContext, PHP_EOL);
}
/**
* @return array
*/
private static function getLogLevels() : array {
return self::$logLevels ?? (self::$logLevels = (new ReflectionClass(LogLevel::class))->getConstants());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
LoggerTrait::alert | public | function | Action must be taken immediately. | |
LoggerTrait::critical | public | function | Critical conditions. | |
LoggerTrait::debug | public | function | Detailed debug information. | |
LoggerTrait::emergency | public | function | System is unusable. | |
LoggerTrait::error | public | function | Runtime errors that do not require immediate action but should typically be logged and monitored. |
|
LoggerTrait::info | public | function | Interesting events. | |
LoggerTrait::notice | public | function | Normal but significant events. | |
LoggerTrait::warning | public | function | Exceptional occurrences that are not errors. | |
SimplePsrFileLogger::$logLevels | private static | property | ||
SimplePsrFileLogger::DEFAULT_LOGGER_NAME | private | constant | ||
SimplePsrFileLogger::formatLog | private | function | ||
SimplePsrFileLogger::getLogLevels | private static | function | ||
SimplePsrFileLogger::log | public | function | @psalm-suppress MoreSpecificImplementedParamType | Overrides LoggerTrait::log |
SimplePsrFileLogger::__construct | public | function |