βοΈ 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/logcalls 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