我正在为我的项目使用log4j。不幸的是,我无法在我手动编写的应用程序中记录信息级别..虽然它完美地记录了数据库日志!
这是我的属性文件:
log4j.rootLogger=OFF
log4j.appender.serviceLog=org.apache.log4j.RollingFileAppender
log4j.appender.serviceLog.File=C:/Users/prateekg/Desktop/log4j/log
log4j.appender.dbLog=org.apache.log4j.RollingFileAppender
log4j.appender.dbLog.File=C:/Users/prateekg/Desktop/log4j/dbLog
log4j.appender.serviceLog.MaxFileSize=1MB
log4j.appender.serviceLog.MaxBackupIndex=1
log4j.appender.serviceLog.layout=org.apache.log4j.PatternLayout
log4j.appender.serviceLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.dbLog.layout=org.apache.log4j.PatternLayout
log4j.appender.dbLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.category.serviceLog=INFO,serviceLog
log4j.category.org.springframework.jdbc=DEBUG, dbLog
编辑:
这是我的日志代码:
public static Logger logger = Logger.getLogger(Controller.class);
Boolean commit=false;
public Controller(DashBoardDAO dao,MessageProcessor mp, HandlerFactory hf, ToXML xml)
{
this.dao=dao;
this.mp=mp;
this.hf=hf;
this.xml=xml;
PropertyConfigurator.configure("log4j.properties");
}
public Boolean controlFlow(Message message)
{
Handler handler=null;
String data[]=null;
Boolean flag=false;
int count=0;
handler=initializeHandlerFactory(message);
data=getMessage(handler,message);
flag=validate(data);
count=getEventCount(data);
//commit=mp.flag1;
//System.out.println("The flag is "+flag);
if(flag)
{
System.out.println("inside flag");
String elementNames[]=dao.getElementNames(Integer.parseInt(data[0]));
System.out.println("No of tags "+elementNames.length);
logger.info("No of tags in the generated XML is "+elementNames.length);
File xmlFile=xml.convertToXML(elementNames, data);
logger.info("Temporary XML File created");
logger.info("Data sent for insertion");
dao.insert(count, data,xmlFile);
Boolean f=xmlFile.delete();
System.out.println("Temp XML file Deleted: "+f);
if(f)
{
logger.info("Temporary XML File deleted");
commit=mp.flag1;
}
else
logger.info("Error in deleting temporary XML File");
}
答案 0 :(得分:1)
您需要将记录器定义为Logger.getLogger("serviceLog")
或定义与您的类匹配的自定义附加程序
log4j.category.my.package.Controller=INFO,serviceLog
或您的包裹
log4j.category.my.package=INFO,serviceLog
此外,不需要调用PropertyConfigurator.configure("log4j.properties");
,因为这是在启动时由log4j查找的默认配置文件名。