gpt4 book ai didi

java - Glassfish 3 : how do you change the (default) logging format?

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:06 25 4
gpt4 key购买 nike

问题源自这里:http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format - 没有答案。

默认的 GlassFish 3 日志记录格式非常烦人,太长了。

[#|2012-03-02T09:22:03.165+0100|SEVERE|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=113;_ThreadName=AWT-EventQueue-0;| MESSAGE... ]

这只是一个可怕的默认 IMO。文档只解释了所有字段,但没有说明如何更改格式:http://docs.oracle.com/cd/E18930_01/html/821-2416/abluk.html

请注意,我将 SLF4J 与我的 web 应用程序一起部署,它也应该采用该格式。

如何更改日志记录格式?

仅供引用:

此处的链接已过时:Install log formater in glassfish ...

此处的问题尚未得到解答:How to configure GlassFish logging to show milliseconds in timestamps? ...

此处的发布没有结果:http://www.java.net/forum/topic/glassfish/glassfish/cant-seem-configure- ...

看起来 GlassFish 日志记录配置本身就是一个问题。 有人可以帮忙吗?

最佳答案

解决方案似乎是这里发布的第一个 SO:Install log formater in glassfish

我已经破解了一个简单的日志格式化程序(随意调整):

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

public class JBossAS7LikeFormatter extends SimpleFormatter
{
private final String nl = System.getProperty("line.separator");
private final DateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");

@Override
public synchronized String format(LogRecord lr)
{
StringBuffer sb = new StringBuffer();

// time only
Date dt = new Date();
dt.setTime(lr.getMillis());

sb.append(df.format(dt));
sb.append(" ");

// level (longest is "WARNING" = 7 chars, space fill for level output)
String level = lr.getLevel().getName();

int numSpaces = 7 - level.length();

sb.append(level);

for ( int i = 0 ; i < numSpaces + 1 ; i++ )
{
sb.append(" ");
}

// package
sb.append("[");
sb.append(lr.getSourceClassName());
sb.append("] ");

// thread (name?)
sb.append("(");
sb.append(lr.getThreadID());
sb.append(") ");

// message
sb.append(formatMessage(lr));
sb.append(nl);

// optional stack trace
if ( lr.getThrown() != null )
{
try
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
lr.getThrown().printStackTrace(pw);
pw.close();
sb.append(sw.toString());
}
catch ( Exception e )
{
}
}

return sb.toString();
}

}

它甚至修复了 Windows 机器上的双换行问题。


我通过将 JAR 放入 domain/lib/ext 一开始 来让它工作,但是对于较新的尝试,无论是什么原因,我只会不断得到一个ClassNotFoundException 现在:

Mrz 08, 2012 9:39:14 AM com.sun.enterprise.admin.launcher.GFLauncherLogger info
Information: Successfully launched in 5 msec.
Launching GlassFish on Felix platform
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.enterprise.glassfish.bootstrap.GlassFishMain.main(GlassFishMain.java:97)
at com.sun.enterprise.glassfish.bootstrap.ASMain.main(ASMain.java:55)
Caused by: java.lang.NoClassDefFoundError: com/sun/enterprise/server/logging/UniformLogFormatter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:410)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.util.logging.LogManager.getFormatterProperty(LogManager.java:1048)
at java.util.logging.StreamHandler.configure(StreamHandler.java:76)
at java.util.logging.StreamHandler.<init>(StreamHandler.java:94)
at java.util.logging.ConsoleHandler.<init>(ConsoleHandler.java:88)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at java.util.logging.LogManager$3.run(LogManager.java:419)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:405)
at java.util.logging.LogManager.initializeGlobalHandlers(LogManager.java:1076)
at java.util.logging.LogManager.access$1100(LogManager.java:148)
at java.util.logging.LogManager$RootLogger.getHandlers(LogManager.java:1157)
at java.util.logging.Logger.log(Logger.java:521)
at com.sun.logging.LogDomains$1.log(LogDomains.java:372)
at java.util.logging.Logger.doLog(Logger.java:543)
at java.util.logging.Logger.log(Logger.java:587)
at com.sun.enterprise.server.logging.LogManagerService.postConstruct(LogManagerService.java:354)
at com.sun.hk2.component.AbstractCreatorImpl.inject(AbstractCreatorImpl.java:131)
at com.sun.hk2.component.ConstructorCreator.initialize(ConstructorCreator.java:91)
at com.sun.hk2.component.AbstractCreatorImpl.get(AbstractCreatorImpl.java:82)
at com.sun.hk2.component.SingletonInhabitant.get(SingletonInhabitant.java:67)
at com.sun.hk2.component.EventPublishingInhabitant.get(EventPublishingInhabitant.java:139)
at com.sun.hk2.component.AbstractInhabitantImpl.get(AbstractInhabitantImpl.java:78)
at com.sun.enterprise.v3.server.AppServerStartup.run(AppServerStartup.java:229)
at com.sun.enterprise.v3.server.AppServerStartup.doStart(AppServerStartup.java:145)
at com.sun.enterprise.v3.server.AppServerStartup.start(AppServerStartup.java:136)
at com.sun.enterprise.glassfish.bootstrap.GlassFishImpl.start(GlassFishImpl.java:79)
at com.sun.enterprise.glassfish.bootstrap.GlassFishDecorator.start(GlassFishDecorator.java:63)
at com.sun.enterprise.glassfish.bootstrap.osgi.OSGiGlassFishImpl.start(OSGiGlassFishImpl.java:69)
at com.sun.enterprise.glassfish.bootstrap.GlassFishMain$Launcher.launch(GlassFishMain.java:117)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.server.logging.UniformLogFormatter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 53 more
Completed shutdown of GlassFish runtime
Command start-domain failed.
The DAS was stopped.

我尝试了几次新格式,但它停止工作了——很奇怪。

我已将 JAR 附加在我最初的 GlassFish 论坛帖子中。我基本上只是使用了我的 ZIP 工具并将其重命名为 JAR,但这应该不是问题 (?)。如果有人让它(再次)工作,请告诉我。

注意:我正在使用 GlassFish 插件来启动/停止服务器,不知道这是否会成为问题。

在这里继续 GlassFish 论坛中的任何讨论:http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format

PS:抱歉,由于 GF 论坛有些损坏,我需要在这里发布堆栈跟踪。

更新:

扩展 java.util.logging.SimpleFormatter 达到了目的。 GlassFish 现在生成如下日志条目:

12:13:02,400 INFO    [com.sun.enterprise.web.WebContainer] (1) WEB0172: Virtual server [server] loaded default web module []
12:13:07,700 INFO [org.hibernate.validator.util.Version] (1) Hibernate Validator 4.2.0.Final
12:13:08,919 WARNING [null] (1) Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@1a53cab6].
12:13:08,920 WARNING [null] (1) JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@1a53cab6] from index [0]
12:13:08,920 WARNING [null] (1) JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@2f740f7e] from index [1]

同样,在 Windows 上:没有双换行符。

关于java - Glassfish 3 : how do you change the (default) logging format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9609380/

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