@@ -43,8 +43,6 @@ public class ContextInitializer {
4343 * @deprecated Please use ClassicConstants.CONFIG_FILE_PROPERTY instead
4444 */
4545 final public static String CONFIG_FILE_PROPERTY = ClassicConstants .CONFIG_FILE_PROPERTY ;
46- private static final String JORAN_CONFIGURATION_DURATION_MSG = "JoranConfiguration duration " ;
47- private static final String CONFIGURATION_AS_A_SERVICE_DURATION_MSG = "Configuration as a service duration " ;
4846
4947 final LoggerContext loggerContext ;
5048
@@ -67,18 +65,21 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
6765 loggerContext .getStatusManager ().add (new InfoStatus (CoreConstants .LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr , loggerContext ));
6866 StatusListenerConfigHelper .installIfAsked (loggerContext );
6967
70- long startConfigurationAsAService = System . currentTimeMillis ();
68+
7169 List <Configurator > configuratorList = ClassicEnvUtil .loadFromServiceLoader (Configurator .class , classLoader );
7270
7371 configuratorList .sort (rankComparator );
7472
73+ printConfiguratorOrder (configuratorList );
74+
7575 for (Configurator c : configuratorList ) {
7676 try {
77+ long start = System .currentTimeMillis ();
7778 contextAware .addInfo ("Constructed configurator of type " + c .getClass ());
7879 c .setContext (loggerContext );
7980 Configurator .ExecutionStatus status = c .configure (loggerContext );
81+ printDuration (start , c , status );
8082 if (status == Configurator .ExecutionStatus .DO_NOT_INVOKE_NEXT_IF_ANY ) {
81- printDuration (startConfigurationAsAService , CONFIGURATION_AS_A_SERVICE_DURATION_MSG , true );
8283 return ;
8384 }
8485 } catch (Exception e ) {
@@ -87,25 +88,33 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
8788 }
8889 }
8990
90- printDuration (startConfigurationAsAService , CONFIGURATION_AS_A_SERVICE_DURATION_MSG , false );
9191
92- long startJoranConfiguration = System .currentTimeMillis ();
93- Configurator .ExecutionStatus es = attemptConfigurationUsingJoranUsingReflexion (classLoader );
9492
95- if (es == Configurator .ExecutionStatus .DO_NOT_INVOKE_NEXT_IF_ANY ) {
96- printDuration (startJoranConfiguration , JORAN_CONFIGURATION_DURATION_MSG , true );
97- return ;
98- }
99- printDuration (startJoranConfiguration , JORAN_CONFIGURATION_DURATION_MSG , false );
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();
104+ }
100105
101- // at this stage invoke basicConfigurator
102- fallbackOnToBasicConfigurator ();
106+ private void printConfiguratorOrder (List <Configurator > configuratorList ) {
107+ contextAware .addInfo ("Here is a list of configurators discovered as a service, by rank: " );
108+ for (Configurator c : configuratorList ) {
109+ contextAware .addInfo (" " +c .getClass ().getName ());
110+ }
111+ contextAware .addInfo ("They will be invoked in order until ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY is returned." );
103112 }
104113
105- private void printDuration (long start , String message , boolean success ) {
114+ private void printDuration (long start , Configurator configurator , Configurator . ExecutionStatus executionStatus ) {
106115 long end = System .currentTimeMillis ();
107- long configurationAsAServiceDuration = end - start ;
108- contextAware .addInfo (message + configurationAsAServiceDuration + " milliseconds. Success status =" +success );
116+ long diff = end - start ;
117+ contextAware .addInfo ( configurator . getClass (). getName ()+ ".configure() call lasted " + diff + " milliseconds. ExecutionStatus =" +executionStatus );
109118 }
110119
111120 private Configurator .ExecutionStatus attemptConfigurationUsingJoranUsingReflexion (ClassLoader classLoader ) {
@@ -128,21 +137,6 @@ private void fallbackOnToBasicConfigurator() {
128137 basicConfigurator .configure (loggerContext );
129138 }
130139
131- // private void sortByPriority(List<Configurator> configuratorList) {
132- // configuratorList.sort(new Comparator<Configurator>() {
133- // @Override
134- // public int compare(Configurator o1, Configurator o2) {
135- // if (o1.getClass() == o2.getClass())
136- // return 0;
137- // if (o1 instanceof DefaultJoranConfigurator) {
138- // return 1;
139- // }
140- //
141- // // otherwise do not intervene
142- // return 0;
143- // }
144- // });
145- // }
146140
147141 Comparator <Configurator > rankComparator = new Comparator <Configurator >() {
148142 @ Override
@@ -151,39 +145,21 @@ public int compare(Configurator c1, Configurator c2) {
151145 ConfiguratorRank r1 = c1 .getClass ().getAnnotation (ConfiguratorRank .class );
152146 ConfiguratorRank r2 = c2 .getClass ().getAnnotation (ConfiguratorRank .class );
153147
154- ConfiguratorRank . Value value1 = r1 == null ? ConfiguratorRank .Value . REGULAR : r1 .value ();
155- ConfiguratorRank . Value value2 = r2 == null ? ConfiguratorRank .Value . REGULAR : r2 .value ();
148+ int value1 = r1 == null ? ConfiguratorRank .DEFAULT : r1 .value ();
149+ int value2 = r2 == null ? ConfiguratorRank .DEFAULT : r2 .value ();
156150
157151 int result = compareRankValue (value1 , value2 );
158152 // reverse the result for high to low sort
159153 return (-result );
160154 }
161155 };
162156
163- private int compareRankValue (ConfiguratorRank .Value value1 , ConfiguratorRank .Value value2 ) {
164-
165- switch (value1 ) {
166- case FIRST :
167- if (value2 == ConfiguratorRank .Value .FIRST )
168- return 0 ;
169- else
170- return 1 ;
171- case REGULAR :
172- if (value2 == ConfiguratorRank .Value .FALLBACK )
173- return 1 ;
174- else if (value2 == ConfiguratorRank .Value .REGULAR )
175- return 0 ;
176- else
177- return -1 ;
178- case FALLBACK :
179- if (value2 == ConfiguratorRank .Value .FALLBACK )
180- return 0 ;
181- else
182- return -1 ;
183-
184- default :
157+ private int compareRankValue (int value1 , int value2 ) {
158+ if (value1 > value2 )
159+ return 1 ;
160+ else if (value1 == value2 )
185161 return 0 ;
186- }
162+ else return - 1 ;
187163
188164 }
189165}
0 commit comments