About Logging
Debugging your scripts is perhaps the most important skill as a developer, next to writing actual code. Debugging is a key to open many closed doors, and an eye to see through many thick walls. Several months ago, we made a blog about the basics of logging in NetSuite. But because of the importance of this module, it may do us some good to take a second, more detailed look into this crucial tool. As with all other modules, it is possible to define it at the beginning of a script. However, we are not alone in highly esteeming the “N/log” module, for NetSuite itself has made this module completely global. This means we actually don’t have to import it to our script. This module imports for us automagically.
Be warned, however, that client side scripts sometimes don’t support the “N/log” module if the script is running in the browser rather than NetSuite’s server. As a rule of thumb, you may want to use console.log (a native JavaScript function) in client-side scripts. There are four log methods in SuiteScripting: log.debug, log.audit, log.error, and log.emergency. Each variation is a step up in significance from the previous one. Here’s a quick summary of each:
Log.debug
The log.debug method is the most common log, and the one we use the most here at Suiterep when writing scripts. Typically, this log is only used with scripts that are in testing or debugging (hence the term log.debug). With this log, we can send any data defined in our script to the NetSuite script execution log for examination.
Log.audit
Like log.debug, log.audit also can be used to log information, although it is intended for higher-level data. We recommend using this if you would like to see some of the major checkpoints in a project.
Log.error
On the error handling side of things, log.error is a common way to log errors to the execution log. This log will only occur when the script encounters a problem in completing a task.
Log.emergency
Log.emergency is the highest level of failure. Whereas log.error might indicate a small “blip” in the automation, log.emergency can be used to indicate an error absolutely fatal to the script.
In this tutorial, we will explain the two foundational types of logs: log.debug and log.error.
Setting Your Deployment Log Type
When you deploy your script, there is a dropdown list on the right where you can set the log type. This specifies the lowest tier you will see. If you choose “debug,” you will see debugging logs. If you choose “audit,” you will see auditing logs but not debugging logs. When the script is changed from “testing” to “released,” NetSuite automatically chooses “audit” as the log type, although you can change this to any of the log types you would like.
How to Use “log.debug”
First, we need to know where to see our logs in NetSuite. There are two places: (1) the script record and (2) the script deployment. Here’s what our empty “Execution Log” on the script record looks like before we run our script:
var foo = "data and stuff";
log.debug({ title: 'This is a log title',
details: 'Here are the details of the log - ' + foo
});
But at SuiteRep, we like shortcuts. Here’s what we do nearly 100% of the time:
var foo = 'data and stuff';
log.debug("This is a log title", 'Here are the details of the log - ' + foo);
Notice how we eliminated the inner object with the keys and such. That’s too complicated. We can just use a comma to separate the two keys—no object needed.
How to Use “log.error”
Here’s how to create an error. In this example, I accidentally misspell log.debug, which throws an error.
try {
log.deeebuggggg("This won't work! :)");
} catch(e) {
log.error("Uh oh. Error in the try/catch! - " + e.name, e.message);
}
It’s best practice to put log.errors into a try/catch. That way, if the operation fails, it won’t cause the entire script to fail. The script will “try” the first part, and if it doesn’t succeed it will “catch” it and log the error without failing the whole automation.
Things to note are the (e) after the “catch.” This is the NetSuite error that is returned after the script failed. That error will have a name (e.name) and a message (e.message). Here’s what the actual error looks like in the execution log:
Conclusion
Feel free to leave a comment with any questions or if you find this “N/log” module SuiteScript tutorial helpful! And be sure to subscribe to our email list for our latest SuiteScript tutorials.