Tomee - slf4j创建日志文件但不写入

时间:2017-06-26 15:48:40

标签: maven java-ee log4j slf4j tomee

我想将log4j用于我的应用程序(我现在不需要处理服务器日志)。我搜索了一个解决方案,但所有帖子都与服务器日志有关。当我运行应用程序时,会读取log4j.properties并创建日志文件,但它没有写入它们。

我使用Tomee作为服务器。我使用Wildfly的其他项目使用相同的log4j解决方案,它可以工作。

log4j.properties:

log4j.appender.TEST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.TEST.File = /opt/share/test-project/logs/test.log
log4j.appender.TEST.DatePattern = '.'yyyy-MM-dd
log4j.appender.TEST.layout = org.apache.log4j.PatternLayout
log4j.appender.TEST.layout.ConversionPattern = [%d{ISO8601}] %5p - %x - %c.%M(%L): %m%n

# Define the types of logger and level of logging    
#log4j.rootLogger.org = DEBUG,CONSOLE
log4j.logger.com.test=DEBUG, TEST

初始化的Log4jConfig类:

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.PropertyConfigurator;

@WebListener
public class Log4jConfig implements ServletContextListener {


    private final static String LO4G_FILENAME="/opt/share/test-project/config/log4j.properties"; 
    private static final Logger LOGGER = LoggerFactory.getLogger(Log4jConfig.class);

...
...
    public void contextInitialized(ServletContextEvent arg0)
    { 

        try{
            File file = new File(LO4G_FILENAME);
            if (file.exists())
            {
                PropertyConfigurator.configure(LO4G_FILENAME);
                LOGGER.info("[Log4JInitServlet - contextInitialized] - Log4J configured:"+LO4G_FILENAME);
            }

        }catch (Exception e) {
            LOGGER.error("[Log4JInitServlet - contextInitialized] - Exception >>",e);
        }

    }

}

Test.java:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

@Path("/test")
public class TestWs {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestWs.class.getName());
    //here LOGGER is null. If I put it into test() is not null but not write on the log either.

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public Response test(){

        LOGGER.debug("[TestWs - init] - init");
        long currentSystemTime = System.currentTimeMillis();

        LOGGER.error("[TestWs - test] - Error: TEST");
        LOGGER.debug("[TestWs - test] - Finish Timing:"+(System.currentTimeMillis()-currentSystemTime));

        return Response.ok().build();

    }
}

的pom.xml:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.12</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.12</version>
</dependency>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

升级slf4f的依赖关系:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.22</version>
</dependency>

private static final Logger logger = LoggerFactory.getLogger(class-name.class);用于要在其中创建日志的类。

使用bellow log4j配置: -

根记录器选项

log4j.rootLogger=INFO, file

将日志消息直接发送到日志文件

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/log.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.springframework=WARN
log4j.logger.com.amstech=DEBUG

答案 1 :(得分:0)

我不确定所有的设置,但是如果你把log4j lib / config放在容器中,你需要在conf / system.properties中设置:

openejb.logger.external=true

否则(由于遗留原因),tomee将以自定义方式设置log4j,使您的配置不受尊重。此属性仅表示“使用正常的log4j行为”。