37
37
*/
38
38
public class ContextInitializer {
39
39
40
- final public static String AUTOCONFIG_FILE = DefaultJoranConfigurator .AUTOCONFIG_FILE ;
41
- final public static String TEST_AUTOCONFIG_FILE = DefaultJoranConfigurator .TEST_AUTOCONFIG_FILE ;
40
+ /**
41
+ * @deprecated Please use ClassicConstants.AUTOCONFIG_FILE instead
42
+ */
43
+ final public static String AUTOCONFIG_FILE = ClassicConstants .AUTOCONFIG_FILE ;
44
+ /**
45
+ * @deprecated Please use ClassicConstants.TEST_AUTOCONFIG_FILE instead
46
+ */
47
+ final public static String TEST_AUTOCONFIG_FILE = ClassicConstants .TEST_AUTOCONFIG_FILE ;
42
48
/**
43
49
* @deprecated Please use ClassicConstants.CONFIG_FILE_PROPERTY instead
44
50
*/
45
51
final public static String CONFIG_FILE_PROPERTY = ClassicConstants .CONFIG_FILE_PROPERTY ;
46
52
53
+ String [] INTERNAL_CONFIGURATOR_CLASSNAME_LIST = {"ch.qos.logback.classic.joran.SerializedModelConfigurator" ,
54
+ "ch.qos.logback.classic.util.DefaultJoranConfigurator" , "ch.qos.logback.classic.BasicConfigurator" };
55
+
47
56
final LoggerContext loggerContext ;
48
57
49
58
final ContextAware contextAware ;
@@ -66,41 +75,59 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
66
75
StatusListenerConfigHelper .installIfAsked (loggerContext );
67
76
68
77
78
+ // invoke custom configurators
69
79
List <Configurator > configuratorList = ClassicEnvUtil .loadFromServiceLoader (Configurator .class , classLoader );
70
-
71
80
configuratorList .sort (rankComparator );
72
-
73
- printConfiguratorOrder (configuratorList );
81
+ if (configuratorList .isEmpty ()) {
82
+ contextAware .addInfo ("No custom configurators were discovered as a service." );
83
+ } else {
84
+ printConfiguratorOrder (configuratorList );
85
+ }
74
86
75
87
for (Configurator c : configuratorList ) {
76
- try {
77
- long start = System .currentTimeMillis ();
78
- contextAware .addInfo ("Constructed configurator of type " + c .getClass ());
79
- c .setContext (loggerContext );
80
- Configurator .ExecutionStatus status = c .configure (loggerContext );
81
- printDuration (start , c , status );
82
- if (status == Configurator .ExecutionStatus .DO_NOT_INVOKE_NEXT_IF_ANY ) {
83
- return ;
84
- }
85
- } catch (Exception e ) {
86
- throw new LogbackException (String .format ("Failed to initialize Configurator: %s using ServiceLoader" ,
87
- c != null ? c .getClass ().getCanonicalName () : "null" ), e );
88
- }
88
+ if (invokeConfigure (c ) == Configurator .ExecutionStatus .DO_NOT_INVOKE_NEXT_IF_ANY )
89
+ return ;
89
90
}
90
91
92
+ // invoke internal configurators
93
+ for (String configuratorClassName : INTERNAL_CONFIGURATOR_CLASSNAME_LIST ) {
94
+ contextAware .addInfo ("Trying to configure with " +configuratorClassName );
95
+ Configurator c = instantiateConfiguratorByClassName (configuratorClassName , classLoader );
96
+ if (c == null )
97
+ continue ;
98
+ if (invokeConfigure (c ) == Configurator .ExecutionStatus .DO_NOT_INVOKE_NEXT_IF_ANY )
99
+ return ;
100
+ }
101
+ }
91
102
103
+ private Configurator instantiateConfiguratorByClassName (String configuratorClassName , ClassLoader classLoader ) {
104
+ try {
105
+ Class <?> classObj = classLoader .loadClass (configuratorClassName );
106
+ return (Configurator ) classObj .getConstructor ().newInstance ();
107
+ } catch (ReflectiveOperationException e ) {
108
+ contextAware .addInfo ("Instantiation failure: " + e .toString ());
109
+ return null ;
110
+ }
111
+ }
92
112
93
- // long startJoranConfiguration = System.currentTimeMillis();
94
- // Configurator.ExecutionStatus es = attemptConfigurationUsingJoranUsingReflexion(classLoader);
95
- //
96
- // if (es == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) {
97
- // printDuration(startJoranConfiguration, JORAN_CONFIGURATION_DURATION_MSG, true);
98
- // return;
99
- // }
100
- // printDuration(startJoranConfiguration, JORAN_CONFIGURATION_DURATION_MSG, false);
101
- //
102
- // // at this stage invoke basicConfigurator
103
- // fallbackOnToBasicConfigurator();
113
+ /**
114
+ *
115
+ * @param configurator
116
+ * @return true if enclosing loop should break, false otherwise
117
+ */
118
+ private Configurator .ExecutionStatus invokeConfigure (Configurator configurator ) {
119
+ try {
120
+ long start = System .currentTimeMillis ();
121
+ contextAware .addInfo ("Constructed configurator of type " + configurator .getClass ());
122
+ configurator .setContext (loggerContext );
123
+ Configurator .ExecutionStatus status = configurator .configure (loggerContext );
124
+ printDuration (start , configurator , status );
125
+ return status ;
126
+
127
+ } catch (Exception e ) {
128
+ throw new LogbackException (String .format ("Failed to initialize or to run Configurator: %s" ,
129
+ configurator != null ? configurator .getClass ().getCanonicalName () : "null" ), e );
130
+ }
104
131
}
105
132
106
133
private void printConfiguratorOrder (List <Configurator > configuratorList ) {
@@ -131,13 +158,6 @@ private Configurator.ExecutionStatus attemptConfigurationUsingJoranUsingReflexio
131
158
132
159
}
133
160
134
- private void fallbackOnToBasicConfigurator () {
135
- BasicConfigurator basicConfigurator = new BasicConfigurator ();
136
- basicConfigurator .setContext (loggerContext );
137
- basicConfigurator .configure (loggerContext );
138
- }
139
-
140
-
141
161
Comparator <Configurator > rankComparator = new Comparator <Configurator >() {
142
162
@ Override
143
163
public int compare (Configurator c1 , Configurator c2 ) {
0 commit comments