⚙️ NetSuite Logging & Governance Tips
🧠 Why Logging & Governance Matter
Every SuiteScript you run consumes governance units and generates logs in NetSuite’s system.
Poor logging habits and inefficient governance handling are the top causes of:
- Unexpected script termination (SSS_USAGE_LIMIT_EXCEEDED)
- System slowdowns due to excessive logs
- Difficult debugging during production
By following smart logging and governance management practices, you’ll make your scripts lighter, faster, and safer for long-term use.
🧩 1. Understand Logging Levels
NetSuite supports four log levels — each serves a specific purpose.
Log Level | Purpose | Typical Usage |
---|---|---|
AUDIT | Key checkpoints, summary logs | Script start, end, summary |
ERROR | Failures or exceptions | Try/Catch errors |
DEBUG | Developer-level debugging | Variable tracking during testing |
EMERGENCY | Critical system issues | Fatal conditions (rarely used) |
✅ Best Practice:
Use AUDIT for production-level tracking, and DEBUG only in sandbox or test environments.
🧩 Example:
log.audit('Script started', { recordType: recordType, totalProcessed: count });
log.error('Error updating record', e.message);
⚙️ 2. Avoid Excessive Debug Logs
Every log.debug()
call consumes memory and counts toward system-wide log limits.
💡 NetSuite Governance Fact:
A company can make up to 100,000
N/log
calls across all scripts within a 60-minute period.
If one script logs excessively, NetSuite automatically raises its log level — preventing further debug logs from appearing.
✅ Best Practice:
Use a script parameter or runtime condition to control log verbosity.
🧩 Example:
const debugMode = runtime.getCurrentScript().getParameter('custscript_debug_mode');
if (debugMode === 'T') {
log.debug('Record processed', recordId);
}
⚙️ 3. Use Structured Logging
Always log structured messages for easier tracking and analysis.
✅ Do:
log.audit('Processing Invoice', { id: invoiceId, status: 'Pending' });
❌ Avoid:
log.audit('Invoice processed successfully ' + invoiceId + ' pending status true');
Structured logs are more readable and easier to parse during debugging or integration monitoring.
⚙️ 4. Monitor Governance Units Regularly
Use the runtime
module to track remaining usage during execution.
🧩 Example:
const remaining = runtime.getCurrentScript().getRemainingUsage();
if (remaining < 200) {
log.audit('Low governance', remaining);
// reschedule or yield
}
✅ Best Practice:
- Log remaining usage at checkpoints.
- Yield or reschedule when governance falls below 200.
- Avoid performing large loops without governance checks.
⚙️ 5. Use “Yield” in Map/Reduce Scripts
Map/Reduce scripts automatically yield when governance is low, but it’s good practice to handle custom yield points for long-running tasks.
🧩 Example:
if (runtime.getCurrentScript().getRemainingUsage() < 100) {
runtime.scheduleScript({
scriptId: 'customscript_large_process'
});
return;
}
This ensures smooth continuation without “Script Execution Usage Exceeded” errors.
⚙️ 6. Keep Log Messages Short
NetSuite truncates long logs in the execution view.
If you need to debug large data structures:
✅ Tip:
Use JSON.stringify()
with substring limits.
🧩 Example:
log.debug('Response Data', JSON.stringify(response).substring(0, 500));
This prints only the first 500 characters — enough to debug without flooding logs.
⚙️ 7. Don’t Log Sensitive Information
Never log credentials, API keys, or customer data.
❌ Avoid:
log.debug('Token', token);
✅ Do:
log.debug('Token generated', '***masked***');
This is especially important for integrations and GDPR compliance.
⚙️ 8. Implement Script Summary Logs
For long scripts, summarize key results at the end rather than logging each record.
🧩 Example:
log.audit('Summary', {
totalProcessed: processedCount,
totalErrors: errorCount,
scriptDuration: `${(new Date() - startTime) / 1000}s`
});
This keeps execution logs concise and gives a high-level view of script success.
⚙️ 9. Clean Up Old System Logs
System logs are automatically purged:
- System Error Logs: after 60 days
- User Logs: after 30 days
✅ Tip:
If you need longer-term retention, export logs using a Saved Search or RESTlet to store in a custom record.
⚙️ 10. Combine Governance + Logging in One Utility
You can create a custom helper module to handle both logging and governance checks consistently across all scripts.
🧩 Example:/SuiteScripts/utils/logGovernance.js
define(['N/runtime', 'N/log'], (runtime, log) => {
const check = (context) => {
const usage = runtime.getCurrentScript().getRemainingUsage();
if (usage < 200) log.audit('Governance Low', usage);
return usage;
};
const info = (title, details) => log.audit(title, details);
const error = (title, e) => log.error(title, e);
return { check, info, error };
});
Then use:
const gov = require('./utils/logGovernance');
gov.check();
gov.info('Step completed', { record: id });
💡 Summary Checklist
✅ Use AUDIT logs for production
✅ Avoid excessive DEBUG logs
✅ Track governance with runtime checks
✅ Log structured, readable messages
✅ Mask sensitive data
✅ Summarize logs at the end of execution
✅ Build reusable logging utility
Leave a Reply