112 throw new LogicException(
113 'Missing stream uri, the stream can not be opened.' );
116 set_error_handler( [ $this,
'errorTrap' ] );
118 if ( substr( $this->uri, 0, 4 ) ==
'udp:' ) {
119 $parsed = parse_url( $this->uri );
120 if ( !isset( $parsed[
'host'] ) ) {
121 throw new UnexpectedValueException( sprintf(
122 'Udp transport "%s" must specify a host', $this->uri
125 if ( !isset( $parsed[
'port'] ) ) {
126 throw new UnexpectedValueException( sprintf(
127 'Udp transport "%s" must specify a port', $this->uri
131 $this->host = $parsed[
'host'];
132 $this->port = $parsed[
'port'];
135 if ( isset( $parsed[
'path'] ) ) {
136 $this->prefix = ltrim( $parsed[
'path'],
'/' );
139 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
146 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
149 $this->sink = fopen( $this->uri,
'a' );
151 restore_error_handler();
153 if ( !$this->sink ) {
155 throw new UnexpectedValueException( sprintf(
156 'The stream or file "%s" could not be opened: %s',
158 $this->uri, $this->error
180 protected function write( array $record ): void {
181 if ( $this->useLegacyFilter &&
183 $record[
'channel'], $record[
'message'],
184 $record[
'level'], $record
193 if ( $this->sink ===
null ) {
197 $text = (string)$record[
'formatted'];
200 if ( $this->prefix !==
'' ) {
201 $leader = ( $this->prefix ===
'{channel}' ) ?
202 $record[
'channel'] : $this->prefix;
203 $text = preg_replace(
'/^/m',
"{$leader} ", $text );
206 if ( strlen( $text ) > 65506 ) {
207 $text = substr( $text, 0, 65506 );
210 if ( substr( $text, -1 ) !=
"\n" ) {
214 } elseif ( strlen( $text ) > 65507 ) {
215 $text = substr( $text, 0, 65507 );
228 fwrite( $this->sink, $text );