Is there any possible log separate files for different package in the Glassfish

大兔子大兔子 提交于 2019-12-04 02:16:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22447430/is-there-any-possible-log-separate-files-for-different-package-in-the-glassfish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!