How Can You Display The Salesforce Session ID In The Apex Debug Log?

You’re likely familiar with the UserInfo.getSessionId() method in Apex, commonly used for making Webservice calls. However, displaying it for external use can be challenging. When debugging, the log will typically output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
System.debug(UserInfo.getSessionId());
USER_DEBUG [1]|DEBUG|SESSION_ID_REMOVED
System.debug('------' + UserInfo.getSessionId() + '======');
USER_DEBUG [1]|DEBUG|------SESSION_ID_REMOVED======
System.debug(UserInfo.getSessionId()); USER_DEBUG [1]|DEBUG|SESSION_ID_REMOVED System.debug('------' + UserInfo.getSessionId() + '======'); USER_DEBUG [1]|DEBUG|------SESSION_ID_REMOVED======
System.debug(UserInfo.getSessionId());
USER_DEBUG [1]|DEBUG|SESSION_ID_REMOVED
System.debug('------' + UserInfo.getSessionId() + '======');
USER_DEBUG [1]|DEBUG|------SESSION_ID_REMOVED======

Even if you concatenate a string to it, you will consistently receive the

SESSION_ID_REMOVED.

However, upon researching online, I discovered a solution that proves effective:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' + UserInfo.getSessionId().substring(15));
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' + UserInfo.getSessionId().substring(15));
System.debug(UserInfo.getOrganizationId().substring(0, 15) + ' ' + UserInfo.getSessionId().substring(15));

Note the presence of a space concatenating the organization ID and the substring of the session ID. It needs to be removed to ensure the session ID is valid.