Logging in connector rule code

I am writing my first connector rule and want to log/print some variables like AccountRequest , ProvisioningPlan.AttributeRequest etc., to see how the object looks like. What is the best way to do this?

Hi @vikramsah ! First you will need to enable your logger, you can follow

then, just use log.info, .debug, .error, .warn, etc.

As AccountRequest, ProvisioningPlan.AttributeRequest, etc are objects, you will get the string serialization, but you can go printing objects attributes. I let you an example of some recent JDBC Provision rule I have developed these days, I always prints all, not only to see how the object look like, but is very good to debug (having ccg.log opened):

List accounts = plan.getAccountRequests();
if ((accounts != null) && (accounts.size() > 0)) {
	Iterator accountsIterator = accounts.iterator();

	while (accountsIterator.hasNext()) {
		AccountRequest account = (AccountRequest) accountsIterator.next();
		if (account == null) {
			log.warn("account returned is null - should never get here");
			result.setStatus(ProvisioningResult.STATUS_FAILED);
			return result;
		}
		log.warn("#################### plan is " + account.getOperation());

		log.warn(
				"before plan operations, getting all account attribute values ");
		Map accountAttributes = new HashMap();
		List attributesRequestList = account.getAttributeRequests();
		Iterator attributesRequesIterator = attributesRequestList.iterator();
		while (attributesRequesIterator.hasNext()) {
			AttributeRequest attributeRequest = (AttributeRequest) attributesRequesIterator.next();
			log.warn("attribute##" + attributeRequest.getName() + "##" + attributeRequest.getValue()
					+ "##" + attributeRequest.getDisplayValue());
			accountAttributes.put(attributeRequest.getName(), attributeRequest.getValue());
		}
	}
}

Hi Vikram

Here is some of the stuff I log - can be quite handy. I set a ‘logPrefix’ so it is easier to grep in CCG log. I also log as Error, so I dont have to keep playing with CCG Log levels

    String logPrefix = "Blah WSBO - ";
    try {
       if (provisioningPlan != null) {
           log.error(logPrefix + "Prov plan is not null");
           //This part is just info dump:
           for (int i = 0 ; i < this.variables.length ; i++) {
              String varName = this.variables[i];
              Object varValue = null;
              try {
                 if ("transient".equals(varName))
                    varValue = "[reserved word]";
                 else
                    varValue = eval(varName);
              } catch (Exception ex) {
                 varValue = "[eval exception]";
              }
              String varClass = "void";
              if ((void != varValue) && (null != varValue))
                 varClass = varValue.getClass().getSimpleName();

              if (void == varValue)
                  log.error(logPrefix + "x " + varName + " is void");
              else if (null == varValue)
                  log.error(logPrefix + "x " + varName + " is null");
              else
                  log.error(logPrefix + "x " + varName + " (" + varClass + ") = " + varValue);
           }
           log.error(logPrefix + "Full URL      = " + requestEndPoint.getFullUrl());
           log.error(logPrefix + "Context URL   = " + requestEndPoint.getContextUrl());
           log.error(logPrefix + "HTTP Method   = " + requestEndPoint.getHttpMethodType());
           log.error(logPrefix + "OperationType = " + requestEndPoint.getOperationType());
           log.error(logPrefix + "App ID        = " + application.getId() );

           for (AccountRequest accReq : Util.iterate(provisioningPlan.getAccountRequests())) {
               log.error(logPrefix + "AccountRequest Operation: " + accReq.getOperation().toString() );
               for (ProvisioningPlan.AttributeRequest attReq : Util.iterate(accReq.getAttributeRequests())) {
                   log.error(logPrefix + "attReq: " + attReq.getName() + " => " + attReq.getValue() + " (" + attReq.getValue().getClass() + ")");
               }

...
           }
       } else {
           log.error(logPrefix + "Rule - Modify Body: plan is null");
       }
    } catch (Exception ex) {
        log.error(logPrefix + "Exception: " + ex);
    }
2 Likes

Thanks Julian, This was helpful.

I agree with a lot of this.

  • do not declare your own logger, use log.{level}(“string”)

setting a specific prefix is also amazingly helpful for Grepping logs.

log.info(“WSAO: SourceName: Operation - {log goes here}”) means that i can:
tail -f ~/log/ccg.log | grep "WSAO: SourceName"
and see all of the relevant details.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.