25 use Monolog\Handler\AbstractProcessingHandler;
27 use UnexpectedValueException;
99 $level = Logger::DEBUG,
102 parent::__construct( $level, $bubble );
103 $this->uri = $stream;
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 ( !is_resource( $this->sink ) ) {
155 throw new UnexpectedValueException( sprintf(
156 'The stream or file "%s" could not be opened: %s',
157 $this->uri, $this->
error
176 return $this->host !==
null;
180 if ( $this->useLegacyFilter &&
182 $record[
'channel'], $record[
'message'],
183 $record[
'level'], $record
192 if ( $this->sink ===
null ) {
196 $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 );
219 $this->sink, $text, strlen( $text ), 0, $this->host, $this->
port
223 fwrite( $this->sink, $text );
228 if ( is_resource( $this->sink ) ) {
230 socket_close( $this->sink );
233 fclose( $this->sink );