How to write custom logs in CAF Portlet Applications

Hi All,

Iam new to this forum and also to webMethods.

We have MWS 8.2 and IS. In my CAF application portlet, i want to write customized logs using log4j or any other in a separate log file.

kindly provide guidelines, sample code…

regards,
Lakshmi.P

Portlets deployed to MWS can use several different logging APIs. You can use whichever one you prefer.

For example:


		//logging via log4j APIs    
		org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(getClass());
		log4jLogger.info("Log Message from org.apache.log4j.Logger");

		//logging via java built in logging APIs
		java.util.logging.Logger juliLogger = java.util.logging.Logger.getLogger(getClass().getName());
		juliLogger.info("Log Message from java.util.Logger");
		
		//logging via apache commons logger
		org.apache.commons.logging.Log oacLogger = org.apache.commons.logging.LogFactory.getLog(getClass());
		oacLogger.info("Log Message from org.apache.commons.logging.Log");
		
		//logging via the slf4j logging facade
		org.slf4j.Logger slf4jLogger = org.slf4j.LoggerFactory.getLogger(getClass());
		slf4jLogger.info("Log Message from org.slf4j.Logger");

The log messages for all of the above logging apis will displayed in the console and recorded in the MWS full.log

1 Like

thanks for your reply eric !

actually i want to write logs in my <custom_log_file>.log not in MWS default _full_log

can i add log4j.jar, log4j.properties in My CAF Portlet Application Project and use it to write custom logs.

or else should i have to use in-built log4j.jar in MWS. i think in-built jar only supports logs to write logs in _full_log file, server.log files.

kindly suggest !!!

the log4j.jar provided by MWS should be fine, you don’t need another copy of that.

Since CAF applications are web applications, you can use the techniques for initializing log4j in a servlet or context listener.

For an example, see:
[url]http://blog.idleworx.com/2010/01/setting-up-log4j-for-simple-java-web.html[/url]

your log4j.properties file content would just need to define the appender and declare what log categories go to the appender.

For example, something like this:


# your category
log4j.category.yourLogCategory=INFO, yourCustomFile
log4j.additivity.yourLogCategory=true

# appender
log4j.appender.yourCustomFile=com.webmethods.rtl.logging.CollectorFileAppender
log4j.appender.yourCustomFile.File=${log4j.logging.dir}/yourLogFileName.log
log4j.appender.yourCustomFile.threshold=${log4j.default.log.level}
log4j.appender.yourCustomFile.Append=true

Hi all,
I am not able to get log details from CAF UI page which is logged for testing
Have used

org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger(getClass());
		log4jLogger.info("Log Message from org.apache.log4j.Logger");
or 

System.out.println("Test Log");

But none of the log is appearing in any log file.
Please let me know if there is any alternate way to get the intermediate value printed in log file for testing.

Wm Version:9.7
OS:Windows

Thanks
Baharul

Your log4j example looks correct to me. That log message would be written to the MWS full.log unless the administrator has configured the logging level for that appender to a level that is higher than ‘info’.

The System.out.println call would only write the message to the console window, it would not get routed to the MWS log file.

There is a solution described on Stack Overflow - Logging in webMethods CAF - Stack Overflow