111 throw new LogicException(
112 'Missing stream uri, the stream can not be opened.' );
115 set_error_handler( [ $this,
'errorTrap' ] );
117 if ( substr( $this->uri, 0, 4 ) ==
'udp:' ) {
118 $parsed = parse_url( $this->uri );
119 if ( !isset( $parsed[
'host'] ) ) {
120 throw new UnexpectedValueException( sprintf(
121 'Udp transport "%s" must specify a host', $this->uri
124 if ( !isset( $parsed[
'port'] ) ) {
125 throw new UnexpectedValueException( sprintf(
126 'Udp transport "%s" must specify a port', $this->uri
130 $this->host = $parsed[
'host'];
131 $this->port = $parsed[
'port'];
134 if ( isset( $parsed[
'path'] ) ) {
135 $this->prefix = ltrim( $parsed[
'path'],
'/' );
138 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
145 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
148 $this->sink = fopen( $this->uri,
'a' );
150 restore_error_handler();
152 if ( !is_resource( $this->sink ) ) {
154 throw new UnexpectedValueException( sprintf(
155 'The stream or file "%s" could not be opened: %s',
156 $this->uri, $this->error
178 protected function write( array $record ) {
179 if ( $this->useLegacyFilter &&
181 $record[
'channel'], $record[
'message'],
182 $record[
'level'], $record
191 if ( $this->sink ===
null ) {
195 $text = (string)$record[
'formatted'];
198 if ( $this->prefix !==
'' ) {
199 $leader = ( $this->prefix ===
'{channel}' ) ?
201 $text = preg_replace(
'/^/m',
"{$leader} ", $text );
204 if ( strlen( $text ) > 65506 ) {
205 $text = substr( $text, 0, 65506 );
208 if ( substr( $text, -1 ) !=
"\n" ) {
212 } elseif ( strlen( $text ) > 65507 ) {
213 $text = substr( $text, 0, 65507 );
217 $this->sink, $text, strlen( $text ), 0, $this->host, $this->port
221 fwrite( $this->sink, $text );