分享

RCP开发中使用Log4j

 LibraryPKU 2013-12-11
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。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多