How to write logs with newline using IO.write in Ajax Truclient

Suppose you want to create your own log file, here's how to do that in Ajax Truclient using the Evaluate Javascript action from the Toolbox.

So, drag it onto your script, make sure it's inside an Action and not in vuser_init or vuser_end. It didn't work when I initially placed it inside vuser_init and I'm not sure why.

To write to a text file, basically you just need to use IO.write in this format

IO.write(filename[string], input[string], append[boolean], charset[string]);

In my case, since I want to add a timestamp and a newline after every log, here's what I used:

var currentDate = new Date();
IO.write("C:\\Data\\Logs\\logs.txt",currentDate.toLocaleString() + " >>> ",true,"UTF-8");
  • On line 1 I created a date variable
  • On line 2 I used toLocaleString() method to output the date time in a readable format based on the machine's default locale. In my case it's en-US. You can add parameters to this to specify a different locale. Also, make sure to escape the slashes on the path.
  • On line 3 I appended my log message which says hello
  • On line 4 I appended a carriage return and a newline using \r\n because sometimes \n alone doesn't work

Running it 3x produces this output. Note that it automatically creates the file if it doesn't exist BUT it doesn't create directories.

ALTERNATIVELY, you can also use the Generic API Action from the toolbox to execute the IO.write function.

Just change the category to IO, method name to write, and put it your arguments. It's essentially the same as IO.write but with less coding involved hehe

Hope this helps!