This sample demonstrates how to write the process default event log record information to a file. Run the sample by entering a command similar to the following:
java LogSample username password <server name>:<port number>/<router instance name> [output_filename]
Note For a detailed explanation of the command line, see the Run the sample application section of the Run the Unmodified Samples topic.
The LogSample class contains two methods: the main method main(String args[]) and the constructor method LogSample(VWSession vwSession, Logger logger).
The main method uses common techniques for validating and defaulting argument values. The default value for the log output file is "LogSample.out". The main method constructs and passes vwSession and Logger objects to the sample constructor. Main() handles the login and logoff for the session with the login() and logoff() methods of the sample SessionHelper class. It provides workflow logging with an instance of the sample Logger class. The main method passes the session and the logger to the sample's class constructor.
The constructor for the LogSample class notifies the user of its work and creates a log query object for the default event log. It validates the object, its elements, and the element fields and displays element and field information. The LogSample constructor performs this as follows:
Notify the user.
logger.logAndDisplay("\n~ Starting LogSample execution.");
Get the log object for the DefaultEventLog and log its name.
vwLog = vwSession.fetchEventLog("DefaultEventLog");
logger.log("Log: " + vwLog.toString());
Set the maximum number of elements to 25, which is half the default value of 50. A value of 25 will require less memory for each fetch.
vwLog.setBufferSize(25);
Construct a log query object and query for all elements. Create a VWLog class query with the VWLog.startQuery() method. Note that this contrasts with queue and roster query objects, which are constructed by createQuery() methods.
logQuery = vwLog.startQuery(null, null, null, 0, null, null);
logElement = logQuery.next();
Check to see if there are any log elements.
if (logElement == null){
logger.log("\t Log elements: none");
}
else { . . .
Iterate through the log elements. Log each element to the output file.
Put the element's field names into a string array with the VWLogElement.getFieldnames() method, and output a message if no fields are found.
do {
logger.log("\t Log element:");
// Display the fields.
fieldNames = logElement.getFieldNames();
if (fieldNames == null){
logger.log("\t\t No fields!");
} else {
logger.log("\t\t Fields:");
Retrieve and display each field name and its value. VWLogElement.getFieldValue(string) returns an object that is the corresponding value for the fieldnames.
for (int i = 0; i < fieldNames.length; i++){
if (fieldNames[i] != null){
// Retrieve the field value object.
value = logElement.getFieldValue(fieldNames[i]);
Display a value string for the field value object by invoking logger.log(String, Object).
// Display the field name and value(s). Note that this log method takes an object for its second parameter.
logger.log("\t\t\t" + fieldNames[i] + " = ", value);
}
}
}
while ((logElement = logQuery.next()) != null);
}
} // . . .
Additional code handles messages, exceptions, and cleanup.