问题描述
我们正在使用glassfish作为我们的应用服务器。我们希望单独记录消息。例如,如果来自xxx.company.xxx.service包的日志,日志文件被命名为service.log,并且如果来自xxx.company.xxx.dao的日志,该日志文件被命名为dao。日志。
现在,所有的日志消息都会打印到server.log文件中,该文件是glassfish服务器日志文件。
我知道glassfish使用java.util.logging API,如何配置log.propeties文件以及如何使用这个记录器API。 Glassfish是否具有类似Apache Tomcat Juli logger的功能?
FileHandler 不支持通过LogManager的软件包名称生成文件名。 ,它将在每次发布时创建并关闭一个FileHandler。 p> public class PackageNameFileHandler extends Handler {
@Override
public synchronized void publish (LogRecord r){
if(isLoggable(r)){
try {
FileHandler h = new FileHandler(fileName(r),Integer.MAX_VALUE,1,true);
尝试{
h.setLevel(getLevel());
h.setEncoding(getEncoding());
h.setFilter(null);
h.setFormatter(getFormatter());
h.setErrorManager(getErrorManager());
h.publish(r);
} finally {
h.close();
}
} catch(IOException | SecurityException jm){
this.reportError(null,jm,ErrorManager.WRITE_FAILURE);
$ @覆盖
public void flush(){
}
@Override
public void close(){
super.setLevel(Level.OFF);
private String fileName(LogRecord r){
try {
String cn = r.getSourceClassName();
if(cn == null){
cn = String.valueOf(r.getLoggerName());
}
//查找包名称。
int index = cn.lastIndexOf('。');
if(index> -1){
cn = cn.substring(0,index);
}
返回新文件(cn).getCanonicalPath();
} catch(IOException invalidFileName){
returnunknown.log;
}
}}
更快的版本将创建一个处理程序池但是,这样写起来更容易。
We are using glassfish as our application server. We want to log messages separately. For example if the log from xxx.company.xxx.service package, the log file is named as service.log, and if the log from xxx.company.xxx.dao, the log file is named as dao.log.
Right now, all the log message are printed to server.log file which is the glassfish server log file.
I know glassfish is using java.util.logging API, how to configure the log.propeties file and how to use this logger API. Does glassfish have similar functionality like Apache Tomcat Juli logger?
The FileHandler doesn't support generating file names by package name from the LogManager. The com.sun.enterprise.server.logging.GFFileHandler doesn't seen to support it either.
If you want to generate a file name per each package you can create a custom Handler that will create and close a FileHandler on each publish.
public class PackageNameFileHandler extends Handler {
@Override
public synchronized void publish(LogRecord r) {
if (isLoggable(r)) {
try {
FileHandler h = new FileHandler(fileName(r), Integer.MAX_VALUE, 1, true);
try {
h.setLevel(getLevel());
h.setEncoding(getEncoding());
h.setFilter(null);
h.setFormatter(getFormatter());
h.setErrorManager(getErrorManager());
h.publish(r);
} finally {
h.close();
}
} catch (IOException | SecurityException jm) {
this.reportError(null, jm, ErrorManager.WRITE_FAILURE);
}
}
}
@Override
public void flush() {
}
@Override
public void close() {
super.setLevel(Level.OFF);
}
private String fileName(LogRecord r) {
try {
String cn = r.getSourceClassName();
if (cn == null) {
cn = String.valueOf(r.getLoggerName());
}
//Find package name.
int index = cn.lastIndexOf('.');
if (index > -1) {
cn = cn.substring(0, index);
}
return new File(cn).getCanonicalPath();
} catch (IOException invalidFileName) {
return "unknown.log";
}
}}
A much faster version would create a pool of handlers on demand but, this is easier to write up.
这篇关于Glassfish中是否有可能为不同的包提供日志单独的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!