122 throw new LogicException(
123 'Missing stream uri, the stream can not be opened.' );
126 set_error_handler( [ $this,
'errorTrap' ] );
128 if ( str_starts_with( $this->uri,
'udp:' ) ) {
129 $parsed = parse_url( $this->uri );
130 if ( !isset( $parsed[
'host'] ) ) {
131 throw new UnexpectedValueException( sprintf(
132 'Udp transport "%s" must specify a host', $this->uri
135 if ( !isset( $parsed[
'port'] ) ) {
136 throw new UnexpectedValueException( sprintf(
137 'Udp transport "%s" must specify a port', $this->uri
141 $this->host = $parsed[
'host'];
142 $this->port = $parsed[
'port'];
145 if ( isset( $parsed[
'path'] ) ) {
146 $this->prefix = ltrim( $parsed[
'path'],
'/' );
149 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
156 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
159 $this->sink = fopen( $this->uri,
'a' );
161 restore_error_handler();
163 if ( !$this->sink ) {
165 throw new UnexpectedValueException( sprintf(
166 'The stream or file "%s" could not be opened: %s',
168 $this->uri, $this->error
190 protected function write( array $record ): void {
191 if ( $this->useLegacyFilter &&
193 $record[
'channel'], $record[
'message'],
194 $record[
'level'], $record
203 if ( $this->sink ===
null ) {
207 $text = (string)$record[
'formatted'];
210 if ( $this->prefix !==
'' ) {
211 $leader = ( $this->prefix ===
'{channel}' ) ?
212 $record[
'channel'] : $this->prefix;
213 $text = preg_replace(
'/^/m',
"{$leader} ", $text );
216 if ( strlen( $text ) > 65506 ) {
217 $text = substr( $text, 0, 65506 );
220 if ( !str_ends_with( $text,
"\n" ) ) {
224 } elseif ( strlen( $text ) > 65507 ) {
225 $text = substr( $text, 0, 65507 );
238 fwrite( $this->sink, $text );