captureConsole

Patches the global console.log, console.warn, console.error, and console.debug functions so every call is also forwarded to the Cairn logger.


Import

import { captureConsole } from '@mpratyush54/sdk-node';

Or via the client instance:

client.captureConsole();

Signature

captureConsole(logger: LoggerClient): () => void   // returns restore()

// Client method (returns this for chaining):
client.captureConsole(): this

Behaviour

Each captured console.* call is printed once and enqueued exactly once. The SDK's own console output (e.g. platform.logger.info echo, internal error messages) is never re-captured, so captureConsole() cannot recurse. Calling it twice is a no-op; call the returned function to restore the original console methods.

Console method Cairn log level
console.log INFO
console.warn WARN
console.error ERROR
console.debug DEBUG

Arguments are serialised:

  • Strings are passed as-is.
  • Objects are JSON-stringified.
  • Error instances are formatted as message\nstack.

The original console functions are preserved and called first, so terminal output is unaffected.


Full Example

import { PlatformClient } from '@mpratyush54/sdk-node';

const client = new PlatformClient();

async function main() {
  await client.init({
    projectName: 'my-service',
    platformUrl: 'https://platform.example.com',
  });

  // After this point all console output is also sent to Cairn
  client.captureConsole();

  console.log('Application started');        // → Cairn log (INFO)
  console.warn('Deprecated API called');     // → Cairn log (WARN)
  console.error(new Error('DB timeout'));    // → Cairn log (ERROR)

  // Later — optional cleanup (stops logger flush too)
  await client.shutdown();
}

main();

Notes

  • There is no built-in restore() at the module level. To restore original behaviour, call client.shutdown() which stops the logger flush, or re-assign the original functions from a saved reference.
  • The patching is global — it affects the entire Node.js process.