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

Tomee - slf4j create logs file but not writing on them

我想为我的应用程序使用 log4j(我现在不需要处理服务器日志)。我搜索了一个解决方案,但所有 post 都与服务器日志有关。当我 运行 应用程序时, 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 class:

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>

有什么想法吗?

升级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); 用于要在其中创建日志的 class。

使用下面的 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

我不确定所有设置,但如果您将 log4j lib/config 放入容器中,您可能需要在 conf/system.properties:

中进行设置
openejb.logger.external=true

否则(出于遗留原因),tomee 将以自定义方式设置 log4j,使您的配置不受尊重。这个 属性 就意味着 "use normal log4j behavior".