cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2813
Views
5
Helpful
6
Replies

CVP Java - Log4j for logging

dipmehta33
Level 1
Level 1

We have implemented custom element using Cisco CVP. We are able to achieve logging using audium addtolog function. But We need to know how we can use log4j for logging in our custom java classes. Do we need to modify logging.properties or log4j.xml by adding package name of our custom classes. We would like to know the location on call studio and vxml server if we can change logging.properties or log4j.xml.

1 Accepted Solution

Accepted Solutions

Angelina Talley
Level 5
Level 5

In order to get Log4J working for your custom code, you do no want to or need to adjust CVP's log configurations. Rather, you will need to create your own Log4J .properties or .xml configuration file and put it in the classpath. You can then, in Java, read the file off the classpath without having to use a static file path. So, if the config file were in your classpath, something like this would load it and initialize your configuration without having to keep a static path:

try {  

    URL url = ApplicationStart.class.getResource("/MyConfig_log4j.xml");

    DOMConfigurator.configure(url);

} catch (Exception e) {

    // do something   

}

Now, there are several places you can put the file and have it work. The best choice really depends on your requirements. In Studio, you can put it in deploy/java/application/classes inside of your Studio project. That way, it will deploy with your Studio project and be available automatically. That's usually the simplest as it means you don't have to maintain a copy separately on the server. Otherwise, you can place it with your custom jar files on the CVP server itself, just be sure to use the classes subdirectory instead of the lib subdirectory of wherever you are placing it.

A couple more important notes:

1. don't override the root logger unless you want to start messing with CVP's logging as well

2. don't call your file log4j.properties or log4j.xml, give it some other unique name (so you again don't mess with CVP's logging)

3. if you have logging config files in multiple Studio projects, be sure they're not configuring the same logger. Otherwise, the last one to load will override all the ones that came before it

Good luck!

View solution in original post

6 Replies 6

Hello!

This is a suggestion how a I have implemented log4j:

public class MyCustomCell extends ActionElementBase implements ElementInterface{

      static{

        PropertyConfigurator.configure("C:\\cfg\\CustomCVPElementLog.properties");

      }

        static Logger logger = Logger.getLogger(MyCustomCell.class);

      ...

}

Angelina Talley
Level 5
Level 5

In order to get Log4J working for your custom code, you do no want to or need to adjust CVP's log configurations. Rather, you will need to create your own Log4J .properties or .xml configuration file and put it in the classpath. You can then, in Java, read the file off the classpath without having to use a static file path. So, if the config file were in your classpath, something like this would load it and initialize your configuration without having to keep a static path:

try {  

    URL url = ApplicationStart.class.getResource("/MyConfig_log4j.xml");

    DOMConfigurator.configure(url);

} catch (Exception e) {

    // do something   

}

Now, there are several places you can put the file and have it work. The best choice really depends on your requirements. In Studio, you can put it in deploy/java/application/classes inside of your Studio project. That way, it will deploy with your Studio project and be available automatically. That's usually the simplest as it means you don't have to maintain a copy separately on the server. Otherwise, you can place it with your custom jar files on the CVP server itself, just be sure to use the classes subdirectory instead of the lib subdirectory of wherever you are placing it.

A couple more important notes:

1. don't override the root logger unless you want to start messing with CVP's logging as well

2. don't call your file log4j.properties or log4j.xml, give it some other unique name (so you again don't mess with CVP's logging)

3. if you have logging config files in multiple Studio projects, be sure they're not configuring the same logger. Otherwise, the last one to load will override all the ones that came before it

Good luck!

dipmehta33
Level 1
Level 1

Thank you very much Angie.

Did any work to add the loggers for log4j2?

I have the same code for log4j 1.x but when I am using the below log4j2 code to configure loggers it is not able to store the logs.

URI uri = LogReader.class.getResource("/log4j2.xml").toURI();
File file = new File(uri);
ConfigurationSource source = new ConfigurationSource(new FileInputStream(file), file);
Configurator.initialize(null, source);

Thanks in advance.

Add the folowing libraries: log4j-api-x.y.x.jar and log4j-core-x.y.z.jar to callstudio_project/deploy/java/util/lib folder. Then try the following snipped:

public class MyElement extends ActionElementBase implements ElementInterface {
	
	static {
		System.setProperty("log4j.configurationFile","C:/path/config_log4j2.properties");
	}

	static Logger logger = LogManager.getLogger(MyElement.class);

	public void doAction(String name, ActionElementData data)
			throws ElementException {
		// ...
		logger.info("My log message");
		// ...
	}
// ...
}

Also, don't forget to put the proper configuration file for log4j2 on the specified path.

Hi @Vladimir Lozano Martinez,

Thanks for your response

I have tried using the above code but in the Cisco CVP 12.6.2, we have below log4j jar files in the Tomcat lib location. 

log4j-api.jar, log4j-core.jar, and log4j-slfj18-impl.jar I am using log4j-api-4.2.17.jar and log4j-core-4.2.17.jar but when I add 4. x jar files into the application lib location it is throwing error not able to deploy the application. I have tried without adding the jar files but it is not generating any logs.

Thanks & Regards

Pradeep S

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: