From 8c87ff02bbed977e9ba6086a53720c2eb1855521 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Wed, 19 Nov 2025 16:43:16 +0300 Subject: [PATCH 1/3] feat: Add Logger API for custom log interception - Add static setters: setDefaultDebugLog, setDefaultWarnLog, setDefaultErrorLog - Add static methods: enable, disable - Export Logger class from main module - Add TypeScript definitions for Logger class - Enables custom logging integration (e.g., Sentry, remote logging) Based on xcally/JsSIP#intercept-debug but applied to latest v3.10.1 This allows applications to intercept and redirect all JsSIP log output (debug, warn, error levels) to custom handlers for remote monitoring, analytics, or integration with logging services like Sentry. Example usage: import * as JsSIP from 'jssip'; JsSIP.Logger.setDefaultDebugLog((...args) => { console.debug(...args); sentryLog(args.join(' ')); }); JsSIP.Logger.enable('JsSIP:*'); --- lib/JsSIP.d.ts | 10 ++++++++-- lib/JsSIP.js | 18 +++++++++++++---- lib/Logger.d.ts | 36 ++++++++++++++++++++++++++++++++++ lib/Logger.js | 51 ++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 lib/Logger.d.ts diff --git a/lib/JsSIP.d.ts b/lib/JsSIP.d.ts index eee914d93..db198228f 100644 --- a/lib/JsSIP.d.ts +++ b/lib/JsSIP.d.ts @@ -1,4 +1,4 @@ -import {Debug} from 'debug' +export {Logger} from './Logger' import * as C from './Constants' import * as Exceptions from './Exceptions' @@ -12,6 +12,12 @@ export {URI} from './URI' export {NameAddrHeader} from './NameAddrHeader' export {WebSocketInterface, Socket, WeightedSocket} from './WebSocketInterface' -export const debug: Debug export const name: string export const version: string +/** + * @deprecated debug should not be used, use Logger instead + */ +export const debug: { + enable: (...namespaces?:string)=> void + disable: ()=> void +} diff --git a/lib/JsSIP.js b/lib/JsSIP.js index bb9fa2499..4de4abd54 100644 --- a/lib/JsSIP.js +++ b/lib/JsSIP.js @@ -7,10 +7,12 @@ const URI = require('./URI'); const NameAddrHeader = require('./NameAddrHeader'); const Grammar = require('./Grammar'); const WebSocketInterface = require('./WebSocketInterface'); -const debug = require('debug')('JsSIP'); +const Logger = require('./Logger'); const RTCSession = require('./RTCSession'); -debug('version %s', pkg.version); +const logger = new Logger('JsSIP'); + +logger.debug('version %s', pkg.version); /** * Expose the JsSIP module. @@ -25,8 +27,16 @@ module.exports = { WebSocketInterface, Grammar, RTCSession, - // Expose the debug module. - debug : require('debug'), + // Expose the Logger module (for its static methods) + Logger, + + /** + * @deprecated debug should not be used, use Logger instead + */ + debug : { + enable : Logger.enable, + disable : Logger.disable + }, get name() { return pkg.title; }, get version() { return pkg.version; } }; diff --git a/lib/Logger.d.ts b/lib/Logger.d.ts new file mode 100644 index 000000000..f13601f7e --- /dev/null +++ b/lib/Logger.d.ts @@ -0,0 +1,36 @@ +type LoggerFn = (...any:[]) => void + +export class Logger{ + /** + * Static setter for debug logger + */ + static setDefaultDebugLog(loggerFn: LoggerFn): void + + /** + * Static setter for warn logger + */ + static setDefaultWarnLog(loggerFn: LoggerFn): void + + /** + * Static setter for error logger + */ + static setDefaultErrorLog(loggerFn: LoggerFn): void + + /** + * Enable debug for namespaces (no namespace = all) + */ + static enable(namespaces: string): void + + /** + * Disable debug + */ + static disable(): void + + constructor(prefix: string) + + get debug():void + + get warn():void + + get error():void +} diff --git a/lib/Logger.js b/lib/Logger.js index 4feeceb11..e13cc7bc4 100644 --- a/lib/Logger.js +++ b/lib/Logger.js @@ -2,8 +2,49 @@ const debug = require('debug'); const APP_NAME = 'JsSIP'; +/* eslint-disable no-console */ +let defaultDebugLog = console.info.bind(console); +let defaultWarnLog = console.warn.bind(console); +let defaultErrorLog = console.error.bind(console); +/* eslint-enable no-console */ + + module.exports = class Logger { + + /** + * Static setter for debug logger + * @param {(...any:[]) => void} loggerFn + */ + static setDefaultDebugLog(loggerFn) { defaultDebugLog = loggerFn; } + + /** + * Static setter for warn logger + * @param {(...any:[]) => void} loggerFn + */ + static setDefaultWarnLog(loggerFn) { defaultWarnLog = loggerFn; } + + /** + * Static setter for error logger + * @param {(...any:[]) => void} loggerFn + */ + static setDefaultErrorLog(loggerFn) { defaultErrorLog = loggerFn; } + + /** + * Enable debug for namespaces (no namespace = all) + * @param {string} [namespaces] optional + */ + static enable(namespaces) { debug.enable(namespaces); } + + /** + * Disable debug + */ + static disable() { debug.disable(); } + + /** + * + * @param {string} prefix namespace prefix + */ constructor(prefix) { if (prefix) @@ -18,11 +59,11 @@ module.exports = class Logger this._warn = debug.default(`${APP_NAME}:WARN`); this._error = debug.default(`${APP_NAME}:ERROR`); } - /* eslint-disable no-console */ - this._debug.log = console.info.bind(console); - this._warn.log = console.warn.bind(console); - this._error.log = console.error.bind(console); - /* eslint-enable no-console */ + + this._debug.log = (...args) => defaultDebugLog(...args); + this._warn.log = (...args) => defaultWarnLog(...args); + this._error.log = (...args) => defaultErrorLog(...args); + } get debug() From a9827f2f57043fd42467102dccb0ba7ccec55112 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Wed, 19 Nov 2025 16:44:56 +0300 Subject: [PATCH 2/3] fix: Correct TypeScript exports in JsSIP.d.ts - Import Socket and WeightedSocket from './Socket' not './WebSocketInterface' - Fix debug.enable signature: use (namespaces: string) instead of (...namespaces?:string) --- lib/JsSIP.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/JsSIP.d.ts b/lib/JsSIP.d.ts index db198228f..39382c18c 100644 --- a/lib/JsSIP.d.ts +++ b/lib/JsSIP.d.ts @@ -10,7 +10,8 @@ export { C, Exceptions, Grammar, Utils }; export {UA} from './UA' export {URI} from './URI' export {NameAddrHeader} from './NameAddrHeader' -export {WebSocketInterface, Socket, WeightedSocket} from './WebSocketInterface' +export {WebSocketInterface} from './WebSocketInterface' +export {Socket, WeightedSocket} from './Socket' export const name: string export const version: string @@ -18,6 +19,6 @@ export const version: string * @deprecated debug should not be used, use Logger instead */ export const debug: { - enable: (...namespaces?:string)=> void + enable: (namespaces: string)=> void disable: ()=> void } From 3c398e1bc894c6d291920a3355408f8dcef434a0 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Wed, 19 Nov 2025 17:54:30 +0300 Subject: [PATCH 3/3] fix: Update main entry point to lib/JsSIP.js The lib-es5 directory is only created during npm publish via the prepublishOnly script. For development installs from GitHub, the source files in lib/ should be used directly. This allows Metro bundler to resolve the module correctly. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4e65305a1..873c5f8e7 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "IƱaki Baz Castillo (https://inakibaz.me)" ], "types": "lib/JsSIP.d.ts", - "main": "lib-es5/JsSIP.js", + "main": "lib/JsSIP.js", "keywords": [ "sip", "websocket",