25
25
public class VertxContext implements Context , ServiceRegistryAwareService {
26
26
27
27
private static final Log LOG = LoggerFactory .make ( Log .class , MethodHandles .lookup () );
28
+ private static final boolean trace = LOG .isTraceEnabled ();
28
29
29
30
private VertxInstance vertxInstance ;
30
31
@@ -37,11 +38,11 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
37
38
public <T > void put (Key <T > key , T instance ) {
38
39
final io .vertx .core .Context context = Vertx .currentContext ();
39
40
if ( context != null ) {
40
- LOG .tracef ( "Putting key,value in context: [%1$s, %2$s]" , key , instance );
41
+ if ( trace ) LOG .tracef ( "Putting key,value in context: [%1$s, %2$s]" , key , instance );
41
42
context .putLocal ( key , instance );
42
43
}
43
44
else {
44
- LOG .tracef ( "Context is null for key,value: [%1$s, %2$s]" , key , instance );
45
+ if ( trace ) LOG .tracef ( "Context is null for key,value: [%1$s, %2$s]" , key , instance );
45
46
throw LOG .notVertxContextActive ();
46
47
}
47
48
}
@@ -51,11 +52,11 @@ public <T> T get(Key<T> key) {
51
52
final io .vertx .core .Context context = Vertx .currentContext ();
52
53
if ( context != null ) {
53
54
T local = context .getLocal ( key );
54
- LOG .tracef ( "Getting value %2$s from context for key %1$s" , key , local );
55
+ if ( trace ) LOG .tracef ( "Getting value %2$s from context for key %1$s" , key , local );
55
56
return local ;
56
57
}
57
58
else {
58
- LOG .tracef ( "Context is null. Returning null for key %s" , key );
59
+ if ( trace ) LOG .tracef ( "Context is null. Returning null for key %s" , key );
59
60
return null ;
60
61
}
61
62
}
@@ -65,28 +66,28 @@ public void remove(Key<?> key) {
65
66
final io .vertx .core .Context context = Vertx .currentContext ();
66
67
if ( context != null ) {
67
68
boolean removed = context .removeLocal ( key );
68
- LOG .tracef ( "Key %s removed from context: %s" , key , removed );
69
+ if ( trace ) LOG .tracef ( "Key %s removed from context: %s" , key , removed );
69
70
}
70
71
else {
71
- LOG .tracef ( "Context is null, nothing to remove for key %s" , key );
72
+ if ( trace ) LOG .tracef ( "Context is null, nothing to remove for key %s" , key );
72
73
}
73
74
}
74
75
75
76
@ Override
76
77
public void execute (Runnable runnable ) {
77
78
final io .vertx .core .Context currentContext = Vertx .currentContext ();
78
79
if ( currentContext == null ) {
79
- LOG .tracef ( "Not in a Vert.x context, checking the VertxInstance service" );
80
+ if ( trace ) LOG .tracef ( "Not in a Vert.x context, checking the VertxInstance service" );
80
81
final io .vertx .core .Context newContext = vertxInstance .getVertx ().getOrCreateContext ();
81
82
// Ensure we don't run on the root context, which is globally scoped:
82
83
// that could lead to unintentionally share the same session with other streams.
83
84
ContextInternal newContextInternal = (ContextInternal ) newContext ;
84
85
final ContextInternal duplicate = newContextInternal .duplicate ();
85
- LOG .tracef ( "Using duplicated context from VertxInstance: %s" , duplicate );
86
+ if ( trace ) LOG .tracef ( "Using duplicated context from VertxInstance: %s" , duplicate );
86
87
duplicate .runOnContext ( x -> runnable .run () );
87
88
}
88
89
else {
89
- LOG .tracef ( "Running in the current Vert.x context %s" , currentContext );
90
+ if ( trace ) LOG .tracef ( "Running in the current Vert.x context %s" , currentContext );
90
91
runnable .run ();
91
92
}
92
93
}
0 commit comments