WebSocket
extends DestinationBase
in package
Outputs log data to a WebSocket connection.
Maintains a persistent connection and automatically reconnects if disconnected.
This is a simple implementation using raw sockets with WebSocket protocol handshake.
Table of Contents
Properties
- $_filters : array<string|int, mixed>
- $_format : IFormat
- $_parent : ILogger|null
- $_stdErr : mixed
- $_stdOut : mixed
- $host : string
- $isConnected : bool
- $maxReconnectAttempts : int
- $path : string
- $port : int
- $reconnectAttempts : int
- $reconnectDelay : float
- $socket : mixed
Methods
- __construct() : mixed
- __destruct() : mixed
- Close the WebSocket connection.
- addFilter() : bool
- Adds a logging filter.
- close() : void
- Closes the destination if required.
- getParent() : ILogger
- Gets the parent logger.
- getStdErr() : mixed
- getStdOut() : mixed
- log() : void
- Handles writing the log data after filtering and formatting.
- open() : bool
- Configure the WebSocket connection.
- removeFilter() : bool
- Removes a logging filter.
- setFileHandles() : void
- Maps STDERR and STDOUT to file handles in non-CLI environments.
- setFormat() : void
- Sets the formatter.
- setParent() : void
- Sets the parent logger.
- write() : void
- Write log data to the WebSocket connection.
- connect() : bool
- Connect to the WebSocket server and perform handshake.
- createFrame() : string
- Create a WebSocket frame for the message.
- performHandshake() : bool
- Perform WebSocket handshake.
- reconnect() : bool
- Attempt to reconnect to the WebSocket server.
Properties
$_filters
private
array<string|int, mixed>
$_filters
= []
$_format
private
IFormat
$_format
$_parent
private
ILogger|null
$_parent
= null
$_stdErr
private
mixed
$_stdErr
$_stdOut
private
mixed
$_stdOut
$host
private
string
$host
$isConnected
private
bool
$isConnected
= false
$maxReconnectAttempts
private
int
$maxReconnectAttempts
= 5
$path
private
string
$path
$port
private
int
$port
$reconnectAttempts
private
int
$reconnectAttempts
= 0
$reconnectDelay
private
float
$reconnectDelay
= 1.0
$socket
private
mixed
$socket
= null
Methods
__construct()
public
__construct(IFormat $format) : mixed
Parameters
- $format : IFormat
__destruct()
Close the WebSocket connection.
public
__destruct() : mixed
addFilter()
Adds a logging filter.
public
addFilter(IFilter $filter) : bool
Parameters
- $filter : IFilter
Return values
boolclose()
Closes the destination if required.
public
close() : void
getParent()
Gets the parent logger.
public
getParent() : ILogger
Return values
ILoggergetStdErr()
public
getStdErr() : mixed
getStdOut()
public
getStdOut() : mixed
log()
Handles writing the log data after filtering and formatting.
public
log(mixed $text, mixed $level[, mixed $context = [] ]) : void
Parameters
- $text : mixed
-
- Output that has been run through the formatter.
- $level : mixed
-
- Text output level.
- $context : mixed = []
-
- Optional context array
open()
Configure the WebSocket connection.
public
open(array<string|int, mixed> $params) : bool
Parameters: url - WebSocket URL (ws://host:port/path)
Parameters
- $params : array<string|int, mixed>
Return values
boolremoveFilter()
Removes a logging filter.
public
removeFilter(IFilter $removeFilter) : bool
Parameters
- $removeFilter : IFilter
Return values
boolsetFileHandles()
Maps STDERR and STDOUT to file handles in non-CLI environments.
public
setFileHandles() : void
setFormat()
Sets the formatter.
public
setFormat(IFormat $format) : void
Parameters
- $format : IFormat
setParent()
Sets the parent logger.
public
setParent(ILogger $logger) : void
Parameters
- $logger : ILogger
write()
Write log data to the WebSocket connection.
public
write(string $text, Data $data) : void
Parameters
- $text : string
- $data : Data
connect()
Connect to the WebSocket server and perform handshake.
private
connect() : bool
Return values
boolcreateFrame()
Create a WebSocket frame for the message.
private
createFrame(string $message) : string
Parameters
- $message : string
Return values
stringperformHandshake()
Perform WebSocket handshake.
private
performHandshake() : bool
Return values
boolreconnect()
Attempt to reconnect to the WebSocket server.
private
reconnect() : bool