gpt4 book ai didi

java - JBOSS自定义登录

转载 作者:行者123 更新时间:2023-12-01 13:40:50 24 4
gpt4 key购买 nike

大家好!
我的记录器有问题。我想登录到自定义文件,但这些文件是空的。有人可以帮忙吗?
基本记录器:

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import java.io.File;
import java.io.IOException;

public class BaseLogger {
private Logger logger;

private BaseLogger(){

}

public BaseLogger(String loggerName) {
this.logger = Logger.getLogger(loggerName);
try {
this.logger.addAppender(createAppender(loggerName + "_APP.LOG"));
} catch (IOException e) {
e.printStackTrace();
}
logger.info("logger init.");
}

public void logInfo(String info){
logger.info(info);
}

public void logInfo(String info, Throwable t){
logger.info(info, t);
}

public void logError(String error) {
logger.error(error);
}

public void logError(String error, Throwable t) {
logger.error(error, t);
}

public void logFatal(String fatal) {
logger.fatal(fatal);
}

public void logFatal(String fatal, Throwable t) {
logger.fatal(fatal, t);
}

private FileAppender createAppender(String fileName) throws IOException {
return new FileAppender(new PatternLayout(), System.getProperty("jboss.server.log.dir") + File.separator + fileName);
}
}


记录器类:

public class ServicesLogger {
public static final BaseLogger GENERAL_LOGGER = new BaseLogger("GENERAL_LOGGER");
public static final BaseLogger SERVICE_EXECUTOR = new BaseLogger("SERVICE_EXECUTOR");
}


当我调用 ServicesLogger.SERVICE_EXECUTOR.logInfo("info"); 时在 jboss 控制台中出现此文本,但在文件中没有。有什么问题吗?

最佳答案

您只能在代码中添加以下内容:

import org.apache.log4j.Logger;
...
public class SomeClass {
private Logger logger = Logger.getLogger("GENERAL_LOGGER");
...
logger.info("some info");
}

然后将appender添加到服务器配置文件中:

<subsystem xmlns="urn:jboss:domain:logging:1.1">
...
<periodic-rotating-file-handler name="GENERAL_LOGGER_handler">
<formatter>
<pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="GENERAL_LOGGER_APP.LOG"/>
<suffix value=".yyyy-MM-dd"/>
<append value="true"/>
</periodic-rotating-file-handler>
<logger category="GENERAL_LOGGER">
<level name="DEBUG"/>
<handlers>
<handler name="GENERAL_LOGGER_handler"/>
</handlers>
</logger>
...

关于java - JBOSS自定义登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com