如何在groovy中为类方法名配置java.util.logging?

如何在groovy中为类方法名配置java.util.logging?,第1张

概述testlog. java: import java.util.logging.Logger;public class testlog { private final static Logger log = Logger.getLogger(testlog.class.getName()); public static void main(String[] args) { testlog. java:

import java.util.logging.Logger;public class testlog {    private final static Logger log = Logger.getLogger(testlog.class.getname());    public static voID main(String[] args) {        System.setProperty("java.util.logging.SimpleFormatter.format","[%1$tF %1$tT]:%4$s:(%2$s): %5$s%n");        log.info("logging test");    }}

执行:

$java testlog[2014-01-18 11:08:51]:INFO:(testlog main): logging test

testlog.groovy:

import java.util.logging.Logger;public class testlog {    private final static Logger log = Logger.getLogger("testlog");    public static voID main(String[] args) {        System.setProperty("java.util.logging.SimpleFormatter.format",'[%1$tF %1$tT]:%4$s:(%2$s): %5$s%n');        log.info("logging test");    }}

执行:

$groovy testlog[2014-01-18 11:11:23]:INFO:(java_util_logging_Logger$info call): logging test

如何为类/方法名称而不是“java_util_logging_Logger $info call”配置groovy?

解决方法 一般来说,Groovy在字节码中使用 CallSite.上面提到的Groovy代码将编译为以下格式的字节码:

import groovy.lang.GroovyObject;import groovy.lang.MetaClass;import java.util.logging.Logger;import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;import org.codehaus.groovy.runtime.callsite.CallSite;public class testlog  implements GroovyObject{  private static final Logger log;  static  {    __$swAPInit();long l1 = 0L;__timeStamp__239_neverHappen1390064566496 = l1;      long l2 =         1390064566495L;__timeStamp = l2;       Object localObject =         $getCallSiteArray()           [2].call(Logger.class,"testlog");log =            (Logger)ScriptBytecodeAdapter.castToType(localObject,Logger.class);  }  public static voID main(String... args)  {    CallSite[] arrayOfCallSite =       $getCallSiteArray();arrayOfCallSite[0].call(System.class,"java.util.logging.SimpleFormatter.format","[%1$tF %1$tT]:%4$s:(%2$s): %5$s%n");    arrayOfCallSite[1].call(log,"logging test");  }  public testlog()  {    testlog this;    CallSite[] arrayOfCallSite = $getCallSiteArray();    MetaClass localMetaClass = $getStaticMetaClass();    this.MetaClass = localMetaClass;  }}

因此,它指的是格式化程序引用时的Log类.要解决此问题,您可以在类级别使用CompileStatic注释,它将源代码转换为普通Java约定中的字节码.

以下是脚本的外观(注意: – 类名是CamelCase):

import java.util.logging.Loggerimport groovy.transform.CompileStatic@CompileStaticpublic class Testlog {    private final static Logger log = Logger.getLogger("testlog")    public static voID main(String[] args) {        System.setProperty("java.util.logging.SimpleFormatter.format",'[%1$tF %1$tT]:%4$s:(%2$s): %5$s%n')        log.info("logging test")    }}
总结

以上是内存溢出为你收集整理的如何在groovy中为类/方法名配置java.util.logging?全部内容,希望文章能够帮你解决如何在groovy中为类/方法名配置java.util.logging?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1220415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存