108 throw new LogicException(
109 'Missing stream uri, the stream can not be opened.' );
112 set_error_handler( $this->
errorTrap( ... ) );
114 if ( str_starts_with( $this->uri,
'udp:' ) ) {
115 $parsed = parse_url( $this->uri );
116 if ( !isset( $parsed[
'host'] ) ) {
117 throw new UnexpectedValueException( sprintf(
118 'Udp transport "%s" must specify a host', $this->uri
121 if ( !isset( $parsed[
'port'] ) ) {
122 throw new UnexpectedValueException( sprintf(
123 'Udp transport "%s" must specify a port', $this->uri
127 $this->host = $parsed[
'host'];
128 $this->port = $parsed[
'port'];
131 if ( isset( $parsed[
'path'] ) ) {
132 $this->prefix = ltrim( $parsed[
'path'],
'/' );
135 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
142 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
145 $this->sink = fopen( $this->uri,
'a' );
147 restore_error_handler();
149 if ( !$this->sink ) {
151 throw new UnexpectedValueException( sprintf(
152 'The stream or file "%s" could not be opened: %s',
154 $this->uri, $this->error
176 protected function write( array $record ): void {
177 if ( $this->useLegacyFilter &&
179 $record[
'channel'], $record[
'message'],
180 $record[
'level'], $record
189 if ( $this->sink ===
null ) {
193 $text = (string)$record[
'formatted'];
196 if ( $this->prefix !==
'' ) {
197 $leader = ( $this->prefix ===
'{channel}' ) ?
198 $record[
'channel'] : $this->prefix;
199 $text = preg_replace(
'/^/m',
"{$leader} ", $text );
202 if ( strlen( $text ) > 65506 ) {
203 $text = substr( $text, 0, 65506 );
206 if ( !str_ends_with( $text,
"\n" ) ) {
210 } elseif ( strlen( $text ) > 65507 ) {
211 $text = substr( $text, 0, 65507 );
224 fwrite( $this->sink, $text );