LogRecord
Value object representing a log record.
LogRecord is an immutable data container for log information including severity, body, attributes, timestamp, and exception details.
All setter methods return a new instance for fluent configuration.
Example usage:
$record = (new LogRecord())
->setSeverity(Severity::ERROR)
->setBody('Database connection failed')
->setAttribute('db.host', 'localhost')
->setAttribute('db.port', 5432)
->setException($exception);
$logger->emit($record);
Properties
- $attributes : Attributes
- $body : string
- $observedTimestamp : DateTimeImmutable|null
- $severity : Severity
- $timestamp : DateTimeImmutable|null
Methods
- __construct() : mixed
- fromArray() : self
- Create a LogRecord from a normalized array representation.
-
normalize()
: array{severity: int, body: string, attributes: array
, timestamp: null|string, observedTimestamp: null|string} - Normalize the LogRecord to an array representation for serialization.
- setAttribute() : self
- Set an attribute on the log record.
- setAttributes() : self
- Set multiple attributes at once.
- setBody() : self
- Set the log body (message).
- setException() : self
- Record an exception in this log record.
- setObservedTimestamp() : self
- Set the observed timestamp for this log record.
- setSeverity() : self
- Set the severity level.
- setTimestamp() : self
- Set the timestamp for this log record.
Properties
$attributes
public
Attributes
$attributes
= new Attributes()
$body
public
string
$body
= ''
$observedTimestamp
public
DateTimeImmutable|null
$observedTimestamp
= null
$severity
public
Severity
$severity
= Severity::INFO
$timestamp
public
DateTimeImmutable|null
$timestamp
= null
Methods
__construct()
public
__construct([Severity $severity = Severity::INFO ][, string $body = '' ][, Attributes $attributes = new Attributes() ][, null|DateTimeImmutable $timestamp = null ][, null|DateTimeImmutable $observedTimestamp = null ]) : mixed
Parameters
- $severity : Severity = Severity::INFO
-
The severity level
- $body : string = ''
-
The log message body
- $attributes : Attributes = new Attributes()
-
Log record attributes
- $timestamp : null|DateTimeImmutable = null
-
When the event occurred
- $observedTimestamp : null|DateTimeImmutable = null
-
When the log was observed by collection
fromArray()
Create a LogRecord from a normalized array representation.
public
static fromArray(array{severity: int, body: string, attributes: array, timestamp: null|string, observedTimestamp: null|string} $data) : self
Parameters
-
$data
: array{severity: int, body: string, attributes: array
, timestamp: null|string, observedTimestamp: null|string} -
Normalized LogRecord data
Return values
selfnormalize()
Normalize the LogRecord to an array representation for serialization.
public
normalize() : array{severity: int, body: string, attributes: array, timestamp: null|string, observedTimestamp: null|string}
Return values
array{severity: int, body: string, attributes: arraysetAttribute()
Set an attribute on the log record.
public
setAttribute(string $key, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable $value) : self
Attributes provide additional context about the log event.
Parameters
- $key : string
-
Attribute key
- $value : array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable
-
Attribute value
Return values
self —New instance with attribute set
setAttributes()
Set multiple attributes at once.
public
setAttributes(array<string, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable> $attributes) : self
Parameters
- $attributes : array<string, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable>
-
Key-value attribute pairs
Return values
self —New instance with attributes set
setBody()
Set the log body (message).
public
setBody(string $body) : self
The body is the primary content of the log record.
Parameters
- $body : string
-
The log message
Return values
self —New instance with body set
setException()
Record an exception in this log record.
public
setException(Throwable $exception) : self
This is a convenience method that sets appropriate attributes for the exception (type, message, stacktrace).
Parameters
- $exception : Throwable
-
The exception to record
Return values
self —New instance with exception attributes set
setObservedTimestamp()
Set the observed timestamp for this log record.
public
setObservedTimestamp(DateTimeImmutable $observedTimestamp) : self
The observed timestamp is when the log record was observed by the collection system. This is typically set by the Logger or LogProcessor, not by the application code.
Parameters
- $observedTimestamp : DateTimeImmutable
-
When the log was observed
Return values
self —New instance with observed timestamp set
setSeverity()
Set the severity level.
public
setSeverity(Severity $severity) : self
Parameters
- $severity : Severity
-
The severity level
Return values
self —New instance with severity set
setTimestamp()
Set the timestamp for this log record.
public
setTimestamp(DateTimeImmutable $timestamp) : self
The timestamp represents when the event occurred. If not set, Logger implementations should use the current time.
Parameters
- $timestamp : DateTimeImmutable
-
When the event occurred
Return values
self —New instance with timestamp set