You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by sivamma <sk...@yahoo.co.in> on 2007/06/28 03:59:44 UTC
How to implement Repositoryselector to get different logs in the
same webapplication
Hi I would like to get different logs in the same application , based on the
classes.
Our application will be used as a library in another applications.We would
like to have our logs separately from applications using our library.
The applications using our library also have log4j support and at present we
r getting even their logs also into our log file.In thier application,
WEB-INF contains both the log4j config files.
We r initiating the log4j config through source code.
I have already posted a query regarding the problem what we are facing now.
As i understood ,the problem will get resolve through repository selectors.I
openned another session
Heri told me the code what i am using for repositoryselectors is useless.Can
you please help me how can i implement respository selectors to solve this
problem.I have very less time.If i could not solve this means i need to move
to our own logging methods,like writing to the files and send mail using
Java Mail.
But I would like to use Log4j as i am sure there will be some possibility to
solve this kind of problem.
Please find repository selector what i have implemented already and not
useful to solve this problem.
==============================================================
package com.CK;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootCategory;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;
/**
* This RepositorySelector is for use with web applications.
* It assumes that your log4j.xml file is in the WEB-INF directory.
* @author Stan Silvert
*/
public class Log4jSetup implements RepositorySelector
{
private static boolean initialized = false;
// This object is used for the guard because it doesn't get
// recycled when the application is redeployed.
private static Object guard = LogManager.getRootLogger();
private static Map repositories = new HashMap();
private static LoggerRepository defaultRepository;
/**
* Register your web-app with this repository selector.
*/
public static String rPath = null;
public Log4jSetup(String realPath)
{
rPath = realPath;
//ServletConfig config;
init();
}
public static synchronized void init()
{
//String realPath =
getServletConfig().getServletContext().getRealPath("/");
//rPath = realPath;
if( !initialized ) // set the global RepositorySelector
{
defaultRepository = LogManager.getLoggerRepository();
RepositorySelector theSelector = new Log4jSetup();
LogManager.setRepositorySelector(theSelector, guard);
initialized = true;
}
Hierarchy hierarchy = new Hierarchy(new
RootCategory(Level.DEBUG));
loadLog4JConfig(hierarchy);
ClassLoader loader =
Thread.currentThread().getContextClassLoader();
repositories.put(loader, hierarchy);
}
// load config.properties from WEB-INF
private static void loadLog4JConfig(Hierarchy hierarchy)
{
try
{
String filePath =
rPath+File.separator+"WEB-INF"+File.separator+"config.properties";
String logfilePath =
rPath+File.separator+"WEB-INF"+File.separator+"logs"+File.separator;
if( !(new File(filePath)).isFile())
{
System.err.println("ERROR:Log4jSetUp::Cannot read the Log4J
configuration file. " +
"Please check the path of the config init param in web.xml");
}
System.setProperty("ck.base",logfilePath);
PropertyConfigurator.configure(filePath);
}
catch (Exception e)
{
System.err.println("Throws Exception::"+e.getMessage());
}
}
private Log4jSetup() {
}
public LoggerRepository getLoggerRepository() {
ClassLoader loader =
Thread.currentThread().getContextClassLoader();
LoggerRepository repository =
(LoggerRepository)repositories.get(loader);
if (repository == null) {
return defaultRepository;
} else {
return repository;
}
}
}
=========================================================
My Log4j config file
============================================================
log4j.rootCategory=debug, R,mail
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${ck.base}ck_logs.log
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy MMM dd HH\:mm\:ss} -- %p
-- %m%n
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.SMTPPassword=xxxxxx
log4j.appender.mail.BufferSize=512
log4j.appender.mail.From=skempili@abc.com
log4j.appender.mail.SMTPUsername=siva
log4j.appender.mail.To=skempili@abc.com
log4j.appender.mail.Subject=\=[SMTPAppender] Application message
log4j.appender.mail.SMTPHost=mail.abc.com
log4j.appender.mail.Threshold=WARN
log4j.appender.mail.evaluatorClass=com.CK.Evaluator
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{yyyy MMM dd HH\:mm\:ss} --
%p -- %m%n
=============================================================================
Thanks & Regards,
Sivamma.
--
View this message in context: http://www.nabble.com/How-to-implement-Repositoryselector-to-get-different-logs-in-the-same-webapplication-tf3992012.html#a11335803
Sent from the Log4j - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org