Merge tag 'upstream/1.7.19'
Upstream version 1.7.19
Emmanuel Bourg
7 years ago
55 | 55 | <pathelement location="../slf4j-simple/target/slf4j-simple-${currentVersion}.jar" /> |
56 | 56 | </path > |
57 | 57 | |
58 | ||
58 | ||
59 | 59 | <path id="incompatibleMultiBinding"> |
60 | 60 | <pathelement location="target/test-classes/" /> |
61 | 61 | <pathelement location="../slf4j-api/target/slf4j-api-${currentVersion}.jar" /> |
102 | 102 | |
103 | 103 | <target name="testAll" depends="init, |
104 | 104 | testMissingSingletonMethod, |
105 | testMismatch, | |
105 | testMismatch, | |
106 | 106 | testMatch, |
107 | 107 | testMultiBinding, |
108 | 108 | testIncompatibleMultiBinding, |
130 | 130 | </junit> |
131 | 131 | |
132 | 132 | </target> |
133 | ||
134 | ||
135 | <target name="testMixed17"> | |
136 | ||
137 | <junit printsummary="yes" fork="no" haltonfailure="yes"> | |
138 | <classpath refid="pathMixed17" /> | |
139 | <formatter type="plain" /> | |
140 | <test fork="yes" todir="target/unit-reports" | |
141 | outfile="TEST-MIXED-17" | |
142 | name="org.slf4j.VersionMismatchAssertionTest" /> | |
143 | </junit> | |
144 | </target> | |
145 | ||
133 | 146 | |
134 | 147 | <target name="testMismatch"> |
135 | 148 |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>integration</artifactId> |
0 | 0 | /** |
1 | * Copyright (c) 2004-2011 QOS.ch | |
1 | * Copyright (c) 2004-2016 QOS.ch | |
2 | 2 | * All rights reserved. |
3 | 3 | * |
4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
6 | 6 | |
7 | 7 | public class Issue324Test extends TestCase { |
8 | 8 | |
9 | ||
10 | 9 | public void testLoggerCreationInPresenseOfSecurityManager() { |
11 | 10 | String currentDir = System.getProperty("user.dir"); |
12 | System.out.println("currentDir:"+currentDir); | |
11 | System.out.println("currentDir:" + currentDir); | |
13 | 12 | Logger logger = LoggerFactory.getLogger(Issue324Test.class); |
14 | 13 | logger.debug("hello"); |
15 | 14 | } |
4 | 4 | <parent> |
5 | 5 | <groupId>org.slf4j</groupId> |
6 | 6 | <artifactId>slf4j-parent</artifactId> |
7 | <version>1.7.14</version> | |
7 | <version>1.7.19</version> | |
8 | 8 | </parent> |
9 | 9 | |
10 | 10 | <modelVersion>4.0.0</modelVersion> |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>jul-to-slf4j</artifactId> |
32 | 32 | import org.junit.Before; |
33 | 33 | import org.junit.Test; |
34 | 34 | import org.slf4j.LoggerFactory; |
35 | ||
35 | 36 | public class SLF4JBridgeHandlerPerfTest { |
36 | 37 | |
37 | 38 | static String LOGGER_NAME = "yay"; |
48 | 49 | org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(LOGGER_NAME); |
49 | 50 | |
50 | 51 | Handler[] existingHandlers; |
51 | ||
52 | 52 | |
53 | 53 | @Before |
54 | 54 | public void setUp() throws Exception { |
34 | 34 | import org.junit.After; |
35 | 35 | import org.junit.Before; |
36 | 36 | import org.junit.Test; |
37 | public class SLF4JBridgeHandlerTest { | |
37 | ||
38 | public class SLF4JBridgeHandlerTest { | |
38 | 39 | |
39 | 40 | static String LOGGER_NAME = "yay"; |
40 | 41 | |
41 | 42 | ListAppender listAppender = new ListAppender(); |
42 | 43 | org.apache.log4j.Logger log4jRoot; |
43 | 44 | java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("yay"); |
44 | ||
45 | 45 | |
46 | 46 | @Before |
47 | 47 | public void setUp() throws Exception { |
178 | 178 | } |
179 | 179 | |
180 | 180 | // See http://jira.qos.ch/browse/SLF4J-337 |
181 | ||
181 | ||
182 | 182 | @Test |
183 | 183 | public void illFormattedInputShouldBeReturnedAsIs() { |
184 | 184 | SLF4JBridgeHandler.install(); |
185 | 185 | String msg = "foo {18=bad} {0}"; |
186 | ||
186 | ||
187 | 187 | julLogger.log(Level.INFO, msg, "ignored parameter due to IllegalArgumentException"); |
188 | 188 | assertEquals(1, listAppender.list.size()); |
189 | 189 | LoggingEvent le = (LoggingEvent) listAppender.list.get(0); |
190 | 190 | assertEquals(msg, le.getMessage()); |
191 | 191 | } |
192 | ||
192 | ||
193 | 193 | void assertLevel(int index, org.apache.log4j.Level expectedLevel) { |
194 | 194 | LoggingEvent le = (LoggingEvent) listAppender.list.get(index); |
195 | 195 | assertEquals(expectedLevel, le.getLevel()); |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 |
345 | 345 | public void setLevel(Level level) { |
346 | 346 | // nothing to do |
347 | 347 | } |
348 | ||
348 | ||
349 | 349 | public boolean getAdditivity() { |
350 | 350 | return false; |
351 | 351 | } |
352 | ||
352 | ||
353 | 353 | public void assertLog(boolean assertion, String msg) { |
354 | if(!assertion) | |
354 | if (!assertion) | |
355 | 355 | error(msg); |
356 | 356 | } |
357 | 357 |
3 | 3 | Bundle-Name: log4j-over-slf4j |
4 | 4 | Bundle-Vendor: SLF4J.ORG |
5 | 5 | Bundle-RequiredExecutionEnvironment: J2SE-1.5 |
6 | Export-Package: org.apache.log4j;version=${log4j.version},org.apache.log4j.helpers;version=${log4j.version},org.apache.log4j.spi;version=${log4j.version},org.apache.log4j.xml;version=${log4j.version} | |
7 | Import-Package: org.slf4j;version=${slf4j.api.minimum.compatible.version}, org.slf4j.helpers;version=${slf4j.api.minimum.compatible.version}, org.slf4j.spi;version=${slf4j.api.minimum.compatible.version} | |
6 | Export-Package: org.apache.log4j;version=${log4j.version}, | |
7 | org.apache.log4j.helpers;version=${log4j.version}, | |
8 | org.apache.log4j.spi;version=${log4j.version}, | |
9 | org.apache.log4j.xml;version=${log4j.version} | |
10 | Import-Package: org.slf4j;version=${slf4j.api.minimum.compatible.version}, | |
11 | org.slf4j.helpers;version=${slf4j.api.minimum.compatible.version}, | |
12 | org.slf4j.spi;version=${slf4j.api.minimum.compatible.version} |
31 | 31 | import org.apache.log4j.Category; |
32 | 32 | import org.apache.log4j.Logger; |
33 | 33 | import org.junit.Test; |
34 | public class Bug131 { | |
34 | ||
35 | public class Bug131 { | |
35 | 36 | |
36 | 37 | @Test |
37 | 38 | public void testBug131() { |
32 | 32 | import org.apache.log4j.Logger; |
33 | 33 | import org.junit.Test; |
34 | 34 | |
35 | public class Bug139 { | |
35 | public class Bug139 { | |
36 | 36 | |
37 | 37 | @Test |
38 | 38 | public void test() { |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>osgi-over-slf4j</artifactId> |
5 | 5 | |
6 | 6 | <groupId>org.slf4j</groupId> |
7 | 7 | <artifactId>slf4j-parent</artifactId> |
8 | <version>1.7.14</version> | |
8 | <version>1.7.19</version> | |
9 | 9 | |
10 | 10 | <packaging>pom</packaging> |
11 | 11 | <name>SLF4J</name> |
27 | 27 | </licenses> |
28 | 28 | |
29 | 29 | <scm> |
30 | <url>https://github.com/ceki/slf4j</url> | |
30 | <url>https://github.com/qos-ch/slf4j</url> | |
31 | 31 | <connection>git@github.com:qos-ch/slf4j.git</connection> |
32 | 32 | </scm> |
33 | 33 | |
43 | 43 | <cal10n.version>0.8.1</cal10n.version> |
44 | 44 | <log4j.version>1.2.17</log4j.version> |
45 | 45 | <logback.version>1.0.13</logback.version> |
46 | <junit.version>4.10</junit.version> | |
46 | <junit.version>4.12</junit.version> | |
47 | 47 | <maven-site-plugin.version>3.3</maven-site-plugin.version> |
48 | 48 | <javadoc.plugin.version>2.10.2</javadoc.plugin.version> |
49 | 49 | </properties> |
181 | 181 | <plugin> |
182 | 182 | <groupId>org.apache.maven.plugins</groupId> |
183 | 183 | <artifactId>maven-surefire-plugin</artifactId> |
184 | <version>2.10</version> | |
184 | <version>2.19.1</version> | |
185 | 185 | <configuration> |
186 | <forkMode>once</forkMode> | |
186 | <forkCount>2C</forkCount> | |
187 | <reuseForks>true</reuseForks> | |
187 | 188 | <reportFormat>plain</reportFormat> |
188 | 189 | <trimStackTrace>false</trimStackTrace> |
189 | 190 | <excludes> |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-android</artifactId> |
80 | 80 | class AndroidLoggerAdapter extends MarkerIgnoringBase { |
81 | 81 | private static final long serialVersionUID = -1227274521521287937L; |
82 | 82 | |
83 | ||
84 | 83 | /** |
85 | 84 | * Package access allows only {@link AndroidLoggerFactory} to instantiate |
86 | 85 | * SimpleLogger instances. |
116 | 116 | // Take leading part and append '*' to indicate that it was truncated |
117 | 117 | int length = loggerName.length(); |
118 | 118 | int lastPeriodIndex = loggerName.lastIndexOf('.'); |
119 | return lastPeriodIndex != -1 && length - (lastPeriodIndex + 1) <= TAG_MAX_LENGTH | |
120 | ? loggerName.substring(lastPeriodIndex + 1) | |
121 | : '*' + loggerName.substring(length - TAG_MAX_LENGTH + 1); | |
119 | return lastPeriodIndex != -1 && length - (lastPeriodIndex + 1) <= TAG_MAX_LENGTH ? loggerName.substring(lastPeriodIndex + 1) : '*' + loggerName | |
120 | .substring(length - TAG_MAX_LENGTH + 1); | |
122 | 121 | } |
123 | 122 | }⏎ |
57 | 57 | // to avoid constant folding by the compiler, this field must *not* be final |
58 | 58 | public static String REQUESTED_API_VERSION = "1.6.99"; // !final |
59 | 59 | |
60 | ||
61 | ||
62 | 60 | private static final String loggerFactoryClassStr = AndroidLoggerFactory.class.getName(); |
63 | 61 | |
64 | 62 | /** |
26 | 26 | import org.slf4j.helpers.NOPMDCAdapter; |
27 | 27 | import org.slf4j.spi.MDCAdapter; |
28 | 28 | |
29 | ||
30 | 29 | /** |
31 | 30 | * This implementation is bound to {@link NOPMDCAdapter}. |
32 | 31 | * |
35 | 34 | */ |
36 | 35 | public class StaticMDCBinder { |
37 | 36 | |
38 | /** | |
39 | * The unique instance of this class. | |
40 | */ | |
41 | public static final StaticMDCBinder SINGLETON = new StaticMDCBinder(); | |
37 | /** | |
38 | * The unique instance of this class. | |
39 | */ | |
40 | public static final StaticMDCBinder SINGLETON = new StaticMDCBinder(); | |
42 | 41 | |
43 | private StaticMDCBinder() { | |
44 | } | |
45 | ||
46 | /** | |
47 | * Return the singleton of this class. | |
48 | * | |
49 | * @return the StaticMDCBinder singleton | |
50 | * @since 1.7.14 | |
51 | */ | |
52 | public static final StaticMDCBinder getSingleton() { | |
53 | return SINGLETON; | |
54 | } | |
55 | /** | |
56 | * Currently this method always returns an instance of | |
57 | * {@link NOPMDCAdapter}. | |
58 | */ | |
59 | public MDCAdapter getMDCA() { | |
60 | return new NOPMDCAdapter(); | |
61 | } | |
62 | ||
63 | public String getMDCAdapterClassStr() { | |
64 | return NOPMDCAdapter.class.getName(); | |
65 | } | |
42 | private StaticMDCBinder() { | |
43 | } | |
44 | ||
45 | /** | |
46 | * Return the singleton of this class. | |
47 | * | |
48 | * @return the StaticMDCBinder singleton | |
49 | * @since 1.7.14 | |
50 | */ | |
51 | public static final StaticMDCBinder getSingleton() { | |
52 | return SINGLETON; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Currently this method always returns an instance of | |
57 | * {@link NOPMDCAdapter}. | |
58 | */ | |
59 | public MDCAdapter getMDCA() { | |
60 | return new NOPMDCAdapter(); | |
61 | } | |
62 | ||
63 | public String getMDCAdapterClassStr() { | |
64 | return NOPMDCAdapter.class.getName(); | |
65 | } | |
66 | 66 | } |
38 | 38 | */ |
39 | 39 | public class StaticMarkerBinder implements MarkerFactoryBinder { |
40 | 40 | |
41 | /** | |
42 | * The unique instance of this class. | |
43 | */ | |
44 | public static final StaticMarkerBinder SINGLETON = new StaticMarkerBinder(); | |
45 | ||
46 | final IMarkerFactory markerFactory = new BasicMarkerFactory(); | |
47 | ||
48 | private StaticMarkerBinder() { | |
49 | } | |
50 | ||
51 | /** | |
52 | * Return the singleton of this class. | |
53 | * | |
54 | * @return the StaticMarkerBinder singleton | |
55 | * @since 1.7.14 | |
56 | */ | |
57 | public static StaticMarkerBinder getSingleton() { | |
58 | return SINGLETON; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Currently this method always returns an instance of | |
63 | * {@link BasicMarkerFactory}. | |
64 | */ | |
65 | public IMarkerFactory getMarkerFactory() { | |
66 | return markerFactory; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Currently, this method returns the class name of | |
71 | * {@link BasicMarkerFactory}. | |
72 | */ | |
73 | public String getMarkerFactoryClassStr() { | |
74 | return BasicMarkerFactory.class.getName(); | |
75 | } | |
41 | /** | |
42 | * The unique instance of this class. | |
43 | */ | |
44 | public static final StaticMarkerBinder SINGLETON = new StaticMarkerBinder(); | |
45 | ||
46 | final IMarkerFactory markerFactory = new BasicMarkerFactory(); | |
47 | ||
48 | private StaticMarkerBinder() { | |
49 | } | |
50 | ||
51 | /** | |
52 | * Return the singleton of this class. | |
53 | * | |
54 | * @return the StaticMarkerBinder singleton | |
55 | * @since 1.7.14 | |
56 | */ | |
57 | public static StaticMarkerBinder getSingleton() { | |
58 | return SINGLETON; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Currently this method always returns an instance of | |
63 | * {@link BasicMarkerFactory}. | |
64 | */ | |
65 | public IMarkerFactory getMarkerFactory() { | |
66 | return markerFactory; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Currently, this method returns the class name of | |
71 | * {@link BasicMarkerFactory}. | |
72 | */ | |
73 | public String getMarkerFactoryClassStr() { | |
74 | return BasicMarkerFactory.class.getName(); | |
75 | } | |
76 | 76 | } |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-api</artifactId> |
25 | 25 | |
26 | 26 | import java.io.IOException; |
27 | 27 | import java.net.URL; |
28 | import java.util.ArrayList; | |
28 | 29 | import java.util.Arrays; |
29 | 30 | import java.util.Enumeration; |
30 | 31 | import java.util.LinkedHashSet; |
31 | 32 | import java.util.List; |
32 | 33 | import java.util.Set; |
33 | ||
34 | import java.util.concurrent.LinkedBlockingQueue; | |
35 | ||
36 | import org.slf4j.event.SubstituteLoggingEvent; | |
34 | 37 | import org.slf4j.helpers.NOPLoggerFactory; |
35 | 38 | import org.slf4j.helpers.SubstituteLogger; |
36 | 39 | import org.slf4j.helpers.SubstituteLoggerFactory; |
67 | 70 | static final String VERSION_MISMATCH = CODES_PREFIX + "#version_mismatch"; |
68 | 71 | static final String SUBSTITUTE_LOGGER_URL = CODES_PREFIX + "#substituteLogger"; |
69 | 72 | static final String LOGGER_NAME_MISMATCH_URL = CODES_PREFIX + "#loggerNameMismatch"; |
73 | static final String REPLAY_URL = CODES_PREFIX + "#replay"; | |
70 | 74 | |
71 | 75 | static final String UNSUCCESSFUL_INIT_URL = CODES_PREFIX + "#unsuccessfulInit"; |
72 | 76 | static final String UNSUCCESSFUL_INIT_MSG = "org.slf4j.LoggerFactory could not be successfully initialized. See also " + UNSUCCESSFUL_INIT_URL; |
77 | 81 | static final int SUCCESSFUL_INITIALIZATION = 3; |
78 | 82 | static final int NOP_FALLBACK_INITIALIZATION = 4; |
79 | 83 | |
80 | static int INITIALIZATION_STATE = UNINITIALIZED; | |
81 | static SubstituteLoggerFactory TEMP_FACTORY = new SubstituteLoggerFactory(); | |
84 | static volatile int INITIALIZATION_STATE = UNINITIALIZED; | |
85 | static SubstituteLoggerFactory SUBST_FACTORY = new SubstituteLoggerFactory(); | |
82 | 86 | static NOPLoggerFactory NOP_FALLBACK_FACTORY = new NOPLoggerFactory(); |
83 | 87 | |
84 | 88 | // Support for detecting mismatched logger names. |
85 | 89 | static final String DETECT_LOGGER_NAME_MISMATCH_PROPERTY = "slf4j.detectLoggerNameMismatch"; |
86 | 90 | static final String JAVA_VENDOR_PROPERTY = "java.vendor.url"; |
87 | ||
91 | ||
88 | 92 | static boolean DETECT_LOGGER_NAME_MISMATCH = Util.safeGetBooleanSystemProperty(DETECT_LOGGER_NAME_MISMATCH_PROPERTY); |
89 | 93 | |
90 | 94 | /** |
113 | 117 | */ |
114 | 118 | static void reset() { |
115 | 119 | INITIALIZATION_STATE = UNINITIALIZED; |
116 | TEMP_FACTORY = new SubstituteLoggerFactory(); | |
117 | 120 | } |
118 | 121 | |
119 | 122 | private final static void performInitialization() { |
135 | 138 | |
136 | 139 | private final static void bind() { |
137 | 140 | try { |
138 | Set<URL> staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet(); | |
139 | reportMultipleBindingAmbiguity(staticLoggerBinderPathSet); | |
141 | Set<URL> staticLoggerBinderPathSet = null; | |
142 | // skip check under android, see also http://jira.qos.ch/browse/SLF4J-328 | |
143 | if (!isAndroid()) { | |
144 | staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet(); | |
145 | reportMultipleBindingAmbiguity(staticLoggerBinderPathSet); | |
146 | } | |
140 | 147 | // the next line does the binding |
141 | 148 | StaticLoggerBinder.getSingleton(); |
142 | 149 | INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION; |
143 | 150 | reportActualBinding(staticLoggerBinderPathSet); |
144 | fixSubstitutedLoggers(); | |
151 | replayEvents(); | |
145 | 152 | } catch (NoClassDefFoundError ncde) { |
146 | 153 | String msg = ncde.getMessage(); |
147 | 154 | if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg)) { |
173 | 180 | Util.report("Failed to instantiate SLF4J LoggerFactory", t); |
174 | 181 | } |
175 | 182 | |
176 | private final static void fixSubstitutedLoggers() { | |
177 | List<SubstituteLogger> loggers = TEMP_FACTORY.getLoggers(); | |
178 | ||
179 | if (loggers.isEmpty()) { | |
183 | private static void replayEvents() { | |
184 | final LinkedBlockingQueue<SubstituteLoggingEvent> queue = SUBST_FACTORY.getEventQueue(); | |
185 | final int queueSize = queue.size(); | |
186 | int count = 0; | |
187 | final int maxDrain = 128; | |
188 | List<SubstituteLoggingEvent> eventList = new ArrayList<SubstituteLoggingEvent>(maxDrain); | |
189 | while (true) { | |
190 | int numDrained = queue.drainTo(eventList, maxDrain); | |
191 | if (numDrained == 0) | |
192 | break; | |
193 | for (SubstituteLoggingEvent event : eventList) { | |
194 | replaySingleEvent(event); | |
195 | if (count++ == 0) | |
196 | emitReplayOrSubstituionWarning(event, queueSize); | |
197 | } | |
198 | eventList.clear(); | |
199 | } | |
200 | } | |
201 | ||
202 | private static void emitReplayOrSubstituionWarning(SubstituteLoggingEvent event, int queueSize) { | |
203 | if (event.getLogger().isDelegateEventAware()) { | |
204 | emitReplayWarning(queueSize); | |
205 | } else if (event.getLogger().isDelegateNOP()) { | |
206 | // nothing to do | |
207 | } else { | |
208 | emitSubstitutionWarning(); | |
209 | } | |
210 | } | |
211 | ||
212 | private static void replaySingleEvent(SubstituteLoggingEvent event) { | |
213 | if (event == null) | |
180 | 214 | return; |
181 | } | |
182 | ||
215 | ||
216 | SubstituteLogger substLogger = event.getLogger(); | |
217 | String loggerName = substLogger.getName(); | |
218 | if (substLogger.isDelegateNull()) { | |
219 | Logger logger = getLogger(loggerName); | |
220 | substLogger.setDelegate(logger); | |
221 | } | |
222 | ||
223 | if (substLogger.isDelegateNOP()) { | |
224 | // nothing to do | |
225 | } else if (substLogger.isDelegateEventAware()) { | |
226 | substLogger.log(event); | |
227 | } else { | |
228 | Util.report(loggerName); | |
229 | } | |
230 | } | |
231 | ||
232 | private static void emitSubstitutionWarning() { | |
183 | 233 | Util.report("The following set of substitute loggers may have been accessed"); |
184 | 234 | Util.report("during the initialization phase. Logging calls during this"); |
185 | 235 | Util.report("phase were not honored. However, subsequent logging calls to these"); |
186 | 236 | Util.report("loggers will work as normally expected."); |
187 | 237 | Util.report("See also " + SUBSTITUTE_LOGGER_URL); |
188 | for (SubstituteLogger subLogger : loggers) { | |
189 | subLogger.setDelegate(getLogger(subLogger.getName())); | |
190 | Util.report(subLogger.getName()); | |
191 | } | |
192 | ||
193 | TEMP_FACTORY.clear(); | |
238 | } | |
239 | ||
240 | private static void emitReplayWarning(int eventCount) { | |
241 | Util.report("A number (" + eventCount + ") of logging calls during the initialization phase have been intercepted and are"); | |
242 | Util.report("now being replayed. These are suject to the filtering rules of the underlying logging system."); | |
243 | Util.report("See also " + REPLAY_URL); | |
194 | 244 | } |
195 | 245 | |
196 | 246 | private final static void versionSanityCheck() { |
245 | 295 | return staticLoggerBinderPathSet; |
246 | 296 | } |
247 | 297 | |
248 | private static boolean isAmbiguousStaticLoggerBinderPathSet(Set<URL> staticLoggerBinderPathSet) { | |
249 | return staticLoggerBinderPathSet.size() > 1; | |
298 | private static boolean isAmbiguousStaticLoggerBinderPathSet(Set<URL> binderPathSet) { | |
299 | return binderPathSet.size() > 1; | |
250 | 300 | } |
251 | 301 | |
252 | 302 | /** |
254 | 304 | * No reporting is done otherwise. |
255 | 305 | * |
256 | 306 | */ |
257 | private static void reportMultipleBindingAmbiguity(Set<URL> staticLoggerBinderPathSet) { | |
258 | if(isAndroid()) { | |
259 | // skip check under android, see also http://jira.qos.ch/browse/SLF4J-328 | |
260 | return; | |
261 | } | |
262 | ||
263 | if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) { | |
307 | private static void reportMultipleBindingAmbiguity(Set<URL> binderPathSet) { | |
308 | if (isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) { | |
264 | 309 | Util.report("Class path contains multiple SLF4J bindings."); |
265 | for (URL path : staticLoggerBinderPathSet) { | |
310 | for (URL path : binderPathSet) { | |
266 | 311 | Util.report("Found binding in [" + path + "]"); |
267 | 312 | } |
268 | 313 | Util.report("See " + MULTIPLE_BINDINGS_URL + " for an explanation."); |
271 | 316 | |
272 | 317 | private static boolean isAndroid() { |
273 | 318 | String vendor = Util.safeGetSystemProperty(JAVA_VENDOR_PROPERTY); |
274 | if(vendor == null) | |
319 | if (vendor == null) | |
275 | 320 | return false; |
276 | 321 | return vendor.toLowerCase().contains("android"); |
277 | 322 | } |
278 | 323 | |
279 | private static void reportActualBinding(Set<URL> staticLoggerBinderPathSet) { | |
280 | if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) { | |
324 | private static void reportActualBinding(Set<URL> binderPathSet) { | |
325 | // binderPathSet can be null under Android | |
326 | if (binderPathSet != null && isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) { | |
281 | 327 | Util.report("Actual binding is of type [" + StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr() + "]"); |
282 | 328 | } |
283 | 329 | } |
354 | 400 | case ONGOING_INITIALIZATION: |
355 | 401 | // support re-entrant behavior. |
356 | 402 | // See also http://jira.qos.ch/browse/SLF4J-97 |
357 | return TEMP_FACTORY; | |
403 | return SUBST_FACTORY; | |
358 | 404 | } |
359 | 405 | throw new IllegalStateException("Unreachable code"); |
360 | 406 | } |
0 | package org.slf4j.event; | |
1 | ||
2 | import org.slf4j.spi.LocationAwareLogger; | |
3 | ||
4 | public class EventConstants { | |
5 | public static final int ERROR_INT = LocationAwareLogger.ERROR_INT; | |
6 | public static final int WARN_INT = LocationAwareLogger.WARN_INT; | |
7 | public static final int INFO_INT = LocationAwareLogger.INFO_INT; | |
8 | public static final int DEBUG_INT = LocationAwareLogger.DEBUG_INT; | |
9 | public static final int TRACE_INT = LocationAwareLogger.TRACE_INT; | |
10 | public static final String NA_SUBST = "NA/SubstituteLogger"; | |
11 | ||
12 | } |
0 | package org.slf4j.event; | |
1 | ||
2 | import java.util.Queue; | |
3 | ||
4 | import org.slf4j.Logger; | |
5 | import org.slf4j.Marker; | |
6 | import org.slf4j.helpers.SubstituteLogger; | |
7 | ||
8 | public class EventRecodingLogger implements Logger { | |
9 | ||
10 | String name; | |
11 | SubstituteLogger logger; | |
12 | Queue<SubstituteLoggingEvent> eventQueue; | |
13 | ||
14 | public EventRecodingLogger(SubstituteLogger logger, Queue<SubstituteLoggingEvent> eventQueue) { | |
15 | this.logger = logger; | |
16 | this.name = logger.getName(); | |
17 | this.eventQueue = eventQueue; | |
18 | } | |
19 | ||
20 | public String getName() { | |
21 | return name; | |
22 | } | |
23 | ||
24 | private void recordEvent(Level level, String msg, Object[] args, Throwable throwable) { | |
25 | recordEvent(level, null, msg, args, throwable); | |
26 | } | |
27 | ||
28 | private void recordEvent(Level level, Marker marker, String msg, Object[] args, Throwable throwable) { | |
29 | // System.out.println("recording logger:"+name+", msg:"+msg); | |
30 | SubstituteLoggingEvent loggingEvent = new SubstituteLoggingEvent(); | |
31 | loggingEvent.setTimeStamp(System.currentTimeMillis()); | |
32 | loggingEvent.setLevel(level); | |
33 | loggingEvent.setLogger(logger); | |
34 | loggingEvent.setLoggerName(name); | |
35 | ||
36 | loggingEvent.setMessage(msg); | |
37 | loggingEvent.setArgumentArray(args); | |
38 | loggingEvent.setThrowable(throwable); | |
39 | loggingEvent.setThreadName(Thread.currentThread().getName()); | |
40 | eventQueue.add(loggingEvent); | |
41 | } | |
42 | ||
43 | public boolean isTraceEnabled() { | |
44 | return true; | |
45 | } | |
46 | ||
47 | public void trace(String msg) { | |
48 | recordEvent(Level.TRACE, msg, null, null); | |
49 | } | |
50 | ||
51 | public void trace(String format, Object arg) { | |
52 | recordEvent(Level.TRACE, format, new Object[] { arg }, null); | |
53 | } | |
54 | ||
55 | public void trace(String format, Object arg1, Object arg2) { | |
56 | recordEvent(Level.TRACE, format, new Object[] { arg1, arg2 }, null); | |
57 | } | |
58 | ||
59 | public void trace(String format, Object... arguments) { | |
60 | recordEvent(Level.TRACE, format, arguments, null); | |
61 | } | |
62 | ||
63 | public void trace(String msg, Throwable t) { | |
64 | recordEvent(Level.TRACE, msg, null, t); | |
65 | } | |
66 | ||
67 | public boolean isTraceEnabled(Marker marker) { | |
68 | return true; | |
69 | } | |
70 | ||
71 | public void trace(Marker marker, String msg) { | |
72 | recordEvent(Level.TRACE, marker, msg, null, null); | |
73 | ||
74 | } | |
75 | ||
76 | public void trace(Marker marker, String format, Object arg) { | |
77 | recordEvent(Level.TRACE, marker, format, new Object[] { arg }, null); | |
78 | } | |
79 | ||
80 | public void trace(Marker marker, String format, Object arg1, Object arg2) { | |
81 | recordEvent(Level.TRACE, marker, format, new Object[] { arg1, arg2 }, null); | |
82 | } | |
83 | ||
84 | public void trace(Marker marker, String format, Object... argArray) { | |
85 | recordEvent(Level.TRACE, marker, format, argArray, null); | |
86 | ||
87 | } | |
88 | ||
89 | public void trace(Marker marker, String msg, Throwable t) { | |
90 | recordEvent(Level.TRACE, marker, msg, null, t); | |
91 | } | |
92 | ||
93 | public boolean isDebugEnabled() { | |
94 | return true; | |
95 | } | |
96 | ||
97 | public void debug(String msg) { | |
98 | recordEvent(Level.TRACE, msg, null, null); | |
99 | } | |
100 | ||
101 | public void debug(String format, Object arg) { | |
102 | recordEvent(Level.DEBUG, format, new Object[] { arg }, null); | |
103 | ||
104 | } | |
105 | ||
106 | public void debug(String format, Object arg1, Object arg2) { | |
107 | recordEvent(Level.DEBUG, format, new Object[] { arg1, arg2 }, null); | |
108 | ||
109 | } | |
110 | ||
111 | public void debug(String format, Object... arguments) { | |
112 | recordEvent(Level.DEBUG, format, arguments, null); | |
113 | } | |
114 | ||
115 | public void debug(String msg, Throwable t) { | |
116 | recordEvent(Level.DEBUG, msg, null, t); | |
117 | } | |
118 | ||
119 | public boolean isDebugEnabled(Marker marker) { | |
120 | return true; | |
121 | } | |
122 | ||
123 | public void debug(Marker marker, String msg) { | |
124 | recordEvent(Level.DEBUG, marker, msg, null, null); | |
125 | } | |
126 | ||
127 | public void debug(Marker marker, String format, Object arg) { | |
128 | recordEvent(Level.DEBUG, marker, format, new Object[] { arg }, null); | |
129 | } | |
130 | ||
131 | public void debug(Marker marker, String format, Object arg1, Object arg2) { | |
132 | recordEvent(Level.DEBUG, marker, format, new Object[] { arg1, arg2 }, null); | |
133 | } | |
134 | ||
135 | public void debug(Marker marker, String format, Object... arguments) { | |
136 | recordEvent(Level.DEBUG, marker, format, arguments, null); | |
137 | } | |
138 | ||
139 | public void debug(Marker marker, String msg, Throwable t) { | |
140 | recordEvent(Level.DEBUG, marker, msg, null, t); | |
141 | } | |
142 | ||
143 | public boolean isInfoEnabled() { | |
144 | return true; | |
145 | } | |
146 | ||
147 | public void info(String msg) { | |
148 | recordEvent(Level.INFO, msg, null, null); | |
149 | } | |
150 | ||
151 | public void info(String format, Object arg) { | |
152 | recordEvent(Level.INFO, format, new Object[] { arg }, null); | |
153 | } | |
154 | ||
155 | public void info(String format, Object arg1, Object arg2) { | |
156 | recordEvent(Level.INFO, format, new Object[] { arg1, arg2 }, null); | |
157 | } | |
158 | ||
159 | public void info(String format, Object... arguments) { | |
160 | recordEvent(Level.INFO, format, arguments, null); | |
161 | } | |
162 | ||
163 | public void info(String msg, Throwable t) { | |
164 | recordEvent(Level.INFO, msg, null, t); | |
165 | } | |
166 | ||
167 | public boolean isInfoEnabled(Marker marker) { | |
168 | return true; | |
169 | } | |
170 | ||
171 | public void info(Marker marker, String msg) { | |
172 | recordEvent(Level.INFO, marker, msg, null, null); | |
173 | } | |
174 | ||
175 | public void info(Marker marker, String format, Object arg) { | |
176 | recordEvent(Level.INFO, marker, format, new Object[] { arg }, null); | |
177 | } | |
178 | ||
179 | public void info(Marker marker, String format, Object arg1, Object arg2) { | |
180 | recordEvent(Level.INFO, marker, format, new Object[] { arg1, arg2 }, null); | |
181 | } | |
182 | ||
183 | public void info(Marker marker, String format, Object... arguments) { | |
184 | recordEvent(Level.INFO, marker, format, arguments, null); | |
185 | } | |
186 | ||
187 | public void info(Marker marker, String msg, Throwable t) { | |
188 | recordEvent(Level.INFO, marker, msg, null, t); | |
189 | ||
190 | } | |
191 | ||
192 | public boolean isWarnEnabled() { | |
193 | return true; | |
194 | } | |
195 | ||
196 | public void warn(String msg) { | |
197 | recordEvent(Level.WARN, msg, null, null); | |
198 | } | |
199 | ||
200 | public void warn(String format, Object arg) { | |
201 | recordEvent(Level.WARN, format, new Object[] { arg }, null); | |
202 | ||
203 | } | |
204 | ||
205 | public void warn(String format, Object arg1, Object arg2) { | |
206 | recordEvent(Level.WARN, format, new Object[] { arg1, arg2 }, null); | |
207 | } | |
208 | ||
209 | public void warn(String format, Object... arguments) { | |
210 | recordEvent(Level.WARN, format, arguments, null); | |
211 | } | |
212 | ||
213 | public void warn(String msg, Throwable t) { | |
214 | recordEvent(Level.WARN, msg, null, t); | |
215 | } | |
216 | ||
217 | public boolean isWarnEnabled(Marker marker) { | |
218 | return true; | |
219 | } | |
220 | ||
221 | public void warn(Marker marker, String msg) { | |
222 | recordEvent(Level.WARN, msg, null, null); | |
223 | } | |
224 | ||
225 | public void warn(Marker marker, String format, Object arg) { | |
226 | recordEvent(Level.WARN, format, new Object[] { arg }, null); | |
227 | } | |
228 | ||
229 | public void warn(Marker marker, String format, Object arg1, Object arg2) { | |
230 | recordEvent(Level.WARN, marker, format, new Object[] { arg1, arg2 }, null); | |
231 | ||
232 | } | |
233 | ||
234 | public void warn(Marker marker, String format, Object... arguments) { | |
235 | recordEvent(Level.WARN, marker, format, arguments, null); | |
236 | } | |
237 | ||
238 | public void warn(Marker marker, String msg, Throwable t) { | |
239 | recordEvent(Level.WARN, marker, msg, null, t); | |
240 | } | |
241 | ||
242 | public boolean isErrorEnabled() { | |
243 | return true; | |
244 | } | |
245 | ||
246 | public void error(String msg) { | |
247 | recordEvent(Level.ERROR, msg, null, null); | |
248 | } | |
249 | ||
250 | public void error(String format, Object arg) { | |
251 | recordEvent(Level.ERROR, format, new Object[] { arg }, null); | |
252 | ||
253 | } | |
254 | ||
255 | public void error(String format, Object arg1, Object arg2) { | |
256 | recordEvent(Level.ERROR, format, new Object[] { arg1, arg2 }, null); | |
257 | ||
258 | } | |
259 | ||
260 | public void error(String format, Object... arguments) { | |
261 | recordEvent(Level.ERROR, format, arguments, null); | |
262 | ||
263 | } | |
264 | ||
265 | public void error(String msg, Throwable t) { | |
266 | recordEvent(Level.ERROR, msg, null, t); | |
267 | } | |
268 | ||
269 | public boolean isErrorEnabled(Marker marker) { | |
270 | return true; | |
271 | } | |
272 | ||
273 | public void error(Marker marker, String msg) { | |
274 | recordEvent(Level.ERROR, marker, msg, null, null); | |
275 | ||
276 | } | |
277 | ||
278 | public void error(Marker marker, String format, Object arg) { | |
279 | recordEvent(Level.ERROR, marker, format, new Object[] { arg }, null); | |
280 | ||
281 | } | |
282 | ||
283 | public void error(Marker marker, String format, Object arg1, Object arg2) { | |
284 | recordEvent(Level.ERROR, marker, format, new Object[] { arg1, arg2 }, null); | |
285 | } | |
286 | ||
287 | public void error(Marker marker, String format, Object... arguments) { | |
288 | recordEvent(Level.ERROR, marker, format, arguments, null); | |
289 | } | |
290 | ||
291 | public void error(Marker marker, String msg, Throwable t) { | |
292 | recordEvent(Level.ERROR, marker, msg, null, t); | |
293 | } | |
294 | ||
295 | } |
0 | package org.slf4j.event; | |
1 | ||
2 | import static org.slf4j.event.EventConstants.DEBUG_INT; | |
3 | import static org.slf4j.event.EventConstants.ERROR_INT; | |
4 | import static org.slf4j.event.EventConstants.INFO_INT; | |
5 | import static org.slf4j.event.EventConstants.TRACE_INT; | |
6 | import static org.slf4j.event.EventConstants.WARN_INT; | |
7 | ||
8 | /** | |
9 | * | |
10 | * @author ceki | |
11 | * @since 1.7.15 | |
12 | */ | |
13 | public enum Level { | |
14 | ||
15 | ERROR(ERROR_INT, "ERROR"), WARN(WARN_INT, "WARN"), INFO(INFO_INT, "INFO"), DEBUG(DEBUG_INT, "DEBUG"), TRACE(TRACE_INT, "TRACE"); | |
16 | ||
17 | private int levelInt; | |
18 | private String levelStr; | |
19 | ||
20 | Level(int i, String s) { | |
21 | levelInt = i; | |
22 | levelStr = s; | |
23 | } | |
24 | ||
25 | public int toInt() { | |
26 | return levelInt; | |
27 | } | |
28 | ||
29 | /** | |
30 | * Returns the string representation of this Level. | |
31 | */ | |
32 | public String toString() { | |
33 | return levelStr; | |
34 | } | |
35 | ||
36 | } |
0 | package org.slf4j.event; | |
1 | ||
2 | import org.slf4j.Marker; | |
3 | ||
4 | /** | |
5 | * | |
6 | * @author ceki | |
7 | * @since 1.7.15 | |
8 | */ | |
9 | public interface LoggingEvent { | |
10 | ||
11 | Level getLevel(); | |
12 | ||
13 | Marker getMarker(); | |
14 | ||
15 | String getLoggerName(); | |
16 | ||
17 | String getMessage(); | |
18 | ||
19 | String getThreadName(); | |
20 | ||
21 | Object[] getArgumentArray(); | |
22 | ||
23 | long getTimeStamp(); | |
24 | ||
25 | Throwable getThrowable(); | |
26 | ||
27 | } |
0 | package org.slf4j.event; | |
1 | ||
2 | import org.slf4j.Marker; | |
3 | import org.slf4j.helpers.SubstituteLogger; | |
4 | ||
5 | public class SubstituteLoggingEvent implements LoggingEvent { | |
6 | ||
7 | Level level; | |
8 | Marker marker; | |
9 | String loggerName; | |
10 | SubstituteLogger logger; | |
11 | String threadName; | |
12 | String message; | |
13 | Object[] argArray; | |
14 | long timeStamp; | |
15 | Throwable throwable; | |
16 | ||
17 | public Level getLevel() { | |
18 | return level; | |
19 | } | |
20 | ||
21 | public void setLevel(Level level) { | |
22 | this.level = level; | |
23 | } | |
24 | ||
25 | public Marker getMarker() { | |
26 | return marker; | |
27 | } | |
28 | ||
29 | public void setMarker(Marker marker) { | |
30 | this.marker = marker; | |
31 | } | |
32 | ||
33 | public String getLoggerName() { | |
34 | return loggerName; | |
35 | } | |
36 | ||
37 | public void setLoggerName(String loggerName) { | |
38 | this.loggerName = loggerName; | |
39 | } | |
40 | ||
41 | public SubstituteLogger getLogger() { | |
42 | return logger; | |
43 | } | |
44 | ||
45 | public void setLogger(SubstituteLogger logger) { | |
46 | this.logger = logger; | |
47 | } | |
48 | ||
49 | public String getMessage() { | |
50 | return message; | |
51 | } | |
52 | ||
53 | public void setMessage(String message) { | |
54 | this.message = message; | |
55 | } | |
56 | ||
57 | public Object[] getArgumentArray() { | |
58 | return argArray; | |
59 | } | |
60 | ||
61 | public void setArgumentArray(Object[] argArray) { | |
62 | this.argArray = argArray; | |
63 | } | |
64 | ||
65 | public long getTimeStamp() { | |
66 | return timeStamp; | |
67 | } | |
68 | ||
69 | public void setTimeStamp(long timeStamp) { | |
70 | this.timeStamp = timeStamp; | |
71 | } | |
72 | ||
73 | public String getThreadName() { | |
74 | return threadName; | |
75 | } | |
76 | ||
77 | public void setThreadName(String threadName) { | |
78 | this.threadName = threadName; | |
79 | } | |
80 | ||
81 | public Throwable getThrowable() { | |
82 | return throwable; | |
83 | } | |
84 | ||
85 | public void setThrowable(Throwable throwable) { | |
86 | this.throwable = throwable; | |
87 | } | |
88 | } |
43 | 43 | */ |
44 | 44 | public class BasicMDCAdapter implements MDCAdapter { |
45 | 45 | |
46 | private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = | |
47 | new InheritableThreadLocal<Map<String, String>>() { | |
46 | private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = new InheritableThreadLocal<Map<String, String>>() { | |
48 | 47 | @Override |
49 | protected Map<String,String> childValue(Map<String,String> parentValue) { | |
48 | protected Map<String, String> childValue(Map<String, String> parentValue) { | |
50 | 49 | if (parentValue == null) { |
51 | 50 | return null; |
52 | 51 | } |
43 | 43 | public FormattingTuple(String message, Object[] argArray, Throwable throwable) { |
44 | 44 | this.message = message; |
45 | 45 | this.throwable = throwable; |
46 | if (throwable == null) { | |
47 | this.argArray = argArray; | |
48 | } else { | |
49 | this.argArray = trimmedCopy(argArray); | |
50 | } | |
51 | } | |
52 | ||
53 | static Object[] trimmedCopy(Object[] argArray) { | |
54 | if (argArray == null || argArray.length == 0) { | |
55 | throw new IllegalStateException("non-sensical empty or null argument array"); | |
56 | } | |
57 | final int trimemdLen = argArray.length - 1; | |
58 | Object[] trimmed = new Object[trimemdLen]; | |
59 | System.arraycopy(argArray, 0, trimmed, 0, trimemdLen); | |
60 | return trimmed; | |
46 | this.argArray = argArray; | |
61 | 47 | } |
62 | 48 | |
63 | 49 | public String getMessage() { |
32 | 32 | /** |
33 | 33 | * Formats messages according to very simple substitution rules. Substitutions |
34 | 34 | * can be made 1, 2 or more arguments. |
35 | * | |
35 | * | |
36 | 36 | * <p> |
37 | 37 | * For example, |
38 | * | |
38 | * | |
39 | 39 | * <pre> |
40 | 40 | * MessageFormatter.format("Hi {}.", "there") |
41 | 41 | * </pre> |
42 | * | |
42 | * | |
43 | 43 | * will return the string "Hi there.". |
44 | 44 | * <p> |
45 | 45 | * The {} pair is called the <em>formatting anchor</em>. It serves to designate |
49 | 49 | * In case your message contains the '{' or the '}' character, you do not have |
50 | 50 | * to do anything special unless the '}' character immediately follows '{'. For |
51 | 51 | * example, |
52 | * | |
52 | * | |
53 | 53 | * <pre> |
54 | 54 | * MessageFormatter.format("Set {1,2,3} is not equal to {}.", "1,2"); |
55 | 55 | * </pre> |
56 | * | |
56 | * | |
57 | 57 | * will return the string "Set {1,2,3} is not equal to 1,2.". |
58 | * | |
58 | * | |
59 | 59 | * <p> |
60 | 60 | * If for whatever reason you need to place the string "{}" in the message |
61 | 61 | * without its <em>formatting anchor</em> meaning, then you need to escape the |
62 | 62 | * '{' character with '\', that is the backslash character. Only the '{' |
63 | 63 | * character should be escaped. There is no need to escape the '}' character. |
64 | 64 | * For example, |
65 | * | |
65 | * | |
66 | 66 | * <pre> |
67 | 67 | * MessageFormatter.format("Set \\{} is not equal to {}.", "1,2"); |
68 | 68 | * </pre> |
69 | * | |
69 | * | |
70 | 70 | * will return the string "Set {} is not equal to 1,2.". |
71 | * | |
71 | * | |
72 | 72 | * <p> |
73 | 73 | * The escaping behavior just described can be overridden by escaping the escape |
74 | 74 | * character '\'. Calling |
75 | * | |
75 | * | |
76 | 76 | * <pre> |
77 | 77 | * MessageFormatter.format("File name is C:\\\\{}.", "file.zip"); |
78 | 78 | * </pre> |
79 | * | |
79 | * | |
80 | 80 | * will return the string "File name is C:\file.zip". |
81 | * | |
81 | * | |
82 | 82 | * <p> |
83 | 83 | * The formatting conventions are different than those of {@link MessageFormat} |
84 | 84 | * which ships with the Java platform. This is justified by the fact that |
85 | 85 | * SLF4J's implementation is 10 times faster than that of {@link MessageFormat}. |
86 | 86 | * This local performance difference is both measurable and significant in the |
87 | 87 | * larger context of the complete logging processing chain. |
88 | * | |
88 | * | |
89 | 89 | * <p> |
90 | 90 | * See also {@link #format(String, Object)}, |
91 | 91 | * {@link #format(String, Object, Object)} and |
92 | 92 | * {@link #arrayFormat(String, Object[])} methods for more details. |
93 | * | |
93 | * | |
94 | 94 | * @author Ceki Gülcü |
95 | 95 | * @author Joern Huxhorn |
96 | 96 | */ |
105 | 105 | * parameter. |
106 | 106 | * <p> |
107 | 107 | * For example, |
108 | * | |
108 | * | |
109 | 109 | * <pre> |
110 | 110 | * MessageFormatter.format("Hi {}.", "there"); |
111 | 111 | * </pre> |
112 | * | |
112 | * | |
113 | 113 | * will return the string "Hi there.". |
114 | 114 | * <p> |
115 | * | |
115 | * | |
116 | 116 | * @param messagePattern |
117 | 117 | * The message pattern which will be parsed and formatted |
118 | * @param argument | |
118 | * @param arg | |
119 | 119 | * The argument to be substituted in place of the formatting anchor |
120 | 120 | * @return The formatted message |
121 | 121 | */ |
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
127 | * | |
127 | * | |
128 | 128 | * Performs a two argument substitution for the 'messagePattern' passed as |
129 | 129 | * parameter. |
130 | 130 | * <p> |
131 | 131 | * For example, |
132 | * | |
132 | * | |
133 | 133 | * <pre> |
134 | 134 | * MessageFormatter.format("Hi {}. My name is {}.", "Alice", "Bob"); |
135 | 135 | * </pre> |
136 | * | |
136 | * | |
137 | 137 | * will return the string "Hi Alice. My name is Bob.". |
138 | * | |
138 | * | |
139 | 139 | * @param messagePattern |
140 | 140 | * The message pattern which will be parsed and formatted |
141 | 141 | * @param arg1 |
150 | 150 | return arrayFormat(messagePattern, new Object[] { arg1, arg2 }); |
151 | 151 | } |
152 | 152 | |
153 | ||
153 | 154 | static final Throwable getThrowableCandidate(Object[] argArray) { |
154 | 155 | if (argArray == null || argArray.length == 0) { |
155 | 156 | return null; |
162 | 163 | return null; |
163 | 164 | } |
164 | 165 | |
165 | /** | |
166 | * Same principle as the {@link #format(String, Object)} and | |
167 | * {@link #format(String, Object, Object)} methods except that any number of | |
168 | * arguments can be passed in an array. | |
169 | * | |
170 | * @param messagePattern | |
171 | * The message pattern which will be parsed and formatted | |
172 | * @param argArray | |
173 | * An array of arguments to be substituted in place of formatting | |
174 | * anchors | |
175 | * @return The formatted message | |
176 | */ | |
177 | 166 | final public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray) { |
178 | ||
179 | 167 | Throwable throwableCandidate = getThrowableCandidate(argArray); |
168 | Object[] args = argArray; | |
169 | if (throwableCandidate != null) { | |
170 | args = trimmedCopy(argArray); | |
171 | } | |
172 | return arrayFormat(messagePattern, args, throwableCandidate); | |
173 | } | |
174 | ||
175 | private static Object[] trimmedCopy(Object[] argArray) { | |
176 | if (argArray == null || argArray.length == 0) { | |
177 | throw new IllegalStateException("non-sensical empty or null argument array"); | |
178 | } | |
179 | final int trimemdLen = argArray.length - 1; | |
180 | Object[] trimmed = new Object[trimemdLen]; | |
181 | System.arraycopy(argArray, 0, trimmed, 0, trimemdLen); | |
182 | return trimmed; | |
183 | } | |
184 | ||
185 | final public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray, Throwable throwable) { | |
180 | 186 | |
181 | 187 | if (messagePattern == null) { |
182 | return new FormattingTuple(null, argArray, throwableCandidate); | |
188 | return new FormattingTuple(null, argArray, throwable); | |
183 | 189 | } |
184 | 190 | |
185 | 191 | if (argArray == null) { |
199 | 205 | if (j == -1) { |
200 | 206 | // no more variables |
201 | 207 | if (i == 0) { // this is a simple string |
202 | return new FormattingTuple(messagePattern, argArray, throwableCandidate); | |
208 | return new FormattingTuple(messagePattern, argArray, throwable); | |
203 | 209 | } else { // add the tail string which contains no variables and return |
204 | 210 | // the result. |
205 | 211 | sbuf.append(messagePattern, i, messagePattern.length()); |
206 | return new FormattingTuple(sbuf.toString(), argArray, throwableCandidate); | |
212 | return new FormattingTuple(sbuf.toString(), argArray, throwable); | |
207 | 213 | } |
208 | 214 | } else { |
209 | 215 | if (isEscapedDelimeter(messagePattern, j)) { |
230 | 236 | } |
231 | 237 | // append the characters following the last {} pair. |
232 | 238 | sbuf.append(messagePattern, i, messagePattern.length()); |
233 | if (L < argArray.length - 1) { | |
234 | return new FormattingTuple(sbuf.toString(), argArray, throwableCandidate); | |
235 | } else { | |
236 | return new FormattingTuple(sbuf.toString(), argArray, null); | |
237 | } | |
239 | return new FormattingTuple(sbuf.toString(), argArray, throwable); | |
238 | 240 | } |
239 | 241 | |
240 | 242 | final static boolean isEscapedDelimeter(String messagePattern, int delimeterStartIndex) { |
296 | 298 | String oAsString = o.toString(); |
297 | 299 | sbuf.append(oAsString); |
298 | 300 | } catch (Throwable t) { |
299 | System.err.println("SLF4J: Failed toString() invocation on an object of type [" + o.getClass().getName() + "]"); | |
300 | t.printStackTrace(); | |
301 | Util.report("SLF4J: Failed toString() invocation on an object of type [" + o.getClass().getName() + "]", t); | |
301 | 302 | sbuf.append("[FAILED toString()]"); |
302 | 303 | } |
303 | 304 | |
408 | 409 | } |
409 | 410 | sbuf.append(']'); |
410 | 411 | } |
412 | ||
411 | 413 | } |
23 | 23 | */ |
24 | 24 | package org.slf4j.helpers; |
25 | 25 | |
26 | import java.lang.reflect.InvocationTargetException; | |
27 | import java.lang.reflect.Method; | |
28 | import java.util.Queue; | |
29 | ||
26 | 30 | import org.slf4j.Logger; |
27 | 31 | import org.slf4j.Marker; |
32 | import org.slf4j.event.EventRecodingLogger; | |
33 | import org.slf4j.event.LoggingEvent; | |
34 | import org.slf4j.event.SubstituteLoggingEvent; | |
28 | 35 | |
29 | 36 | /** |
30 | 37 | * A logger implementation which logs via a delegate logger. By default, the delegate is a |
31 | * {@link NOPLogger}. However, a different delegate can be set at anytime. | |
38 | * {@link NOPLogger}. However, a different delegate can be set at any time. | |
32 | 39 | * <p/> |
33 | 40 | * See also the <a href="http://www.slf4j.org/codes.html#substituteLogger">relevant |
34 | 41 | * error code</a> documentation. |
35 | 42 | * |
36 | 43 | * @author Chetan Mehrotra |
44 | * @author Ceki Gulcu | |
37 | 45 | */ |
38 | 46 | public class SubstituteLogger implements Logger { |
39 | 47 | |
40 | 48 | private final String name; |
41 | ||
42 | 49 | private volatile Logger _delegate; |
43 | ||
44 | public SubstituteLogger(String name) { | |
50 | private Boolean delegateEventAware; | |
51 | private Method logMethodCache; | |
52 | private EventRecodingLogger eventRecodingLogger; | |
53 | private Queue<SubstituteLoggingEvent> eventQueue; | |
54 | ||
55 | public SubstituteLogger(String name, Queue<SubstituteLoggingEvent> eventQueue) { | |
45 | 56 | this.name = name; |
57 | this.eventQueue = eventQueue; | |
46 | 58 | } |
47 | 59 | |
48 | 60 | public String getName() { |
314 | 326 | * instance. |
315 | 327 | */ |
316 | 328 | Logger delegate() { |
317 | return _delegate != null ? _delegate : NOPLogger.NOP_LOGGER; | |
329 | return _delegate != null ? _delegate : getEventRecordingLogger(); | |
330 | } | |
331 | ||
332 | private Logger getEventRecordingLogger() { | |
333 | if (eventRecodingLogger == null) { | |
334 | eventRecodingLogger = new EventRecodingLogger(this, eventQueue); | |
335 | } | |
336 | return eventRecodingLogger; | |
318 | 337 | } |
319 | 338 | |
320 | 339 | /** |
324 | 343 | public void setDelegate(Logger delegate) { |
325 | 344 | this._delegate = delegate; |
326 | 345 | } |
346 | ||
347 | public boolean isDelegateEventAware() { | |
348 | if (delegateEventAware != null) | |
349 | return delegateEventAware; | |
350 | ||
351 | try { | |
352 | logMethodCache = _delegate.getClass().getMethod("log", LoggingEvent.class); | |
353 | delegateEventAware = Boolean.TRUE; | |
354 | } catch (NoSuchMethodException e) { | |
355 | delegateEventAware = Boolean.FALSE; | |
356 | } | |
357 | return delegateEventAware; | |
358 | } | |
359 | ||
360 | public void log(LoggingEvent event) { | |
361 | if (isDelegateEventAware()) { | |
362 | try { | |
363 | logMethodCache.invoke(_delegate, event); | |
364 | } catch (IllegalAccessException e) { | |
365 | } catch (IllegalArgumentException e) { | |
366 | } catch (InvocationTargetException e) { | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
371 | ||
372 | public boolean isDelegateNull() { | |
373 | return _delegate == null; | |
374 | } | |
375 | ||
376 | public boolean isDelegateNOP() { | |
377 | return _delegate instanceof NOPLogger; | |
378 | } | |
327 | 379 | } |
23 | 23 | */ |
24 | 24 | package org.slf4j.helpers; |
25 | 25 | |
26 | import org.slf4j.ILoggerFactory; | |
27 | import org.slf4j.Logger; | |
28 | ||
29 | 26 | import java.util.ArrayList; |
30 | 27 | import java.util.List; |
31 | 28 | import java.util.concurrent.ConcurrentHashMap; |
32 | 29 | import java.util.concurrent.ConcurrentMap; |
30 | import java.util.concurrent.LinkedBlockingQueue; | |
31 | ||
32 | import org.slf4j.ILoggerFactory; | |
33 | import org.slf4j.Logger; | |
34 | import org.slf4j.event.SubstituteLoggingEvent; | |
33 | 35 | |
34 | 36 | /** |
35 | 37 | * SubstituteLoggerFactory manages instances of {@link SubstituteLogger}. |
41 | 43 | |
42 | 44 | final ConcurrentMap<String, SubstituteLogger> loggers = new ConcurrentHashMap<String, SubstituteLogger>(); |
43 | 45 | |
46 | final LinkedBlockingQueue<SubstituteLoggingEvent> eventQueue = new LinkedBlockingQueue<SubstituteLoggingEvent>(); | |
47 | ||
44 | 48 | public Logger getLogger(String name) { |
45 | 49 | SubstituteLogger logger = loggers.get(name); |
46 | 50 | if (logger == null) { |
47 | logger = new SubstituteLogger(name); | |
51 | logger = new SubstituteLogger(name, eventQueue); | |
48 | 52 | SubstituteLogger oldLogger = loggers.putIfAbsent(name, logger); |
49 | 53 | if (oldLogger != null) |
50 | 54 | logger = oldLogger; |
60 | 64 | return new ArrayList<SubstituteLogger>(loggers.values()); |
61 | 65 | } |
62 | 66 | |
67 | public LinkedBlockingQueue<SubstituteLoggingEvent> getEventQueue() { | |
68 | return eventQueue; | |
69 | } | |
70 | ||
63 | 71 | public void clear() { |
64 | 72 | loggers.clear(); |
73 | eventQueue.clear(); | |
65 | 74 | } |
66 | 75 | } |
68 | 68 | |
69 | 69 | private static ClassContextSecurityManager SECURITY_MANAGER; |
70 | 70 | private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false; |
71 | ||
71 | ||
72 | 72 | private static ClassContextSecurityManager getSecurityManager() { |
73 | if(SECURITY_MANAGER != null) | |
73 | if (SECURITY_MANAGER != null) | |
74 | 74 | return SECURITY_MANAGER; |
75 | else if(SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED) | |
75 | else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED) | |
76 | 76 | return null; |
77 | 77 | else { |
78 | SECURITY_MANAGER = safeCreateSecurityManager(); | |
78 | SECURITY_MANAGER = safeCreateSecurityManager(); | |
79 | 79 | SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true; |
80 | 80 | return SECURITY_MANAGER; |
81 | 81 | } |
82 | 82 | } |
83 | ||
83 | ||
84 | 84 | private static ClassContextSecurityManager safeCreateSecurityManager() { |
85 | 85 | try { |
86 | 86 | return new ClassContextSecurityManager(); |
96 | 96 | */ |
97 | 97 | public static Class<?> getCallingClass() { |
98 | 98 | ClassContextSecurityManager securityManager = getSecurityManager(); |
99 | if(securityManager == null) | |
99 | if (securityManager == null) | |
100 | 100 | return null; |
101 | 101 | Class<?>[] trace = securityManager.getClassContext(); |
102 | 102 | String thisClassName = Util.class.getName(); |
26 | 26 | import org.slf4j.ILoggerFactory; |
27 | 27 | |
28 | 28 | /** |
29 | * The binding of {@link LoggerFactory} class with an actual instance of | |
29 | * The binding of {@link org.slf4j.LoggerFactory} class with an actual instance of | |
30 | 30 | * {@link ILoggerFactory} is performed using information returned by this class. |
31 | * | |
32 | * This class is meant to provide a dummy StaticLoggerBinder to the slf4j-api module. | |
33 | * Real implementations are found in each SLF4J binding project, e.g. slf4j-nop, | |
31 | * | |
32 | * This class is meant to provide a dummy StaticLoggerBinder to the slf4j-api module. | |
33 | * Real implementations are found in each SLF4J binding project, e.g. slf4j-nop, | |
34 | 34 | * slf4j-log4j12 etc. |
35 | * | |
35 | * | |
36 | 36 | * @author Ceki Gülcü |
37 | 37 | */ |
38 | 38 | public class StaticLoggerBinder { |
44 | 44 | |
45 | 45 | /** |
46 | 46 | * Return the singleton of this class. |
47 | * | |
47 | * | |
48 | 48 | * @return the StaticLoggerBinder singleton |
49 | 49 | */ |
50 | 50 | public static final StaticLoggerBinder getSingleton() { |
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
55 | * Declare the version of the SLF4J API this implementation is compiled against. | |
56 | * The value of this field is modified with each major release. | |
55 | * Declare the version of the SLF4J API this implementation is compiled against. | |
56 | * The value of this field is modified with each major release. | |
57 | 57 | */ |
58 | 58 | // to avoid constant folding by the compiler, this field must *not* be final |
59 | 59 | public static String REQUESTED_API_VERSION = "1.6.99"; // !final |
41 | 41 | private StaticMDCBinder() { |
42 | 42 | throw new UnsupportedOperationException("This code should never make it into the jar"); |
43 | 43 | } |
44 | ||
44 | ||
45 | 45 | /** |
46 | 46 | * Return the singleton of this class. |
47 | 47 | * |
49 | 49 | * @since 1.7.14 |
50 | 50 | */ |
51 | 51 | public static final StaticMDCBinder getSingleton() { |
52 | return SINGLETON; | |
52 | return SINGLETON; | |
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
59 | 59 | public static StaticMarkerBinder getSingleton() { |
60 | 60 | return SINGLETON; |
61 | 61 | } |
62 | ||
62 | ||
63 | 63 | /** |
64 | 64 | * Currently this method always returns an instance of |
65 | 65 | * {@link BasicMarkerFactory}. |
38 | 38 | */ |
39 | 39 | public interface LocationAwareLogger extends Logger { |
40 | 40 | |
41 | // these constants should be in EventContants. However, in order to preserve binary backward compatibility | |
42 | // we keep these constants here | |
41 | 43 | final public int TRACE_INT = 00; |
42 | 44 | final public int DEBUG_INT = 10; |
43 | 45 | final public int INFO_INT = 20; |
3 | 3 | Bundle-Name: slf4j-api |
4 | 4 | Bundle-Vendor: SLF4J.ORG |
5 | 5 | Bundle-RequiredExecutionEnvironment: J2SE-1.5 |
6 | Export-Package: org.slf4j;version=${parsedVersion.osgiVersion}, org.slf4j.spi;version=${parsedVersion.osgiVersion}, org.slf4j.helpers;version=${parsedVersion.osgiVersion} | |
6 | Export-Package: org.slf4j;version=${parsedVersion.osgiVersion}, | |
7 | org.slf4j.spi;version=${parsedVersion.osgiVersion}, | |
8 | org.slf4j.helpers;version=${parsedVersion.osgiVersion}, | |
9 | org.slf4j.event;version=${parsedVersion.osgiVersion} | |
7 | 10 | Import-Package: org.slf4j.impl;version=${slf4j.api.minimum.compatible.version} |
36 | 36 | * @author Ceki Gülcü |
37 | 37 | * @author Joern Huxhorn |
38 | 38 | */ |
39 | public class BasicMarkerTest { | |
39 | public class BasicMarkerTest { | |
40 | 40 | static final String BLUE_STR = "BLUE"; |
41 | 41 | static final String RED_STR = "RED"; |
42 | 42 | static final String GREEN_STR = "GREEN"; |
0 | package org.slf4j; | |
1 | ||
2 | import org.junit.Ignore; | |
3 | import org.junit.Test; | |
4 | ||
5 | public class FindStaticLoggerBinderPathsPerfTest { | |
6 | ||
7 | @Test | |
8 | @Ignore | |
9 | public void test() { | |
10 | long duration = timeFindBindingSetCall(); | |
11 | System.out.println(duration / (1000) + " microseconds"); | |
12 | ||
13 | int count = 10; | |
14 | long sum = 0; | |
15 | for (int i = 0; i < count; i++) { | |
16 | sum += timeFindBindingSetCall(); | |
17 | } | |
18 | System.out.println(sum / (count * 1000) + " microseconds in average"); | |
19 | } | |
20 | ||
21 | @Test | |
22 | public void testAsync() throws InterruptedException { | |
23 | long start = System.nanoTime(); | |
24 | FindPathSetThread thread = new FindPathSetThread(); | |
25 | thread.start(); | |
26 | long end = System.nanoTime(); | |
27 | ||
28 | long duration = end - start; | |
29 | System.out.println(duration / (1000) + " microseconds"); | |
30 | ||
31 | thread.join(); | |
32 | } | |
33 | ||
34 | long timeFindBindingSetCall() { | |
35 | long start = System.nanoTime(); | |
36 | ||
37 | LoggerFactory.findPossibleStaticLoggerBinderPathSet(); | |
38 | long end = System.nanoTime(); | |
39 | return end - start; | |
40 | ||
41 | } | |
42 | ||
43 | static class FindPathSetThread extends Thread { | |
44 | ||
45 | public void run() { | |
46 | long start = System.nanoTime(); | |
47 | LoggerFactory.findPossibleStaticLoggerBinderPathSet(); | |
48 | long end = System.nanoTime(); | |
49 | ||
50 | System.out.println("Found set in " + (end - start)/1000 + " microseconds"); | |
51 | ||
52 | } | |
53 | } | |
54 | } |
0 | package org.slf4j; | |
1 | ||
2 | import org.junit.Ignore; | |
3 | import org.junit.Test; | |
4 | ||
5 | public class FindStaticLoggerBinderPathsPerfTest { | |
6 | ||
7 | @Test | |
8 | @Ignore | |
9 | public void test() { | |
10 | long duration = timeFindBindingSetCall(); | |
11 | System.out.println(duration / (1000) + " microseconds"); | |
12 | ||
13 | int count = 10; | |
14 | long sum = 0; | |
15 | for (int i = 0; i < count; i++) { | |
16 | sum += timeFindBindingSetCall(); | |
17 | } | |
18 | System.out.println(sum / (count * 1000) + " microseconds in average"); | |
19 | } | |
20 | ||
21 | @Test | |
22 | public void testAsync() throws InterruptedException { | |
23 | long start = System.nanoTime(); | |
24 | FindPathSetThread thread = new FindPathSetThread(); | |
25 | thread.start(); | |
26 | long end = System.nanoTime(); | |
27 | ||
28 | long duration = end - start; | |
29 | System.out.println(duration / (1000) + " microseconds"); | |
30 | ||
31 | thread.join(); | |
32 | } | |
33 | ||
34 | long timeFindBindingSetCall() { | |
35 | long start = System.nanoTime(); | |
36 | ||
37 | LoggerFactory.findPossibleStaticLoggerBinderPathSet(); | |
38 | long end = System.nanoTime(); | |
39 | return end - start; | |
40 | ||
41 | } | |
42 | ||
43 | static class FindPathSetThread extends Thread { | |
44 | ||
45 | public void run() { | |
46 | long start = System.nanoTime(); | |
47 | LoggerFactory.findPossibleStaticLoggerBinderPathSet(); | |
48 | long end = System.nanoTime(); | |
49 | ||
50 | System.out.println("Found set in " + (end - start)/1000 + " microseconds"); | |
51 | ||
52 | } | |
53 | } | |
54 | } |
44 | 44 | } |
45 | 45 | |
46 | 46 | @Test |
47 | public void testMDC() { | |
47 | public void testMDC() { | |
48 | 48 | MDC.put("k" + diff, "v"); |
49 | 49 | assertNull(MDC.get("k")); |
50 | 50 | } |
40 | 40 | * |
41 | 41 | * @author Lukasz Cwik |
42 | 42 | */ |
43 | public class BasicMDCAdapterTest { | |
43 | public class BasicMDCAdapterTest { | |
44 | 44 | MDCAdapter mdc = new BasicMDCAdapter(); |
45 | 45 | |
46 | 46 | @After |
54 | 54 | mdc.put("testKey", "testValue"); |
55 | 55 | assertEquals(mdc.get("testKey"), "testValue"); |
56 | 56 | } |
57 | ||
57 | ||
58 | 58 | @Test |
59 | 59 | public void testOverwritingAKeyInMDC() { |
60 | 60 | assertNull(mdc.get("testKey")); |
29 | 29 | import org.junit.Test; |
30 | 30 | |
31 | 31 | @Ignore |
32 | public class MessageFormatterPerfTest{ //extends TestCase { | |
32 | public class MessageFormatterPerfTest { // extends TestCase { | |
33 | 33 | |
34 | 34 | Integer i1 = new Integer(1); |
35 | 35 | Integer i2 = new Integer(2); |
310 | 310 | assertTrue(Arrays.equals(iaWitness, ft.getArgArray())); |
311 | 311 | assertEquals(t, ft.getThrowable()); |
312 | 312 | |
313 | ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {} -- {} .", ia); | |
314 | assertEquals("Value 1 is smaller than 2 and 3 -- " + t.toString() + " .", ft.getMessage()); | |
315 | assertTrue(Arrays.equals(ia, ft.getArgArray())); | |
316 | assertNull(ft.getThrowable()); | |
313 | ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia); | |
314 | assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage()); | |
315 | assertTrue(Arrays.equals(iaWitness, ft.getArgArray())); | |
316 | assertEquals(t, ft.getThrowable()); | |
317 | 317 | |
318 | 318 | ft = MessageFormatter.arrayFormat("{}{}{}{}", ia); |
319 | assertEquals("123" + t.toString(), ft.getMessage()); | |
320 | assertTrue(Arrays.equals(ia, ft.getArgArray())); | |
321 | assertNull(ft.getThrowable()); | |
319 | assertEquals("123{}", ft.getMessage()); | |
320 | assertTrue(Arrays.equals(iaWitness, ft.getArgArray())); | |
321 | assertEquals(t, ft.getThrowable()); | |
322 | ||
323 | ft = MessageFormatter.arrayFormat("1={}", new Object[] { i1 }, t); | |
324 | assertEquals("1=1", ft.getMessage()); | |
325 | assertTrue(Arrays.equals(new Object[] { i1 }, ft.getArgArray())); | |
326 | assertEquals(t, ft.getThrowable()); | |
327 | ||
322 | 328 | } |
323 | 329 | } |
38 | 38 | |
39 | 39 | import org.junit.Test; |
40 | 40 | import org.slf4j.Logger; |
41 | import org.slf4j.event.EventRecodingLogger; | |
41 | 42 | |
42 | 43 | /** |
43 | 44 | * @author Chetan Mehrotra |
47 | 48 | |
48 | 49 | @Test |
49 | 50 | public void testDelegate() throws Exception { |
50 | SubstituteLogger log = new SubstituteLogger("foo"); | |
51 | assertTrue(log.delegate() instanceof NOPLogger); | |
51 | SubstituteLogger log = new SubstituteLogger("foo", null); | |
52 | assertTrue(log.delegate() instanceof EventRecodingLogger); | |
52 | 53 | |
53 | 54 | Set<String> expectedMethodSignatures = determineMethodSignatures(Logger.class); |
54 | 55 | LoggerInvocationHandler ih = new LoggerInvocationHandler(); |
34 | 34 | import java.util.HashSet; |
35 | 35 | import java.util.Set; |
36 | 36 | |
37 | public class SubstituteLoggerFactoryTest { | |
37 | public class SubstituteLoggerFactoryTest { | |
38 | 38 | private SubstituteLoggerFactory factory = new SubstituteLoggerFactory(); |
39 | 39 | |
40 | 40 | @Test |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-ext</artifactId> |
33 | 33 | |
34 | 34 | /** |
35 | 35 | * Entry point for slf4j-ext when used as a Java agent. |
36 | * | |
36 | * | |
37 | 37 | */ |
38 | 38 | public class AgentPremain { |
39 | 39 | |
40 | 40 | /** |
41 | 41 | * JavaAgent premain entry point as specified in the MANIFEST.MF file. See |
42 | * {@link http | |
43 | * ://java.sun.com/javase/6/docs/api/java/lang/instrument/package- | |
44 | * summary.html} for details. | |
45 | * | |
42 | * <a href="http://java.sun.com/javase/6/docs/api/java/lang/instrument/package-summary.html">http://java.sun.com/javase/6/docs/api/java/lang/instrument/package-summary.html</a> for details. | |
43 | * | |
46 | 44 | * @param agentArgument |
47 | 45 | * string provided after "=" up to first space |
48 | 46 | * @param instrumentation |
84 | 82 | * Consider the argument string to be a property file (by converting the |
85 | 83 | * splitter character to line feeds), and then reading it like any other |
86 | 84 | * property file. |
87 | * | |
88 | * | |
85 | * | |
86 | * | |
89 | 87 | * @param agentArgument |
90 | 88 | * string given by instrumentation framework |
91 | 89 | * @param separator |
108 | 106 | * Print the start message to System.err with the time NOW, and register a |
109 | 107 | * shutdown hook which will print the stop message to System.err with the |
110 | 108 | * time then and the number of milliseconds passed since. |
111 | * | |
109 | * | |
112 | 110 | */ |
113 | 111 | private static void printStartStopTimes() { |
114 | 112 | final long start = System.currentTimeMillis(); |
124 | 122 | }; |
125 | 123 | Runtime.getRuntime().addShutdownHook(hook); |
126 | 124 | } |
127 | }⏎ | |
125 | } |
29 | 29 | import ch.qos.cal10n.IMessageConveyor; |
30 | 30 | |
31 | 31 | /** |
32 | * | |
32 | * | |
33 | 33 | * This class is essentially a wrapper around an {@link LoggerFactory} producing |
34 | 34 | * {@link LocLogger} instances. |
35 | * | |
35 | * | |
36 | 36 | * <p> |
37 | 37 | * Contrary to {@link LoggerFactory#getLogger(String)} method of |
38 | * {@link LoggerFactory}, each call to {@link getLocLogger} produces a new | |
38 | * {@link LoggerFactory}, each call to {@link #getLocLogger(String)} produces a new | |
39 | 39 | * instance of {@link LocLogger}. This should not matter because a LocLogger |
40 | * instance does have any state beyond that of the {@link Logger} in stance it | |
40 | * instance does have any state beyond that of the {@link Logger} instance it | |
41 | 41 | * wraps and its message conveyor. |
42 | * | |
42 | * | |
43 | 43 | * @author Ceki Gücü |
44 | * | |
44 | * | |
45 | 45 | */ |
46 | 46 | public class LocLoggerFactory { |
47 | 47 | |
53 | 53 | |
54 | 54 | /** |
55 | 55 | * Get an LocLogger instance by name. |
56 | * | |
56 | * | |
57 | 57 | * @param name |
58 | 58 | * @return LocLogger instance by name. |
59 | 59 | */ |
64 | 64 | /** |
65 | 65 | * Get a new LocLogger instance by class. The returned LocLogger will be named |
66 | 66 | * after the class. |
67 | * | |
67 | * | |
68 | 68 | * @param clazz |
69 | 69 | * @return LocLogger instance by class |
70 | 70 | */ |
22 | 22 | * |
23 | 23 | */ |
24 | 24 | /** |
25 | * | |
25 | * | |
26 | 26 | */ |
27 | 27 | package org.slf4j.instrumentation; |
28 | 28 | |
56 | 56 | /** |
57 | 57 | * Builder provides a flexible way of configuring some of many options on the |
58 | 58 | * parent class instead of providing many constructors. |
59 | * | |
60 | * {@link http | |
61 | * ://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html} | |
62 | * | |
59 | * | |
60 | * <a href="http://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html">http://rwhansen.blogspot.com/2007/07/theres-builder-pattern-that-joshua.html</a> | |
61 | * | |
63 | 62 | */ |
64 | 63 | public static class Builder { |
65 | 64 | |
66 | 65 | /** |
67 | 66 | * Build and return the LogTransformer corresponding to the options set in |
68 | 67 | * this Builder. |
69 | * | |
68 | * | |
70 | 69 | * @return |
71 | 70 | */ |
72 | 71 | public LogTransformer build() { |
81 | 80 | /** |
82 | 81 | * Should each method log entry (with parameters) and exit (with parameters |
83 | 82 | * and returnvalue)? |
84 | * | |
83 | * | |
85 | 84 | * @param b |
86 | 85 | * value of flag |
87 | 86 | * @return |
104 | 103 | /** |
105 | 104 | * Should LogTransformer be verbose in what it does? This currently list the |
106 | 105 | * names of the classes being processed. |
107 | * | |
106 | * | |
108 | 107 | * @param b |
109 | 108 | * @return |
110 | 109 | */ |
176 | 175 | * transform0 sees if the className starts with any of the namespaces to |
177 | 176 | * ignore, if so it is returned unchanged. Otherwise it is processed by |
178 | 177 | * doClass(...) |
179 | * | |
178 | * | |
180 | 179 | * @param className |
181 | 180 | * @param clazz |
182 | 181 | * @param domain |
226 | 225 | * defined have bodies, and a static final logger object is added with the |
227 | 226 | * name of this class as an argument, and each method then gets processed with |
228 | 227 | * doMethod(...) to have logger calls added. |
229 | * | |
228 | * | |
230 | 229 | * @param name |
231 | 230 | * class name (slashes separate, not dots) |
232 | 231 | * @param clazz |
284 | 283 | /** |
285 | 284 | * process a single method - this means add entry/exit logging if requested. |
286 | 285 | * It is only called for methods with a body. |
287 | * | |
286 | * | |
288 | 287 | * @param method |
289 | 288 | * method to work on |
290 | 289 | * @throws NotFoundException |
309 | 308 | method.insertAfter(after); |
310 | 309 | } |
311 | 310 | } |
312 | }⏎ | |
311 | } |
40 | 40 | assertEquals("", NDC.pop()); |
41 | 41 | } |
42 | 42 | |
43 | ||
44 | 43 | @Test |
45 | 44 | public void testSmoke() { |
46 | 45 | NDC.push("a"); |
47 | 46 | String result = NDC.pop(); |
48 | 47 | assertEquals("a", result); |
49 | 48 | } |
50 | ||
51 | 49 | |
52 | 50 | @Test |
53 | 51 | public void testSmoke2() { |
36 | 36 | |
37 | 37 | import ch.qos.cal10n.IMessageConveyor; |
38 | 38 | import ch.qos.cal10n.MessageConveyor; |
39 | public class LocLoggerTest { | |
39 | ||
40 | public class LocLoggerTest { | |
40 | 41 | |
41 | 42 | ListAppender listAppender; |
42 | 43 | org.apache.log4j.Logger log4jRoot; |
45 | 46 | LocLoggerFactory llFactory_uk = new LocLoggerFactory(imc); |
46 | 47 | |
47 | 48 | final static String EXPECTED_FILE_NAME = "LocLoggerTest.java"; |
48 | ||
49 | 49 | |
50 | 50 | @Before |
51 | 51 | public void setUp() throws Exception { |
63 | 63 | assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName()); |
64 | 64 | } |
65 | 65 | |
66 | ||
67 | 66 | @Test |
68 | 67 | public void testSmoke() { |
69 | 68 | LocLogger locLogger = llFactory_uk.getLocLogger(this.getClass()); |
37 | 37 | import org.slf4j.MDC; |
38 | 38 | import org.slf4j.ext.EventData; |
39 | 39 | import org.slf4j.ext.EventLogger; |
40 | public class EventLoggerTest { | |
40 | ||
41 | public class EventLoggerTest { | |
41 | 42 | |
42 | 43 | ListAppender listAppender; |
43 | 44 | org.apache.log4j.Logger log4; |
44 | 45 | |
45 | 46 | final static String EXPECTED_FILE_NAME = "EventLoggerTest.java"; |
46 | ||
47 | 47 | |
48 | 48 | @Before |
49 | 49 | public void setUp() throws Exception { |
75 | 75 | assertEquals(expectedMsg, le.getMessage()); |
76 | 76 | assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName()); |
77 | 77 | } |
78 | ||
78 | ||
79 | 79 | @Test |
80 | 80 | public void testEventLogger() { |
81 | 81 | EventData data[] = new EventData[2]; |
28 | 28 | import org.junit.Test; |
29 | 29 | import org.slf4j.MDC; |
30 | 30 | import org.slf4j.ext.MDCStrLookup; |
31 | public class MDCStrLookupTest { | |
31 | ||
32 | public class MDCStrLookupTest { | |
32 | 33 | |
33 | 34 | @Test |
34 | 35 | public void testLookup() throws Exception { |
23 | 23 | */ |
24 | 24 | package org.slf4j.dummyExt; |
25 | 25 | |
26 | import junit.framework.*; | |
27 | ||
28 | 26 | import org.junit.runner.RunWith; |
29 | 27 | import org.junit.runners.Suite; |
30 | 28 | import org.junit.runners.Suite.SuiteClasses; |
27 | 27 | |
28 | 28 | import org.junit.Test; |
29 | 29 | |
30 | public class ToStringHelperTest { | |
30 | public class ToStringHelperTest { | |
31 | 31 | |
32 | 32 | @Test |
33 | 33 | public void testRenderer() { |
28 | 28 | import org.junit.runners.Suite.SuiteClasses; |
29 | 29 | |
30 | 30 | @RunWith(Suite.class) |
31 | @SuiteClasses({UtilTest.class, | |
32 | ProfilerTest.class}) | |
31 | @SuiteClasses({ UtilTest.class, ProfilerTest.class }) | |
33 | 32 | public class PackageTest { |
34 | 33 | |
35 | ||
36 | 34 | }⏎ |
30 | 30 | import org.junit.Test; |
31 | 31 | import org.slf4j.Logger; |
32 | 32 | import org.slf4j.LoggerFactory; |
33 | ||
33 | 34 | public class ProfilerTest { |
34 | 35 | |
35 | 36 | Logger logger = LoggerFactory.getLogger(ProfilerTest.class); |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-jcl</artifactId> |
56 | 56 | // to avoid constant folding by the compiler, this field must *not* be final |
57 | 57 | public static String REQUESTED_API_VERSION = "1.6.99"; // !final |
58 | 58 | |
59 | ||
60 | 59 | // Binding specific code: |
61 | 60 | private static final String loggerFactoryClassStr = JCLLoggerFactory.class.getName(); |
62 | 61 |
56 | 56 | public static StaticMarkerBinder getSingleton() { |
57 | 57 | return SINGLETON; |
58 | 58 | } |
59 | ||
59 | ||
60 | 60 | /** |
61 | 61 | * Currently this method always returns an instance of |
62 | 62 | * {@link BasicMarkerFactory}. |
41 | 41 | |
42 | 42 | Level oldLevel; |
43 | 43 | java.util.logging.Logger root = java.util.logging.Logger.getLogger(""); |
44 | ||
45 | 44 | |
46 | 45 | @Before |
47 | 46 | public void setUp() throws Exception { |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-jdk14</artifactId> |
28 | 28 | |
29 | 29 | import org.slf4j.Logger; |
30 | 30 | import org.slf4j.Marker; |
31 | import org.slf4j.event.EventConstants; | |
32 | import org.slf4j.event.LoggingEvent; | |
31 | 33 | import org.slf4j.helpers.FormattingTuple; |
32 | 34 | import org.slf4j.helpers.MarkerIgnoringBase; |
33 | 35 | import org.slf4j.helpers.MessageFormatter; |
573 | 575 | LogRecord record = new LogRecord(level, msg); |
574 | 576 | record.setLoggerName(getName()); |
575 | 577 | record.setThrown(t); |
576 | // Note: parameters in record are not set because SLF4J only | |
578 | // Note: parameters in record are not set because SLF4J only | |
577 | 579 | // supports a single formatting style |
578 | 580 | fillCallerData(callerFQCN, record); |
579 | 581 | logger.log(record); |
580 | ||
581 | 582 | } |
582 | 583 | |
583 | 584 | static String SELF = JDK14LoggerAdapter.class.getName(); |
620 | 621 | } |
621 | 622 | |
622 | 623 | public void log(Marker marker, String callerFQCN, int level, String message, Object[] argArray, Throwable t) { |
624 | Level julLevel = slf4jLevelIntToJULLevel(level); | |
625 | // the logger.isLoggable check avoids the unconditional | |
626 | // construction of location data for disabled log | |
627 | // statements. As of 2008-07-31, callers of this method | |
628 | // do not perform this check. See also | |
629 | // http://jira.qos.ch/browse/SLF4J-81 | |
630 | if (logger.isLoggable(julLevel)) { | |
631 | log(callerFQCN, julLevel, message, t); | |
632 | } | |
633 | } | |
634 | ||
635 | private Level slf4jLevelIntToJULLevel(int slf4jLevelInt) { | |
623 | 636 | Level julLevel; |
624 | switch (level) { | |
637 | switch (slf4jLevelInt) { | |
625 | 638 | case LocationAwareLogger.TRACE_INT: |
626 | 639 | julLevel = Level.FINEST; |
627 | 640 | break; |
638 | 651 | julLevel = Level.SEVERE; |
639 | 652 | break; |
640 | 653 | default: |
641 | throw new IllegalStateException("Level number " + level + " is not recognized."); | |
642 | } | |
643 | // the logger.isLoggable check avoids the unconditional | |
644 | // construction of location data for disabled log | |
645 | // statements. As of 2008-07-31, callers of this method | |
646 | // do not perform this check. See also | |
647 | // http://jira.qos.ch/browse/SLF4J-81 | |
654 | throw new IllegalStateException("Level number " + slf4jLevelInt + " is not recognized."); | |
655 | } | |
656 | return julLevel; | |
657 | } | |
658 | ||
659 | /** | |
660 | * @since 1.7.15 | |
661 | */ | |
662 | public void log(LoggingEvent event) { | |
663 | Level julLevel = slf4jLevelIntToJULLevel(event.getLevel().toInt()); | |
648 | 664 | if (logger.isLoggable(julLevel)) { |
649 | log(callerFQCN, julLevel, message, t); | |
650 | } | |
665 | LogRecord record = eventToRecord(event, julLevel); | |
666 | logger.log(record); | |
667 | } | |
668 | } | |
669 | ||
670 | private LogRecord eventToRecord(LoggingEvent event, Level julLevel) { | |
671 | String format = event.getMessage(); | |
672 | Object[] arguments = event.getArgumentArray(); | |
673 | FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments); | |
674 | if (ft.getThrowable() != null && event.getThrowable() != null) { | |
675 | throw new IllegalArgumentException("both last element in argument array and last argument are of type Throwable"); | |
676 | } | |
677 | ||
678 | Throwable t = event.getThrowable(); | |
679 | if (ft.getThrowable() != null) { | |
680 | t = ft.getThrowable(); | |
681 | throw new IllegalStateException("fix above code"); | |
682 | } | |
683 | ||
684 | LogRecord record = new LogRecord(julLevel, ft.getMessage()); | |
685 | record.setLoggerName(event.getLoggerName()); | |
686 | record.setMillis(event.getTimeStamp()); | |
687 | record.setSourceClassName(EventConstants.NA_SUBST); | |
688 | record.setSourceMethodName(EventConstants.NA_SUBST); | |
689 | ||
690 | record.setThrown(t); | |
691 | return record; | |
651 | 692 | } |
652 | 693 | } |
42 | 42 | |
43 | 43 | public JDK14LoggerFactory() { |
44 | 44 | loggerMap = new ConcurrentHashMap<String, Logger>(); |
45 | // ensure jul initialization. see also SLF4J-359 | |
46 | java.util.logging.LogManager.getLogManager(); | |
45 | 47 | } |
46 | 48 | |
47 | 49 | /* |
40 | 40 | |
41 | 41 | private StaticMDCBinder() { |
42 | 42 | } |
43 | ||
43 | ||
44 | 44 | /** |
45 | 45 | * Return the singleton of this class. |
46 | 46 | * |
50 | 50 | public static final StaticMDCBinder getSingleton() { |
51 | 51 | return SINGLETON; |
52 | 52 | } |
53 | ||
53 | ||
54 | 54 | /** |
55 | 55 | * Currently this method always returns an instance of |
56 | 56 | * {@link BasicMDCAdapter}. |
56 | 56 | public static StaticMarkerBinder getSingleton() { |
57 | 57 | return SINGLETON; |
58 | 58 | } |
59 | ||
59 | ||
60 | 60 | /** |
61 | 61 | * Currently this method always returns an instance of |
62 | 62 | * {@link BasicMarkerFactory}. |
4 | 4 | Bundle-Vendor: SLF4J.ORG |
5 | 5 | Bundle-RequiredExecutionEnvironment: J2SE-1.5 |
6 | 6 | Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion} |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, org.slf4j.spi;version=${parsedVersion.osgiVersion}, org.slf4j.helpers;version=${parsedVersion.osgiVersion} | |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, | |
8 | org.slf4j.spi;version=${parsedVersion.osgiVersion}, | |
9 | org.slf4j.helpers;version=${parsedVersion.osgiVersion}, | |
10 | org.slf4j.event;version=${parsedVersion.osgiVersion} | |
8 | 11 | Fragment-Host: slf4j.api⏎ |
0 | package org.slf4j; | |
1 | ||
2 | public class LoggerFactoryFriend { | |
3 | static public void reset() { | |
4 | LoggerFactory.reset(); | |
5 | } | |
6 | } |
25 | 25 | |
26 | 26 | import static org.junit.Assert.assertNotNull; |
27 | 27 | |
28 | import java.util.Random; | |
28 | 29 | import java.util.logging.Handler; |
29 | 30 | import java.util.logging.LogRecord; |
30 | 31 | import java.util.logging.Logger; |
35 | 36 | |
36 | 37 | public class JDK14AdapterLoggerNameTest { |
37 | 38 | private MockHandler mockHandler; |
38 | ||
39 | static Random random = new Random(System.currentTimeMillis()); | |
40 | long diff = random.nextInt(10000); | |
41 | String loggerName = "JDK14AdapterLoggerNameTest"+diff; | |
42 | ||
43 | Logger logger = Logger.getLogger(loggerName); | |
44 | ||
39 | 45 | @Before |
40 | 46 | public void setUp() throws Exception { |
41 | Logger logger = Logger.getLogger("TEST"); | |
42 | mockHandler = new MockHandler(); | |
43 | removeHandlers(logger); | |
44 | logger.addHandler(mockHandler); | |
47 | Logger logger = Logger.getLogger(loggerName); | |
48 | addMockHandler(logger); | |
45 | 49 | } |
50 | ||
51 | ||
46 | 52 | |
47 | 53 | @After |
48 | 54 | public void tearDown() throws Exception { |
49 | removeHandlers(Logger.getLogger("TEST")); | |
55 | removeHandlers(Logger.getLogger(loggerName)); | |
50 | 56 | } |
51 | 57 | |
52 | 58 | @Test |
53 | public void testLoggerNameusingJdkLogging() throws Exception { | |
54 | Logger.getLogger("TEST").info("test message"); | |
59 | public void testLoggerNameUsingJdkLogging() throws Exception { | |
60 | logger.info("test message"); | |
55 | 61 | assertCorrectLoggerName(); |
56 | ||
57 | 62 | } |
58 | 63 | |
59 | 64 | @Test |
60 | 65 | public void testLoggerNameUsingSlf4j() throws Exception { |
61 | 66 | JDK14LoggerFactory factory = new JDK14LoggerFactory(); |
62 | org.slf4j.Logger logger = factory.getLogger("TEST"); | |
67 | org.slf4j.Logger logger = factory.getLogger(loggerName); | |
63 | 68 | logger.info("test message"); |
64 | 69 | assertCorrectLoggerName(); |
65 | 70 | } |
66 | 71 | |
72 | private void addMockHandler(Logger logger) { | |
73 | mockHandler = new MockHandler(); | |
74 | removeHandlers(logger); | |
75 | logger.addHandler(mockHandler); | |
76 | } | |
77 | ||
67 | 78 | private void removeHandlers(Logger logger) { |
68 | 79 | logger.setUseParentHandlers(false); |
69 | 80 | Handler[] handlers = logger.getHandlers(); |
0 | /** | |
1 | * Copyright (c) 2004-2016 QOS.ch | |
2 | * All rights reserved. | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining | |
5 | * a copy of this software and associated documentation files (the | |
6 | * "Software"), to deal in the Software without restriction, including | |
7 | * without limitation the rights to use, copy, modify, merge, publish, | |
8 | * distribute, sublicense, and/or sell copies of the Software, and to | |
9 | * permit persons to whom the Software is furnished to do so, subject to | |
10 | * the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be | |
13 | * included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 | * | |
23 | */ | |
24 | package org.slf4j.impl; | |
25 | ||
26 | import static org.junit.Assert.assertEquals; | |
27 | import static org.junit.Assert.fail; | |
28 | ||
29 | import java.util.List; | |
30 | import java.util.Random; | |
31 | import java.util.concurrent.BrokenBarrierException; | |
32 | import java.util.concurrent.CyclicBarrier; | |
33 | import java.util.concurrent.atomic.AtomicLong; | |
34 | import java.util.logging.Handler; | |
35 | import java.util.logging.LogRecord; | |
36 | ||
37 | import org.junit.After; | |
38 | import org.junit.Before; | |
39 | import org.junit.Test; | |
40 | import org.slf4j.Logger; | |
41 | import org.slf4j.LoggerFactory; | |
42 | ||
43 | public class MultithreadedInitializationTest { | |
44 | ||
45 | final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2; | |
46 | ||
47 | private static AtomicLong EVENT_COUNT = new AtomicLong(0); | |
48 | ||
49 | final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1); | |
50 | ||
51 | int diff = new Random().nextInt(10000); | |
52 | String packagePrefix = "org.slf4j.impl.MultithreadedInitializationTest" + diff; | |
53 | ||
54 | java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(packagePrefix); | |
55 | ||
56 | @Before | |
57 | public void addRecordingHandler() { | |
58 | julLogger.addHandler(new RecordingHandler()); | |
59 | } | |
60 | ||
61 | @After | |
62 | public void tearDown() throws Exception { | |
63 | Handler[] handlers = julLogger.getHandlers(); | |
64 | for (int i = 0; i < handlers.length; i++) { | |
65 | if (handlers[i] instanceof RecordingHandler) { | |
66 | julLogger.removeHandler(handlers[i]); | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
71 | @Test | |
72 | public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException { | |
73 | System.out.println("THREAD_COUNT=" + THREAD_COUNT); | |
74 | LoggerAccessingThread[] accessors = harness(); | |
75 | ||
76 | for (int i = 0; i < accessors.length; i++) { | |
77 | LoggerAccessingThread accessor = accessors[i]; | |
78 | EVENT_COUNT.getAndIncrement(); | |
79 | if (accessor.logger == null) { | |
80 | fail("logger for LoggerAccessingThread " + i + " is not set"); | |
81 | } | |
82 | accessor.logger.info("post harness"); | |
83 | } | |
84 | ||
85 | Logger logger = LoggerFactory.getLogger(packagePrefix + ".test"); | |
86 | logger.info("hello"); | |
87 | EVENT_COUNT.getAndIncrement(); | |
88 | ||
89 | List<LogRecord> records = getRecordedEvents(); | |
90 | assertEquals(EVENT_COUNT.get(), records.size()); | |
91 | } | |
92 | ||
93 | private List<LogRecord> getRecordedEvents() { | |
94 | RecordingHandler ra = findRecordingHandler(); | |
95 | if (ra == null) { | |
96 | fail("failed to fing RecordingHandler"); | |
97 | } | |
98 | return ra.records; | |
99 | } | |
100 | ||
101 | private RecordingHandler findRecordingHandler() { | |
102 | Handler[] handlers = julLogger.getHandlers(); | |
103 | for (Handler h : handlers) { | |
104 | if (h instanceof RecordingHandler) | |
105 | return (RecordingHandler) h; | |
106 | } | |
107 | return null; | |
108 | } | |
109 | ||
110 | private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException { | |
111 | LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT]; | |
112 | final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1); | |
113 | for (int i = 0; i < THREAD_COUNT; i++) { | |
114 | threads[i] = new LoggerAccessingThread(barrier, i); | |
115 | threads[i].start(); | |
116 | } | |
117 | ||
118 | // trigger barrier | |
119 | barrier.await(); | |
120 | ||
121 | for (int i = 0; i < THREAD_COUNT; i++) { | |
122 | threads[i].join(); | |
123 | } | |
124 | ||
125 | return threads; | |
126 | } | |
127 | ||
128 | class LoggerAccessingThread extends Thread { | |
129 | final CyclicBarrier barrier; | |
130 | volatile Logger logger; | |
131 | final int count; | |
132 | ||
133 | LoggerAccessingThread(CyclicBarrier barrier, int count) { | |
134 | this.barrier = barrier; | |
135 | this.count = count; | |
136 | } | |
137 | ||
138 | public void run() { | |
139 | try { | |
140 | barrier.await(); | |
141 | } catch (Exception e) { | |
142 | e.printStackTrace(); | |
143 | } | |
144 | for (int i = 0; i < 64; i++) { | |
145 | logger = LoggerFactory.getLogger(packagePrefix + ".LoggerAccessingThread" + count + "-" + i); | |
146 | logger.info("in run method"); | |
147 | EVENT_COUNT.getAndIncrement(); | |
148 | } | |
149 | } | |
150 | }; | |
151 | ||
152 | } |
23 | 23 | */ |
24 | 24 | package org.slf4j.impl; |
25 | 25 | |
26 | import org.junit.Ignore; | |
26 | 27 | import org.junit.Test; |
27 | 28 | import org.slf4j.Logger; |
28 | 29 | import org.slf4j.LoggerFactory; |
29 | 30 | import org.slf4j.helpers.BogoPerf; |
30 | 31 | |
32 | @Ignore | |
31 | 33 | public class PerfTest { |
32 | 34 | |
33 | 35 | static long REFERENCE_BIPS = 9000; |
34 | 36 | |
35 | 37 | @Test |
36 | public void testBug72() { | |
38 | public void issue63() { | |
37 | 39 | |
38 | 40 | int LEN = 1000 * 1000 * 10; |
39 | 41 | debugLoop(LEN); // warm up |
0 | package org.slf4j.impl; | |
1 | ||
2 | import java.util.ArrayList; | |
3 | import java.util.Collections; | |
4 | import java.util.List; | |
5 | import java.util.logging.Handler; | |
6 | import java.util.logging.LogRecord; | |
7 | ||
8 | public class RecordingHandler extends Handler { | |
9 | ||
10 | List<LogRecord> records = Collections.synchronizedList(new ArrayList<LogRecord>()); | |
11 | ||
12 | @Override | |
13 | public void publish(LogRecord record) { | |
14 | records.add(record); | |
15 | } | |
16 | ||
17 | @Override | |
18 | public void flush() { | |
19 | } | |
20 | ||
21 | @Override | |
22 | public void close() throws SecurityException { | |
23 | } | |
24 | ||
25 | } |
41 | 41 | * See http://jira.qos.ch/browse/SLF4J-252 |
42 | 42 | * @author Thorbjorn Ravn Andersen |
43 | 43 | */ |
44 | public class LoggerSerializationTest { | |
44 | public class LoggerSerializationTest { | |
45 | 45 | |
46 | 46 | static class LoggerHolder implements Serializable { |
47 | 47 | private static final long serialVersionUID = 1L; |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-log4j12</artifactId> |
23 | 23 | */ |
24 | 24 | package org.slf4j.impl; |
25 | 25 | |
26 | import static org.slf4j.event.EventConstants.NA_SUBST; | |
27 | ||
26 | 28 | import java.io.Serializable; |
27 | 29 | |
28 | 30 | import org.apache.log4j.Level; |
31 | import org.apache.log4j.spi.LocationInfo; | |
32 | import org.apache.log4j.spi.ThrowableInformation; | |
29 | 33 | import org.slf4j.Logger; |
30 | 34 | import org.slf4j.Marker; |
35 | import org.slf4j.event.LoggingEvent; | |
31 | 36 | import org.slf4j.helpers.FormattingTuple; |
32 | 37 | import org.slf4j.helpers.MarkerIgnoringBase; |
33 | 38 | import org.slf4j.helpers.MessageFormatter; |
571 | 576 | } |
572 | 577 | |
573 | 578 | public void log(Marker marker, String callerFQCN, int level, String msg, Object[] argArray, Throwable t) { |
579 | Level log4jLevel = toLog4jLevel(level); | |
580 | logger.log(callerFQCN, log4jLevel, msg, t); | |
581 | } | |
582 | ||
583 | private Level toLog4jLevel(int level) { | |
574 | 584 | Level log4jLevel; |
575 | 585 | switch (level) { |
576 | 586 | case LocationAwareLogger.TRACE_INT: |
591 | 601 | default: |
592 | 602 | throw new IllegalStateException("Level number " + level + " is not recognized."); |
593 | 603 | } |
594 | logger.log(callerFQCN, log4jLevel, msg, t); | |
604 | return log4jLevel; | |
605 | } | |
606 | ||
607 | public void log(LoggingEvent event) { | |
608 | Level log4jLevel = toLog4jLevel(event.getLevel().toInt()); | |
609 | if (!logger.isEnabledFor(log4jLevel)) | |
610 | return; | |
611 | ||
612 | org.apache.log4j.spi.LoggingEvent log4jevent = toLog4jEvent(event, log4jLevel); | |
613 | logger.callAppenders(log4jevent); | |
614 | ||
615 | } | |
616 | ||
617 | private org.apache.log4j.spi.LoggingEvent toLog4jEvent(LoggingEvent event, Level log4jLevel) { | |
618 | ||
619 | FormattingTuple ft = MessageFormatter.format(event.getMessage(), event.getArgumentArray(), event.getThrowable()); | |
620 | ||
621 | LocationInfo locationInfo = new LocationInfo(NA_SUBST, NA_SUBST, NA_SUBST, "0"); | |
622 | ||
623 | ThrowableInformation ti = null; | |
624 | Throwable t = ft.getThrowable(); | |
625 | if (t != null) | |
626 | ti = new ThrowableInformation(t); | |
627 | ||
628 | org.apache.log4j.spi.LoggingEvent log4jEvent = new org.apache.log4j.spi.LoggingEvent(FQCN, logger, event.getTimeStamp(), log4jLevel, ft.getMessage(), | |
629 | event.getThreadName(), ti, null, locationInfo, null); | |
630 | ||
631 | return log4jEvent; | |
595 | 632 | } |
596 | 633 | |
597 | 634 | } |
40 | 40 | public class Log4jLoggerFactory implements ILoggerFactory { |
41 | 41 | |
42 | 42 | private static final String LOG4J_DELEGATION_LOOP_URL = "http://www.slf4j.org/codes.html#log4jDelegationLoop"; |
43 | ||
43 | ||
44 | 44 | // check for delegation loops |
45 | 45 | static { |
46 | 46 | try { |
61 | 61 | |
62 | 62 | public Log4jLoggerFactory() { |
63 | 63 | loggerMap = new ConcurrentHashMap<String, Logger>(); |
64 | // force log4j to initialize | |
65 | org.apache.log4j.LogManager.getRootLogger(); | |
64 | 66 | } |
65 | 67 | |
66 | 68 | /* |
39 | 39 | |
40 | 40 | private StaticMDCBinder() { |
41 | 41 | } |
42 | ||
42 | ||
43 | 43 | /** |
44 | 44 | * Return the singleton of this class. |
45 | 45 | * |
49 | 49 | public static final StaticMDCBinder getSingleton() { |
50 | 50 | return SINGLETON; |
51 | 51 | } |
52 | ||
52 | ||
53 | 53 | /** |
54 | 54 | * Currently this method always returns an instance of |
55 | 55 | * {@link StaticMDCBinder}. |
56 | 56 | public static StaticMarkerBinder getSingleton() { |
57 | 57 | return SINGLETON; |
58 | 58 | } |
59 | ||
59 | ||
60 | 60 | /** |
61 | 61 | * Currently this method always returns an instance of |
62 | 62 | * {@link BasicMarkerFactory}. |
4 | 4 | Bundle-Vendor: SLF4J.ORG |
5 | 5 | Bundle-RequiredExecutionEnvironment: J2SE-1.5 |
6 | 6 | Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion} |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, org.slf4j.spi;version=${parsedVersion.osgiVersion}, org.slf4j.helpers;version=${parsedVersion.osgiVersion}, org.apache.log4j | |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, | |
8 | org.slf4j.spi;version=${parsedVersion.osgiVersion}, | |
9 | org.slf4j.helpers;version=${parsedVersion.osgiVersion}, | |
10 | org.slf4j.event;version=${parsedVersion.osgiVersion}, | |
11 | org.apache.log4j | |
8 | 12 | Fragment-Host: slf4j.api⏎ |
47 | 47 | ListAppender listAppender = new ListAppender(); |
48 | 48 | org.apache.log4j.Logger root; |
49 | 49 | |
50 | ||
51 | 50 | @Before |
52 | 51 | public void setUp() throws Exception { |
53 | 52 | root = org.apache.log4j.Logger.getRootLogger(); |
58 | 57 | public void tearDown() throws Exception { |
59 | 58 | root.getLoggerRepository().resetConfiguration(); |
60 | 59 | } |
61 | ||
60 | ||
62 | 61 | @Test |
63 | 62 | public void test1() { |
64 | 63 | Logger logger = LoggerFactory.getLogger("test1"); |
118 | 117 | String[] parameters = null; |
119 | 118 | String msg = "hello {}"; |
120 | 119 | |
121 | logger.debug(msg, (Object[]) parameters); | |
122 | ||
120 | logger.debug(msg, (Object[]) parameters); | |
121 | ||
123 | 122 | assertEquals(1, listAppender.list.size()); |
124 | 123 | LoggingEvent e = (LoggingEvent) listAppender.list.get(0); |
125 | 124 | assertEquals(msg, e.getMessage()); |
0 | /** | |
1 | * Copyright (c) 2004-2011 QOS.ch | |
2 | * All rights reserved. | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining | |
5 | * a copy of this software and associated documentation files (the | |
6 | * "Software"), to deal in the Software without restriction, including | |
7 | * without limitation the rights to use, copy, modify, merge, publish, | |
8 | * distribute, sublicense, and/or sell copies of the Software, and to | |
9 | * permit persons to whom the Software is furnished to do so, subject to | |
10 | * the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be | |
13 | * included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
19 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
20 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
21 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 | * | |
23 | */ | |
24 | package org.slf4j.impl; | |
25 | ||
26 | import static org.junit.Assert.assertEquals; | |
27 | ||
28 | import java.util.List; | |
29 | import java.util.Random; | |
30 | import java.util.concurrent.BrokenBarrierException; | |
31 | import java.util.concurrent.CyclicBarrier; | |
32 | import java.util.concurrent.atomic.AtomicLong; | |
33 | ||
34 | import org.apache.log4j.LogManager; | |
35 | import org.apache.log4j.spi.LoggingEvent; | |
36 | import org.junit.After; | |
37 | import org.junit.Test; | |
38 | import org.slf4j.Logger; | |
39 | import org.slf4j.LoggerFactory; | |
40 | ||
41 | public class MultithreadedInitializationTest { | |
42 | ||
43 | // value of LogManager.DEFAULT_CONFIGURATION_KEY; | |
44 | static String CONFIG_FILE_KEY = "log4j.configuration"; | |
45 | ||
46 | final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2; | |
47 | ||
48 | private static AtomicLong EVENT_COUNT = new AtomicLong(0); | |
49 | ||
50 | final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1); | |
51 | ||
52 | int diff = new Random().nextInt(10000); | |
53 | String loggerName = "org.slf4j.impl.RecursiveInitializationTest"; | |
54 | ||
55 | @After | |
56 | public void tearDown() throws Exception { | |
57 | System.clearProperty(CONFIG_FILE_KEY); | |
58 | } | |
59 | ||
60 | @Test | |
61 | public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException { | |
62 | System.out.println("THREAD_COUNT=" + THREAD_COUNT); | |
63 | System.setProperty(CONFIG_FILE_KEY, "recursiveInitWithActivationDelay.properties"); | |
64 | LoggerAccessingThread[] accessors = harness(); | |
65 | ||
66 | for (LoggerAccessingThread accessor : accessors) { | |
67 | EVENT_COUNT.getAndIncrement(); | |
68 | accessor.logger.info("post harness"); | |
69 | } | |
70 | ||
71 | Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff); | |
72 | logger.info("hello"); | |
73 | EVENT_COUNT.getAndIncrement(); | |
74 | ||
75 | List<LoggingEvent> events = getRecordedEvents(); | |
76 | // 3 evetns generated by RecursiveAppender | |
77 | assertEquals(EVENT_COUNT.get() + 3, events.size()); | |
78 | } | |
79 | ||
80 | private List<LoggingEvent> getRecordedEvents() { | |
81 | org.apache.log4j.Logger root = LogManager.getRootLogger(); | |
82 | ||
83 | RecursiveAppender ra = (RecursiveAppender) root.getAppender("RECURSIVE"); | |
84 | return ra.events; | |
85 | } | |
86 | ||
87 | private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException { | |
88 | LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT]; | |
89 | final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1); | |
90 | for (int i = 0; i < THREAD_COUNT; i++) { | |
91 | threads[i] = new LoggerAccessingThread(barrier, i); | |
92 | threads[i].start(); | |
93 | } | |
94 | ||
95 | barrier.await(); | |
96 | for (int i = 0; i < THREAD_COUNT; i++) { | |
97 | threads[i].join(); | |
98 | } | |
99 | return threads; | |
100 | } | |
101 | ||
102 | static class LoggerAccessingThread extends Thread { | |
103 | final CyclicBarrier barrier; | |
104 | Logger logger; | |
105 | int count; | |
106 | ||
107 | LoggerAccessingThread(CyclicBarrier barrier, int count) { | |
108 | this.barrier = barrier; | |
109 | this.count = count; | |
110 | } | |
111 | ||
112 | public void run() { | |
113 | try { | |
114 | barrier.await(); | |
115 | } catch (Exception e) { | |
116 | e.printStackTrace(); | |
117 | } | |
118 | for (int i = 0; i < 64; i++) { | |
119 | logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + count + "-" + i); | |
120 | logger.info("in run method"); | |
121 | EVENT_COUNT.getAndIncrement(); | |
122 | } | |
123 | } | |
124 | }; | |
125 | ||
126 | } |
23 | 23 | */ |
24 | 24 | package org.slf4j.impl; |
25 | 25 | |
26 | import java.util.ArrayList; | |
27 | import java.util.List; | |
26 | 28 | import java.util.Random; |
27 | 29 | |
28 | 30 | import org.apache.log4j.AppenderSkeleton; |
33 | 35 | public class RecursiveAppender extends AppenderSkeleton { |
34 | 36 | |
35 | 37 | int diff = new Random().nextInt(); |
38 | int activationDelay = 0; | |
39 | String loggerName = "org.slf4j.impl.RecursiveAppender" + diff; | |
40 | ||
41 | List<LoggingEvent> events = new ArrayList<LoggingEvent>(); | |
36 | 42 | |
37 | 43 | public RecursiveAppender() { |
38 | System.out.println("in RecursiveAppender constructor"); | |
39 | Logger logger = LoggerFactory.getLogger("RecursiveAppender" + diff); | |
40 | System.out.println("logger class=" + logger.getClass().getName()); | |
44 | System.out.println("entering RecursiveAppender constructor"); | |
45 | Logger logger = LoggerFactory.getLogger(loggerName); | |
41 | 46 | logger.info("Calling a logger in the constructor"); |
47 | System.out.println("exiting RecursiveAppender constructor"); | |
42 | 48 | } |
43 | 49 | |
44 | protected void append(LoggingEvent arg0) { | |
50 | protected void append(LoggingEvent e) { | |
51 | events.add(e); | |
45 | 52 | } |
46 | 53 | |
47 | 54 | public void close() { |
50 | 57 | public boolean requiresLayout() { |
51 | 58 | return false; |
52 | 59 | } |
60 | ||
61 | @Override | |
62 | public void activateOptions() { | |
63 | System.out.println("entering RecursiveAppender.activateOptions"); | |
64 | if (activationDelay > 0) { | |
65 | Logger logger = LoggerFactory.getLogger(loggerName); | |
66 | logger.info("About to wait {} millis", activationDelay); | |
67 | try { | |
68 | Thread.sleep(activationDelay); | |
69 | } catch (InterruptedException e) { | |
70 | e.printStackTrace(); | |
71 | } | |
72 | logger.info("Done waiting {} millis", activationDelay); | |
73 | } | |
74 | super.activateOptions(); | |
75 | ||
76 | System.out.println("exiting RecursiveAppender.activateOptions"); | |
77 | } | |
78 | ||
79 | public int getActivationDelay() { | |
80 | return activationDelay; | |
81 | } | |
82 | ||
83 | public void setActivationDelay(int activationDelay) { | |
84 | this.activationDelay = activationDelay; | |
85 | } | |
86 | ||
53 | 87 | } |
26 | 26 | import java.util.Random; |
27 | 27 | |
28 | 28 | import org.junit.After; |
29 | import org.junit.Before; | |
30 | 29 | import org.junit.Test; |
31 | 30 | import org.slf4j.Logger; |
32 | 31 | import org.slf4j.LoggerFactory; |
32 | ||
33 | 33 | public class RecursiveInitializationTest { |
34 | 34 | |
35 | 35 | // value of LogManager.DEFAULT_CONFIGURATION_KEY; |
36 | 36 | static String CONFIG_FILE_KEY = "log4j.configuration"; |
37 | 37 | |
38 | 38 | int diff = new Random().nextInt(10000); |
39 | ||
40 | @Before | |
41 | public void setUp() throws Exception { | |
42 | System.setProperty(CONFIG_FILE_KEY, "recursiveInit.properties"); | |
43 | } | |
39 | String loggerName = "org.slf4j.impl.RecursiveInitializationTest"; | |
44 | 40 | |
45 | 41 | @After |
46 | 42 | public void tearDown() throws Exception { |
48 | 44 | } |
49 | 45 | |
50 | 46 | @Test |
51 | public void testLog4j() { | |
52 | Logger logger = LoggerFactory.getLogger("x" + diff); | |
53 | System.out.println("logger class=" + logger.getClass().getName()); | |
47 | public void loggingDuringInitialization() { | |
48 | System.setProperty(CONFIG_FILE_KEY, "recursiveInit.properties"); | |
49 | Logger logger = LoggerFactory.getLogger(loggerName + ".loggingDuringInitialization-" + diff); | |
54 | 50 | logger.info("hello"); |
55 | 51 | } |
56 | 52 |
0 | log4j.debug=true | |
1 | log4j.rootLogger=DEBUG, CONSOLE, RECURSIVE | |
2 | ||
3 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender | |
4 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout | |
5 | log4j.appender.CONSOLE.layout.ConversionPattern=CON %d [%t] %c - %m%n | |
6 | ||
7 | log4j.appender.RECURSIVE=org.slf4j.impl.RecursiveAppender | |
8 | log4j.appender.RECURSIVE.activationDelay=10⏎ |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-migrator</artifactId> |
45 | 45 | import org.slf4j.migrator.Constant; |
46 | 46 | import org.slf4j.migrator.helper.SpringLayoutHelper; |
47 | 47 | |
48 | /** | |
49 | * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI | |
50 | * Builder, which is free for non-commercial use. If Jigloo is being used | |
51 | * commercially (ie, by a corporation, company or business for any purpose | |
52 | * whatever) then you should purchase a license for each developer using Jigloo. | |
53 | * Please visit www.cloudgarden.com for details. Use of Jigloo implies | |
54 | * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN | |
55 | * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR | |
56 | * ANY CORPORATE OR COMMERCIAL PURPOSE. | |
57 | */ | |
58 | 48 | public class MigratorFrame extends JFrame implements ActionListener { |
59 | 49 | private static final long serialVersionUID = 1L; |
60 | 50 |
30 | 30 | import org.junit.Test; |
31 | 31 | import org.slf4j.migrator.internal.NopProgressListener; |
32 | 32 | import org.slf4j.migrator.line.EmptyRuleSet; |
33 | public class FileConverterTest { | |
34 | 33 | |
34 | public class FileConverterTest { | |
35 | 35 | |
36 | 36 | @Test |
37 | 37 | @Ignore |
29 | 29 | import org.junit.Test; |
30 | 30 | import org.slf4j.migrator.internal.NopProgressListener; |
31 | 31 | |
32 | public class ProjectConverterTest { | |
32 | public class ProjectConverterTest { | |
33 | 33 | |
34 | 34 | public void test() { |
35 | 35 | } |
27 | 27 | import static org.junit.Assert.assertTrue; |
28 | 28 | |
29 | 29 | import org.junit.Test; |
30 | ||
30 | 31 | public class AbbreviatorTest { |
31 | 32 | |
32 | 33 | static final char FS = '/'; |
26 | 26 | import static org.junit.Assert.assertEquals; |
27 | 27 | |
28 | 28 | import org.junit.Test; |
29 | ||
29 | 30 | public class JCLRuleSetTest { |
30 | 31 | |
31 | 32 | LineConverter jclConverter = new LineConverter(new JCLRuleSet()); |
37 | 38 | // Log import replacement |
38 | 39 | assertEquals("import org.slf4j.Logger;", jclConverter.getOneLineReplacement("import org.apache.commons.logging.Log;")); |
39 | 40 | } |
40 | ||
41 | 41 | |
42 | 42 | @Test |
43 | 43 | public void testLogFactoryGetLogReplacement() { |
92 | 92 | jclConverter.getOneLineReplacement("// myLog = LogFactory.getFactory().getInstance(MyClass.class);//logger instanciation")); |
93 | 93 | } |
94 | 94 | |
95 | ||
96 | 95 | @Test |
97 | 96 | public void testLogDeclarationReplacement() { |
98 | 97 |
26 | 26 | import static org.junit.Assert.assertEquals; |
27 | 27 | |
28 | 28 | import org.junit.Test; |
29 | public class NoConversionTest { | |
29 | ||
30 | public class NoConversionTest { | |
30 | 31 | |
31 | 32 | /** |
32 | 33 | * This test shows that performing JCL to SLF4J conversion has no impact on |
6 | 6 | <parent> |
7 | 7 | <groupId>org.slf4j</groupId> |
8 | 8 | <artifactId>slf4j-parent</artifactId> |
9 | <version>1.7.14</version> | |
9 | <version>1.7.19</version> | |
10 | 10 | </parent> |
11 | 11 | |
12 | 12 | <artifactId>slf4j-nop</artifactId> |
50 | 50 | public static final StaticMDCBinder getSingleton() { |
51 | 51 | return SINGLETON; |
52 | 52 | } |
53 | ||
53 | ||
54 | 54 | /** |
55 | 55 | * Currently this method always returns an instance of |
56 | 56 | * {@link StaticMDCBinder}. |
56 | 56 | public static StaticMarkerBinder getSingleton() { |
57 | 57 | return SINGLETON; |
58 | 58 | } |
59 | ||
59 | ||
60 | 60 | /** |
61 | 61 | * Currently this method always returns an instance of |
62 | 62 | * {@link BasicMarkerFactory}. |
4 | 4 | Bundle-Vendor: SLF4J.ORG |
5 | 5 | Bundle-RequiredExecutionEnvironment: J2SE-1.5 |
6 | 6 | Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion} |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, org.slf4j.spi;version=${parsedVersion.osgiVersion}, org.slf4j.helpers;version=${parsedVersion.osgiVersion} | |
7 | Import-Package: org.slf4j;version=${parsedVersion.osgiVersion}, | |
8 | org.slf4j.spi;version=${parsedVersion.osgiVersion}, | |
9 | org.slf4j.helpers;version=${parsedVersion.osgiVersion}, | |
10 | org.slf4j.event;version=${parsedVersion.osgiVersion} | |
8 | 11 | Fragment-Host: slf4j.api⏎ |
0 | package org.slf4j; | |
1 | ||
2 | public class LoggerFactoryFriend { | |
3 | static public void reset() { | |
4 | LoggerFactory.reset(); | |
5 | } | |
6 | } |
0 | /** | |
1 | * Copyright (c) 2004-2016 QOS.ch | |
2 | * All rights reserved. | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining | |
5 | * a copy of this software and associated documentation files (the | |
6 | * "Software"), to deal in the Software without restriction, including | |
7 | * without limitation the rights to use, copy, modify, merge, publish, | |
8 | * distribute, sublicense, and/or sell copies of the Software, and to | |
9 | * permit persons to whom the Software is furnished to do so, subject to | |
10 | * the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be | |
13 | * included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 |
|