Logging Mechanism

 

WebNMS SNMP API supports logging of the SNMP requests. There are two types of logging that can be done from applications.

Logging debug and error messages

 

Using this feature, applications can log the following messages while querying for one or more variables from the remote agent.

To do this, applications should implement LogInterface class available in com.adventnet.utils package. The LogInterface contains the following methods.

 

// implemented for logging debug messages
public void dbg(String debug); 

//implemented for logging error messages
public void err(String error); 

//implemented for logging result
public void out(String out);

 

The LogManager class from com.adventnet.utils package manages the redirecting of debug messages and error messages from the SnmpSession class to the client, which sends request. The client has to implement LogInterface in his application. When the client is registered by calling addLogClient() method, LogManager generates an unique ID and returns it to the client that registers with LogManager. This clientID should be set in the requests that are sent to the agent. In the case of low-level api's, this should be set on the SnmpPDU instance that is used in sending the request. In the case of high-level api's, this will be handled internally. Using this clientID, the LogManager will call the corresponding client's debug()/err()/out() method of the LogInterface implementation. The following code snippet shows how to implement the LogInterface in application that uses high-level API.

 

public class client implements LogInterface{

public client() {

SnmpTarget target =new SnmpTarget();
target.addLogClient(this);
//sending request
.......
.......
}

public void dbg(String debug){

......
}

public void err(String error){

......
}

public void  out(String out){

......
}

}

 

The following code snippet shows how to implement the LogInterface in applications using low-level API.

 

public class client implements LogInterface{

public client() {

SnmpAPI api = new SnmpAPI();
........
SnmpSession session = new SnmpSession(api);
.......
int id = LogManager.addLogClient(this);
SnmpPDU pdu =new SnmpPDU();
pdu.setClientID(id);
......
// sending request
......
......
}

public void dbg(String debug){

......
}

public void err(String error){

......
}

public void  out(String out){

......
}

}

 

Logging the actions performed in the application

 

Using this feature, applications can log various actions that take place during the processing of SNMP request. This would come in handy for various purposes like pinpointing bugs, performance blockades etc. Messages can be logged

To enable this feature, addLogClient(LoggerProperties) method should be invoked. In application, if low-level api is used, then this method should be invoked from com.adventnet.utils.LogManager class. If the application uses high-level api then the same should be invoked from the class being used like SnmpTarget, SnmpRequestServer etc.

 

LoggerProperties class should be instantiated first by invoking LoggerProperties() constructor with the first argument as the package name and the second argument as the filename. For example, if a LoggerProperties instance needs to be created for the beans package with the filename "beans.txt", then invoke LoggerProperties.

 

loggerProp = new LoggerProperties("BEANS", "beans.txt");

 

The different package names with which LoggerProperties instance can be created is SNMP2, MIBS, LLAPI, BEANS, HLAPI and SNMP.

 

Logging level can be set by invoking setLogLevel(int ) method in LoggerProperties class. The different logging level that are supported are DEFAULT, SERIOUS and CRITICAL. Static constants for the logging levels have been provided in com.adventnet.utils.LogManager class. Depending on the level, logging of messages will be done.

 

The logging type should be added as a custom property to the LoggerProperties instance with the key as "LOGTYPE". DEBUG and PERF are the two logging types that are currently supported. The logging type can be added as a custom property by calling addCustomProperty("LOGTYPE", "DEBUG");

 

If any user implementation of Logger interface needs to be used in the application, then the implementation file with the package structure should be passed to setClassName() in LoggerProperties. We are providing a default implementation, SnmpLoggerImpl.

 

The following code snippet shows how to enable this feature in a application that uses high-level API.

 

public class client {
public client() {
.......
.......
LoggerProperties loggerProp = new LoggerProperties("SNMP", "SNMP.txt");
loggerProp.setClassName("com.adventnet.utils.SnmpLoggerImpl");
loggerProp.setLogLevel(LogManager.CRITICAL);
loggerProp.addCustomProperty("LOGTYPE", "DEBUG");
SnmpTarget target =new SnmpTarget();
target.addLogClient(loggerProp);
.......
.......
}
}

 

The following code snippet shows how to enable the logging feature in a application that uses low-level API.

 

public class client {
public client() {
......
......
LoggerProperties loggerProp = new LoggerProperties("SNMP2", "SNMP2.txt");
loggerProp.setClassName("com.adventnet.utils.SnmpLoggerImpl");
loggerProp.setLogLevel(LogManager.CRITICAL);
loggerProp.addCustomProperty("LOGTYPE", "DEBUG");
int clientID = LogManager.addLogClient(loggerProp);
SnmpPDU pdu = new SnmpPDU();
pdu.setClientID(clientID);
// Send the data
.......
.......
}
}



Copyright © 2012, ZOHO Corp. All Rights Reserved.