在REST API上关闭FileHandler的最佳方法

我不知道如何在API REST上处理FileHandler

我在weblogic处安装了REST API,正在使用Java日志记录,当我的应用程序启动时,将启动记录器,然后打开FileHandler。 就像我从不关闭FileHandler一样,创建的package main import ( "crypto/tls" "database/sql" "github.com/SAP/go-hdb/driver" _ "github.com/SAP/go-hdb/driver" "log" ) const ( HOST = "host" PORT = ":port" username = "user" PASSWORD = "password" ) func main() { c := driver.NewBasicAuthConnector( HOST+PORT,username,PASSWORD) tlsConfig := tls.Config{ InsecureSkipVerify: false,ServerName: HOST,} c.SetTLSConfig(&tlsConfig) db := sql.OpenDB(c) var id int var name string res := db.QueryRow("SELECT * FROM SCHEMA.TABLE LIMIT 1") res.Scan(&id,&name) log.Println("res ",id,name) } 文件仍位于我的日志文件夹中。我真的不在乎该文件的存在,但是当我重新部署该应用程序时,就像FileHandler仍然打开时,我的应用程序将启动一个新的日志文件(例如:.lckmyLog.log.0)。我已经读过JVM本身应该关闭FileHandler,但是那没有发生。 我曾尝试myLog.log.1关闭FileHandler,但是该代码无法正常工作,如果我重新部署该应用程序,它仍然保持打开状态。

addShutodownHook

我的@ApplicationPath("api") public class GenericApplication extends Application { public GenericApplication() { initSwagger(); initLog(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { CtgLogger.fileJson.close(); // fileJson is my FileHandler,i made it public static to call him here. } }); } 方法仅调用下一个initLog() ...

CtgLogger.setup()

仅此而已,然后我只需呼叫端点并使用记录器即可。

我的问题是,每次调用端点时都应该打开和关闭FileHandler吗?还是有更好的方法呢?

woaixu13142 回答:在REST API上关闭FileHandler的最佳方法

  

我的问题是,每次调用端点时都应该打开和关闭FileHandler吗?还是有更好的方法呢?

无需在每次调用端点时打开和关闭。

  1. 删除关机挂钩代码。 LogManager将在关机时为您关闭任何已附加文件处理程序。
  2. logger更改为静态最终引用,使其为impossible to garbage collect。这样可以确保您的设置得到保留。
  3. 由于您正在使用JavaEE,因此请使用javax.annotation.PostConstructjavax.annotation.PreDestroy批注来管理记录器设置。
  4. 如果手动关闭FileHandler,请确保调用Logger::remove(Handler),以便可以对垃圾处理程序进行垃圾收集。您当前的代码仅关闭处理程序。
    private final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private volatile FileHandler fileJson;

    public void preDestroy() {
       Handler h = this.fileJson;
       this.fileJson = null;
       if (h != null) {
           h.close();
       }
       logger.removeHandler(h);  
    }

    @PostConstruct 
    public void setup() throws IOException {
        if (fileJson != null) {
            preDestroy();
        }

        String level = PropertiesUtil.get("ctg.log.level");
        logger.setLevel(LEVEL_MAP.get(level));

        String filePath = PropertiesUtil.get("ctg.log.path");
        String fileSize = PropertiesUtil.get("ctg.log.max.size");
        String fileCount = PropertiesUtil.get("ctg.log.max.count");
        if (StringUtils.isNotEmpty(fileSize) && StringUtils.isNotEmpty(fileSize) &&
        NumberUtils.isNumber(fileSize) && NumberUtils.isNumber(fileCount)) {
            fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME,Integer.parseInt(fileSize),Integer.parseInt(fileCount),true);
        } else {
               fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME);
        }
        jsonFormatter = new JsonCustomFormatter();
        fileJson.setFormatter(jsonFormatter);
        for (Handler h: logger.getHandlers()) {
            h.close();
            logger.removeHander(h);
        }
        logger.addHandler(fileJson);
    }
本文链接:https://www.f2er.com/3159453.html

大家都在问