1.Eclipse本身已经存在了.log文件和Error Log View. 使用Log4j插件有什么意义?

2.添加org.apache.log4j_1.2.13.v200903072027.jar的依赖。

3.在OpenViewAction.java中添加:

Logger logger = Logger.getLogger(OpenViewAction.class);

logger.log(Level.ERROR, "This is a test...");

4.运行控制台打印:(只在控制台上,不在.log文件中)

log4j:WARN No appenders could be found for logger(myrcp.OpenViewAction).

log4j:WARN Please initialize the log4j systemproperly.

这是因为没有添加Log4j的配置信息。

5.在启动RCP的插件中的start方法里:

public void start(BundleContext context) throwsException {

super.start(context);

plugin =this;

Properties prop = newProperties();

try{

prop.load(Activator.class.getResourceAsStream("log4j.properties"));//$NON-NLS-1$

} catch(IOException e) {

// e.printStackTrace();

}

PropertyConfigurator.configure(prop);

}

6.在myrcp.Activator同级目录下添加log4j.properties:

### direct log messages to stdout ###

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L -%m%n

### direct messages to file hibernate.log ###

#log4j.appender.file=org.apache.log4j.FileAppender

#log4j.appender.file.File=hibernate.log

#log4j.appender.file.layout=org.apache.log4j.PatternLayout

#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}%5p %c{1}:%L -%m%n

### set log levels - for more verbose logging change 'info' to'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.myrcp=debug

#log4j.logger.org.hibernate=info

log4j.logger.org.hibernate=debug

### log HQL query parser activity

#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL

#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###

log4j.logger.org.hibernate.type=info

#log4j.logger.org.hibernate.type=debug

### log schema export/update ###

log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees

#log4j.logger.org.hibernate.hql=debug

### log cache activity ###

#log4j.logger.org.hibernate.cache=debug

### log transaction activity

#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition

#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection###

### leakages when using DriverManagerConnectionProvider ###

#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

7.运行。OK。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐