2222
2323import org .jruby .RubyClass ;
2424import org .jruby .RubyModule ;
25- import org .jruby .RubyString ;
2625import org .jruby .anno .JRubyMethod ;
2726import org .jruby .anno .JRubyModule ;
28- import org .jruby .runtime .Block ;
2927import org .jruby .runtime .ThreadContext ;
3028import org .jruby .runtime .builtin .IRubyObject ;
3129import org .jruby .runtime .builtin .InstanceVariables ;
3230import org .logstash .RubyUtil ;
3331
34- import static org .logstash .RubyUtil .RUBY ;
32+ import java .util .Locale ;
33+
34+ import static org .logstash .log .SlowLoggerExt .toLong ;
3535
3636@ JRubyModule (name = "Loggable" )
3737public final class LoggableExt {
@@ -64,24 +64,18 @@ public static IRubyObject deprecationLogger(final ThreadContext context, final I
6464 return self .getSingletonClass ().callMethod (context , "deprecation_logger" );
6565 }
6666
67- private static RubyString log4jName (final ThreadContext context , final RubyModule self ) {
68- IRubyObject name = self . name ( context ) ;
69- if (name . isNil () ) {
70- final RubyClass clazz ;
67+ private static String log4jName (final RubyModule self ) {
68+ String name ;
69+ if (self . getBaseName () == null ) { // anonymous module/class
70+ RubyModule real = self ;
7171 if (self instanceof RubyClass ) {
72- clazz = ((RubyClass ) self ).getRealClass ();
73- } else {
74- clazz = self .getMetaClass ();
75- }
76- name = clazz .name (context );
77- if (name .isNil ()) {
78- name = clazz .to_s ();
72+ real = ((RubyClass ) self ).getRealClass ();
7973 }
74+ name = real .getName (); // for anonymous: "#<Class:0xcafebabe>"
75+ } else {
76+ name = self .getName ();
8077 }
81- return ((RubyString ) ((RubyString ) name ).gsub (
82- context , RUBY .newString ("::" ), RUBY .newString ("." ),
83- Block .NULL_BLOCK
84- )).downcase (context );
78+ return name .replace ("::" , "." ).toLowerCase (Locale .ENGLISH );
8579 }
8680
8781 /**
@@ -105,9 +99,8 @@ public static IRubyObject logger(final ThreadContext context, final IRubyObject
10599 }
106100 IRubyObject logger = instanceVariables .getInstanceVariable ("logger" );
107101 if (logger == null || logger .isNil ()) {
108- logger = RubyUtil .LOGGER .callMethod (context , "new" ,
109- LoggableExt .log4jName (context , (RubyModule ) self )
110- );
102+ final String loggerName = log4jName ((RubyModule ) self );
103+ logger = RubyUtil .LOGGER .callMethod (context , "new" , context .runtime .newString (loggerName ));
111104 instanceVariables .setInstanceVariable ("logger" , logger );
112105 }
113106 return logger ;
@@ -117,18 +110,15 @@ public static IRubyObject logger(final ThreadContext context, final IRubyObject
117110 public static SlowLoggerExt slowLogger (final ThreadContext context ,
118111 final IRubyObject self , final IRubyObject [] args ) {
119112 final InstanceVariables instanceVariables = self .getInstanceVariables ();
120- SlowLoggerExt logger =
121- (SlowLoggerExt ) instanceVariables .getInstanceVariable ("slow_logger" );
113+ IRubyObject logger = instanceVariables .getInstanceVariable ("slow_logger" );
122114 if (logger == null || logger .isNil ()) {
123- logger = new SlowLoggerExt (context .runtime , RubyUtil .SLOW_LOGGER ).initialize (
124- context , new IRubyObject []{
125- LoggableExt .log4jName (context , (RubyModule ) self ), args [0 ], args [1 ],
126- args [2 ], args [3 ]
127- }
115+ final String loggerName = log4jName ((RubyModule ) self );
116+ logger = new SlowLoggerExt (context .runtime , RubyUtil .SLOW_LOGGER , loggerName ,
117+ toLong (args [0 ]), toLong (args [1 ]), toLong (args [2 ]), toLong (args [3 ])
128118 );
129119 instanceVariables .setInstanceVariable ("slow_logger" , logger );
130120 }
131- return logger ;
121+ return ( SlowLoggerExt ) logger ;
132122 }
133123
134124 @ JRubyMethod (name = "deprecation_logger" , meta = true )
@@ -141,8 +131,8 @@ public static IRubyObject deprecationLogger(final ThreadContext context, final I
141131 }
142132 IRubyObject logger = instanceVariables .getInstanceVariable ("deprecation_logger" );
143133 if (logger == null || logger .isNil ()) {
144- logger = new DeprecationLoggerExt ( context . runtime , RubyUtil . DEPRECATION_LOGGER )
145- . initialize (context , LoggableExt . log4jName ( context , ( RubyModule ) self ) );
134+ final String loggerName = log4jName (( RubyModule ) self );
135+ logger = new DeprecationLoggerExt (context . runtime , RubyUtil . DEPRECATION_LOGGER , loggerName );
146136 instanceVariables .setInstanceVariable ("deprecation_logger" , logger );
147137 }
148138 return logger ;
0 commit comments