socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Dynamic `DEBUG` env variable

Open xgrommx opened this issue 3 years ago • 2 comments

Hello we can use DEBUG only during start application, but I need change it on the fly (during running application) Can I change DEBUG env variable via code?

xgrommx avatar Mar 23 '22 14:03 xgrommx

Every module has separated version of debug instance. I think will be better to have only one instance and expose enable/disable We can control debug on the fly (don't need to restart application, it isn't possible in my situation and enable/disable debug on the fly will be nice approach)

xgrommx avatar Mar 23 '22 19:03 xgrommx

Any advices @rauchg?

xgrommx avatar Mar 23 '22 22:03 xgrommx

Right before creating the server, you can import the debug package and call debug.enable("socket.io*,engine,socket.io*")

wvhulle avatar Feb 22 '23 16:02 wvhulle

debug logs can indeed be enabled on the fly:

import { Server } from "socket.io";
import debugPackage from "debug";

const io = new Server(3000);

io.on("connection", (socket) => {
  console.log(`connect ${socket.id}`);

  debugPackage.disable("socket*");
});

debugPackage.enable("socket*");

prints

  socket.io:server incoming connection with id 6t5_AorgjnvnZLzLAAAA +0ms
  socket.io-parser decoded 0 as {"type":0,"nsp":"/"} +0ms
  socket.io:client connecting to namespace / +0ms
  socket.io:namespace adding socket to nsp / +0ms
  socket.io:socket socket connected - writing packet +0ms
  socket.io:socket join room tYHxCafjaCT8hFpfAAAB +0ms
  socket.io-parser encoding packet {"type":0,"data":{"sid":"tYHxCafjaCT8hFpfAAAB"},"nsp":"/"} +2ms
  socket.io-parser encoded {"type":0,"data":{"sid":"tYHxCafjaCT8hFpfAAAB"},"nsp":"/"} as 0{"sid":"tYHxCafjaCT8hFpfAAAB"} +0ms
connect tYHxCafjaCT8hFpfAAAB
// and then nothing

Please reopen if needed.

darrachequesne avatar Jun 20 '23 14:06 darrachequesne