How to output Salesforce session Id in Apex debug log?

UserInfo.getSessionId() is one of those methods you reach for constantly, mainly when you’re making REST callouts from Apex. The annoyance: you can’t actually see it in your debug logs.

Try the obvious approach:

System.debug(UserInfo.getSessionId());

Output:

USER_DEBUG [1]|DEBUG|SESSION_ID_REMOVED

Try being clever about it:

System.debug('Token: ' + UserInfo.getSessionId());

Same result. Salesforce scrubs session IDs out of logs regardless of how you wrap them. Intentional, not a bug.

The Workaround

System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' + UserInfo.getSessionId().substring(15));

The session ID shares its first 15 characters with the Org ID. Split it there, stick a space in between, and the scrubber doesn’t catch it. You get both parts in the log output. To get the actual token back, concatenate them and remove the space.

Useful when you need a live token to test a callout manually in Postman or debug an integration locally. That said, session tokens carry full user-level access. Don’t leave this in production code.