gpt4 book ai didi

java.util.logging - Add a prefix at runtime(Logging--在运行时添加前缀)

转载 作者:bug小助手 更新时间:2023-10-25 23:19:32 27 4
gpt4 key购买 nike



I have a Java web application running on Tomcat that interprets and runs other mini Java applications (Beanshells). Both the main app and the mini apps use the default java.util.logging. The main app includes a logging.properties file that enables writing logs to a file separate from the main Tomcat logs.

我有一个运行在Tomcat上的Java Web应用程序,它解释和运行其他迷你Java应用程序(BeanShell)。主应用程序和小应用程序都使用默认的java.util.Logging。主应用程序包括一个logging.properties文件,该文件允许将日志写入独立于Tomcat主日志的文件。


At runtime within the mini app interpreter, I want to be able to place the current mini app name at the front of every log statement. Is there a way to do this with default java.util.logging?

在运行时,在迷你应用程序解释器中,我希望能够将当前的迷你应用程序名称放在每个日志语句的前面。有没有办法使用默认的java.util.Logging来实现这一点?


I have attempted to use a system property called app.name in the format string inside logging.properties, setting and clearing the property at runtime, but no dice - nothing is logged before the rest of the string. Here's the logging.properties file for the main webapp:

我尝试在logging.properties内的格式字符串中使用一个名为app.name的系统属性,在运行时设置和清除该属性,但没有任何结果--在字符串的其余部分之前不会记录任何内容。下面是主Web应用程序的logging.properties文件:


handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

org.apache.juli.FileHandler.level=INFO
org.apache.juli.FileHandler.directory=${catalina.base}/logs
org.apache.juli.FileHandler.prefix=apprunner.
org.apache.juli.FileHandler.encoding=UTF-8
org.apache.juli.FileHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.SimpleFormatter.format=${app.name} %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n

Inside the evalJava function inside com.apprunner.App:

在com.apprunner.App内的valJava函数中:


logger.info("### BEGIN JAVA EXECUTION ###");
System.setProperty("app.name", ui_object_name);
logger.info(System.getProperty("app.name"));

Resulting logs in apprunner.[date].log:

Apprunner中的结果日志。[日期].log:


Jul 19, 2023 4:09:29 PM com.apprunner.App evalJava
INFO: ### BEGIN JAVA EXECUTION ###
Jul 19, 2023 4:09:29 PM com.apprunner.App evalJava
INFO: testapp/app/testapp.java

更多回答
优秀答案推荐


At runtime within the mini app interpreter, I want to be able to place the current mini app name at the front of every log statement. Is there a way to do this with default java.util.logging?



One issue with java.util.logging.SimpleFormatter.format system property or logging property is that it is read and set once when the class is loaded and is forever that value.

Java.util.logging.SimpleFormatter.Format系统属性或日志记录属性的一个问题是,它在类加载时被读取和设置一次,并且永远是该值。


To build something dynamic you have to write your own java.util.logging.Formatter or find a 3rd party formatter:

要构建动态的东西,您必须编写自己的java.util.logging.ForMatter或寻找第三方格式化程序:


public class AppFormatter extends SimpleFormatter {
public String format(LogRecord record) {
return System.getProperty("app.name") + " " + super.format(record);
}
}

Then in your configuration file you have to replace the use of SimpleFormatter with full qualified name of your AppFormatter.

然后,在您的配置文件中,您必须将SimpleForMatter的用法替换为您的AppForMatter的全限定名。


更多回答

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