简体   繁体   English

GlassFish 3:如何更改(默认)日志记录格式?

[英]GlassFish 3: how do you change the (default) logging format?

The question originated from here: http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format - without an answer.问题来自这里: http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format - 没有答案。

The default GlassFish 3 logging format is very annoying, much too long.默认的 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... ]

This is just a horrible default IMO.这只是一个可怕的默认 IMO。 The docs just explain all the fields, but not how to change the format: http://docs.oracle.com/cd/E18930_01/html/821-2416/abluk.html文档只是解释了所有字段,但没有解释如何更改格式: http://docs.oracle.com/cd/E18930_01/html/821-2416/abluk.ZFC3568ZE8A3D

Note, that I deploy SLF4J along with my webapp which should pick up the format as well.请注意,我将 SLF4J 与我的 webapp 一起部署,它也应该采用这种格式。

How do you change the logging format?您如何更改日志记录格式?

FYI:供参考:

The links here are outdated: Install log formater in glassfish ...此处的链接已过时: 在 glassfish 中安装日志格式化程序...

The question here hasn't been answered: How to configure GlassFish logging to show milliseconds in timestamps?这里的问题尚未得到解答: 如何配置 GlassFish 日志记录以在时间戳中显示毫秒? ... ...

The posting here resulted in nothing: http://www.java.net/forum/topic/glassfish/glassfish/cant-seem-configure- ...这里的帖子一无所获: http://www.java.net/forum/topic/glassfish/glassfish/cant-seem-configure- ...

It looks like GlassFish logging configuration is an issue of its own.看起来 GlassFish 日志记录配置本身就是一个问题。 Can anybody help?有人可以帮忙吗?

The solution seems to be the first SO posting here: Install log formater in glassfish 该解决方案似乎是第一个在此发布的SO: 在glassfish中安装日志格式化程序

I've hacked together a simple log formatter (adjust at will): 我已经将一个简单的日志格式化程序一起攻击(随意调整):

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();
    }

}

It even fixes the double newline problem on Windows machines. 它甚至修复了Windows机器上的双重换行问题。


I got this to work by putting the JAR into domain/lib/ext at first , but for newer tries, whatever the reason is, I only keep getting a ClassNotFoundException now: 我首先将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.

I saw the new format for a few tries, but the it stopped to work - strange. 我看了几次尝试的新格式,但它停止工作 - 奇怪。

I've attached the JAR as is in my original GlassFish forum post. 我在原版GlassFish论坛帖子中附加了JAR。 I've basically just used my ZIP tool and renamed it to JAR, but this shouldn't be a problem (?). 我基本上只使用了我的ZIP工具并将其重命名为JAR,但这应该不是问题(?)。 If anybody gets it to work (again) please let me know. 如果有人让它(再次)工作,请告诉我。

Note: I'm using the GlassFish plugin to start/stop the server, don't know if this could be an issue. 注意:我正在使用GlassFish插件来启动/停止服务器,不知道这是否是一个问题。

Please continue any discussions in the GlassFish forum here: http://www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format 继续在GlassFish论坛中进行任何讨论: http//www.java.net/forum/topic/glassfish/glassfish/configuring-glassfish-logging-format

PS: sorry I needed to post the stack trace here as the GF forum is somewhat broken. PS:对不起,我需要在这里发布堆栈跟踪,因为GF论坛有些破碎。

UPDATE : 更新

Extending java.util.logging.SimpleFormatter did the trick. 扩展java.util.logging.SimpleFormatter就可以了。 GlassFish now produces log entries like: 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] 

Again, on Windows: no double newlines. 再次,在Windows上:没有双重换行。

The default logging format can be changed as per the post above. 可以根据上面的帖子更改默认日志记录格式。 The NoClassDefFoundError reported above can be avoided by extending java.util.logging.Formatter rather than the com.sun.enterprise.server.logging.UniformLogFormatter. 通过扩展java.util.logging.Formatter而不是com.sun.enterprise.server.logging.UniformLogFormatter可以避免上面报告的NoClassDefFoundError。

To summarize: 总结一下:

  1. Implement a custom formatter which extends java.util.logging.Formatter 实现一个扩展java.util.logging.Formatter的自定义格式化程序
  2. Copy the jar containing custom formatter to domain/lib/ext 将包含自定义格式化程序的jar复制到domain/lib/ext
  3. Specify the log formatter in logging.properties , eg. logging.properties指定日志格式化程序,例如。

     com.sun.enterprise.server.logging.GFFileHandler.formatter=com.myformatter.CustomFormatter 

Following all the guides and failing, what finally got this to work for me in GF 3.1.2.2 was to update the correct property in logging.properties as explained above: 遵循所有指南并失败,最终在GF 3.1.2.2中为我工作的是更新logging.properties中的正确属性,如上所述:

com.sun.enterprise.server.logging.GFFileHandler.formatter=com.myformatter.CustomFormatter com.sun.enterprise.server.logging.GFFileHandler.formatter = com.myformatter.CustomFormatter

All guides I've seen have talked about updating the "handlers" property, but this was not neccessary. 我见过的所有指南都谈到了更新“处理程序”属性,但这不是必要的。 The property above is the only property I've changed. 上面的房产是我改变的唯一房产。

Oh; 哦; and my custom formatter extends SimpleFormatter. 我的自定义格式化程序扩展了SimpleFormatter。

You need to supply your own logging implementation since there doesn't seem to be a way to change glassfish's. 您需要提供自己的日志记录实现,因为似乎没有办法更改glassfish。 SLF4J is just a facade and doesn't actually do any logging, it forwards it to the logger that is present on your classpath. SLF4J只是一个外观,实际上并没有进行任何记录,而是将它转发到类路径中的记录器。

What I've done in my app is replace the entire glassfish logging with logback following this post. 我在我的应用程序中所做的是在此帖后用logback替换整个glassfish日志记录。 http://hwellmann.blogspot.com/2010/12/glassfish-logging-with-slf4j-part-2.html http://hwellmann.blogspot.com/2010/12/glassfish-logging-with-slf4j-part-2.html

Since GlassFish 7.0.0-M8 you can switch the formatter as you wish for any handler used.从 GlassFish 7.0.0-M8 开始,您可以根据需要为使用的任何处理程序切换格式化程序。 In a simplest case you can use SimpleFormatter and set some custom format for it.在最简单的情况下,您可以使用SimpleFormatter并为其设置一些自定义格式。 Also the GlassFish documentation improved. GlassFish 文档也得到了改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改Java Transformer中的默认日志记录 - How do I change the default logging in Java Transformer 是否可以覆盖Glassfish v3的默认日志记录? - Is it possible to override the default logging for Glassfish v3? 如何使用vertx-default-jul-logging.properties文件控制文件的格式和数量? - How do I use of the vertx-default-jul-logging.properties file to control the format and number of files? android studio 中的默认拨号器 package 如何更改? - How do you change the default dialer package in android studio? Hibernate:如何调整默认日志记录级别(SJF4J和JDK 1.4记录器)? - Hibernate: how do you adjust the default logging level (SJF4J and JDK 1.4 logger)? 您如何更改 Swagger 文档中的日期格式? - How do you change the date format in Swagger documentation? 如何更改Glassfish应用程序服务器的默认浏览器图标? - How to change glassfish application server default browser icon? 无法更改GlassFish中的TopLink日志记录级别 - Unable to change TopLink logging level in GlassFish 如何配置 Glassfish 5 以使用 Moxy 作为默认提供程序? - How do I configure Glassfish 5 to use Moxy as the default Provider? 如何在Glassfish中获取远程EJB客户端的IP地址? - How do you get the ip address of a remote EJB client in Glassfish?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM