Created
March 28, 2017 16:57
-
-
Save codefriar/4269c2546e89fbd8bcb40d62d11a4708 to your computer and use it in GitHub Desktop.
Log.cls is a standardized log formatter for putting data into salesforce debug logs in a clean and efficient manner. The idea, is to make the debug data easy to find in the 1+ mb of text that each apex execution context can produce.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by kpoorman on 3/25/17. | |
* Log provides standardized methods for logging data to | |
* debug logs in a structured, easy to find way. | |
* All methods are static by design. This should be 0 setup | |
* or at least 0 in code setup. | |
* | |
* Future versions may pull configuration from | |
* custom metadata #safeHarbor | |
*/ | |
Public Class Log { | |
Private Static String logHeader = ''; | |
Private Static String logFooter = ''; | |
/** | |
* Stores the count of times the log method has been invoked | |
* in *this* apex execution context. | |
*/ | |
Private Static Integer logExecutionCount = 0; | |
Public Static Void debug(Object toLog){ | |
logHeader = 'START DEBUG LOG - ' + Log.logExecutionCount; | |
logFooter = 'END DEBUG LOG - ' + + Log.logExecutionCount; | |
log(toLog, '#'); | |
} | |
Public Static Void error(Object toLog){ | |
logHeader = 'START ERROR LOG - ' + Log.logExecutionCount; | |
logFooter = 'END ERROR LOG - ' + + Log.logExecutionCount; | |
log(toLog, '%'); | |
} | |
/** | |
* | |
*/ | |
Private Static Void log(Object toLog, String borderChar){ | |
List<object> toLogList = new List<object>(); | |
try { | |
toLogList = (list<object>) toLog; | |
} catch (Exception e){ | |
system.debug(e); | |
} | |
if(toLogList.size() > 0) { | |
String stringVersion = ''; | |
for (Integer i = 0; i < toLogList.size(); i++) { | |
stringVersion += '[' + String.valueOf(i).leftPad(4) + ']' + toLogList[i] + '\n'; | |
} | |
system.debug('\n\n' + logHeader.center(80, borderChar) + '\n\n' + stringVersion + '\n\n' + logFooter.center(80, borderChar) + '\n\n'); | |
} else{ | |
system.debug('\n\n' + logHeader.center(80, borderChar) + '\n\n' + toLog + '\n\n' + logFooter.center(80, borderChar) + '\n\n'); | |
} | |
Log.logExecutionCount++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment