Purpose of Logging in SuiteScript
Logging in SuiteScript serves to:
- Track script execution paths
- Validate values at runtime
- Identify logical or runtime errors
- Document important events for audit or support teams
SuiteScript 2.x provides structured logging via the N/log module, which supports multiple log severity levels.
Log Levels in NetSuite
NetSuite provides four standard log levels, each suited for different purposes:
1. DEBUG
Used for low-level details useful during development and testing. Commonly used to:
- Output variable values
- Show conditional logic outcomes
- Print internal function results
2. AUDIT
Intended for higher-level messages that confirm successful execution of key operations. Used for:
- Logging when a script completes successfully
- Confirming record creation, transformation, or updates
- Highlighting important process steps for future reference
3. ERROR
Used to log problems and exceptions encountered during script execution. Helps identify:
- Business logic errors
- Failed data manipulations
- Exceptions thrown by NetSuite modules or custom logic
4. EMERGENCY
Reserved for critical system failures. This level indicates an issue severe enough to halt operations and usually requires immediate attention.
Importance of Log Level Optimization
Using log levels properly helps:
- Reduce clutter in execution logs
- Improve script readability and maintainability
- Make production logs more meaningful
- Ensure sensitive or unnecessary development information isn’t exposed in live environments
Best Practices for Log Level Optimization
✔ Use DEBUG selectively
Utilize debug logs during development to inspect variable values, decision-making points, and loop iterations. These should generally be disabled or removed before deployment to production.
✔ Use AUDIT to track key business logic
Audit logs help system admins and developers understand when and why certain processes occurred. These are appropriate in production to provide a high-level overview of script activity.
✔ Use ERROR for exceptions
Always log errors with enough context to understand the failure—include function names, record IDs, and a brief message. Avoid overusing it for non-critical warnings.
✔ Use EMERGENCY only when essential
Use this level for unrecoverable or severe problems that may cause downstream system issues. This log level should be rare and reserved for true emergency scenarios.
Conditional Logging
To reduce production noise and ensure logs are only created, when necessary, consider logging based on:
- Execution context: Use
runtime.executionContextto log only in non-production environments. - Script parameters: Enable or disable logging dynamically through script deployment parameters.
- Log flags or environment checks: Control verbose logging through internal logic to prevent performance impacts.