pablosanches / php-websocket-logger
PSR3 LoggerInterface implementation with websocket support for real-time log monitoring.
v1.0.1
2021-08-27 12:06 UTC
Requires
- php: ^5.6|^7.0
- psr/log: ^1.0
- wisembly/elephant.io: ^3.3
This package is auto-updated.
Last update: 2024-12-27 19:20:06 UTC
README
PSR3 LoggerInterface implementation with websocket support for real-time log monitoring.
Using
composer require pablosanches/php-websocket-logger
PHP
<?php require __DIR__ . '/vendor/autoload.php'; use PabloSanches\Logger; Logger::info('Message info here - {variable}', array('variable' => 'changed')); Logger::critical('Message critical here - {variable}', array('variable' => 'changed'));
NODEJS
const server = require('http').createServer(); const io = require('socket.io')(server); const logger = require('winston'); const port = 1337; const token = 'PABLO-TOKEN'; // Logger config logger.remove(logger.transports.Console); logger.add(logger.transports.Console, { colorize: true, timestamp: true }); logger.info('SocketIO > listening on port ' + port); // set up initialization and authorization method io.use(function (socket, next) { let auth = socket.request.headers.authorization; if(auth) { const token = auth.replace("Bearer ", ""); logger.info("auth token", token); return next(); } else { return next(new Error("no authorization header")); } }); io.on('connection', function (socket){ logger.info('SocketIO > Connected socket ' + socket.id); logger.info("X-My-Header", socket.handshake.headers['x-my-header']); socket.on('logger_emmiter', function (params) { if (params['token'] !== token) { logger.error('Invalid token!'); } else { logger.info('Received a log event. Log type:' + params['level']); logger.info('LOG Message: ' + params['message']); logger.info('LOG Token: ' + params['token']); } }); socket.on('disconnect', function () { logger.info('SocketIO > Disconnected socket ' + socket.id); }); }); server.listen(port);