├── .gitignore
├── .travis.yml
├── LICENSE.txt
├── checkstyle.xml
├── findbugsfilter.xml
├── functional-tests
├── no-system-jar-on-classpath
│ ├── pom.xml
│ └── src
│ │ └── test
│ │ └── java
│ │ └── uk
│ │ └── org
│ │ └── lidalia
│ │ └── sysoutslf4j
│ │ └── SysoutOverSlf4jWithNoSystemJarOnClasspathTests.java
├── pom.xml
├── sysout-over-slf4j-webapps
│ ├── pom.xml
│ └── webapp-with-jdk14
│ │ ├── pom.xml
│ │ └── src
│ │ └── main
│ │ ├── java
│ │ └── uk
│ │ │ └── org
│ │ │ └── lidalia
│ │ │ └── sysoutslf4j
│ │ │ └── webapps
│ │ │ ├── LoggerServlet.java
│ │ │ ├── LoggingFilter.java
│ │ │ └── PrinterServlet.java
│ │ └── webapp
│ │ ├── META-INF
│ │ └── MANIFEST.MF
│ │ └── WEB-INF
│ │ └── web.xml
└── tests
│ └── pom.xml
├── pmd-rules.xml
├── pom.xml
├── src
└── site
│ ├── apt
│ ├── index.apt.vm
│ └── quickstart.apt.vm
│ ├── fml
│ └── faq.fml.vm
│ └── site.xml
├── sysout-over-slf4j-context
├── pom.xml
└── src
│ ├── main
│ ├── .gitignore
│ └── java
│ │ └── uk
│ │ └── org
│ │ └── lidalia
│ │ └── sysoutslf4j
│ │ └── context
│ │ ├── CallOrigin.java
│ │ ├── LoggingMessages.java
│ │ ├── LoggingOutputStream.java
│ │ ├── LoggingSystemRegister.java
│ │ ├── ReferenceHolder.java
│ │ ├── ServletContextListener.java
│ │ ├── StringUtils.java
│ │ ├── SysOutOverSLF4J.java
│ │ ├── SysOutOverSLF4JSystemJarNotPresentException.java
│ │ ├── exceptionhandlers
│ │ ├── ExceptionHandlingStrategy.java
│ │ ├── ExceptionHandlingStrategyFactory.java
│ │ └── LogPerLineExceptionHandlingStrategyFactory.java
│ │ ├── jul
│ │ └── ConsoleHandler.java
│ │ ├── log4j
│ │ └── ConsoleAppender.java
│ │ └── logback
│ │ └── ConsoleAppender.java
│ └── test
│ ├── .gitignore
│ └── java
│ └── uk
│ └── org
│ └── lidalia
│ ├── sysoutslf4j
│ ├── SysOutOverSLF4JTestCase.java
│ ├── context
│ │ ├── CallOriginTests.java
│ │ ├── LoggingOutputStreamTests.java
│ │ ├── LoggingSystemRegisterTests.java
│ │ ├── ReferenceHolderTests.java
│ │ ├── ServletContextListenerTests.java
│ │ ├── StringUtilsTests.java
│ │ ├── SysOutOverSLF4JNotInstantiableTest.java
│ │ ├── SysOutOverSLF4JTests.java
│ │ ├── exceptionhandlers
│ │ │ └── TestLogPerLineExceptionHandlingStrategyFactory.java
│ │ ├── jul
│ │ │ └── ConsoleHandlerTests.java
│ │ ├── log4j
│ │ │ └── ConsoleAppenderTests.java
│ │ └── logback
│ │ │ └── ConsoleAppenderTests.java
│ └── integration
│ │ ├── CrossClassLoaderTestUtils.java
│ │ ├── ISysOutUser.java
│ │ ├── SysOutUser.java
│ │ ├── TestForClassloaderLeaks.java
│ │ ├── TestSysOutOverSLF4JInClassLoader.java
│ │ ├── TestSysOutOverSLF4JThreadSafety.java
│ │ └── TestSysOutOverSlf4J.java
│ └── testutils
│ ├── ClassCreationUtils.java
│ └── SimpleClassloader.java
└── sysout-over-slf4j-system
├── pom.xml
└── src
├── main
└── java
│ └── uk
│ └── org
│ └── lidalia
│ └── sysoutslf4j
│ └── system
│ ├── PerContextPrintStream.java
│ ├── PerContextStore.java
│ ├── PerContextSystemOutput.java
│ └── SystemOutput.java
└── test
└── java
└── uk
└── org
└── lidalia
└── sysoutslf4j
└── system
├── PerContextPrintStreamTests.java
├── PerContextStoreMemoryManagementTests.java
├── PerContextStoreTests.java
├── PerContextSystemOutputTests.java
├── SysOutOverSLF4JTestCase.java
└── SystemOutputTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | .settings
2 | target
3 | .classpath
4 | .project
5 | *.iml
6 | *.ipr
7 | *.iws
8 | *~
9 | integration/bundle/
10 | integration/felix-cache
11 | integration/felix-cache//.settings
12 | .pmd
13 | bin
14 | .DS_Store
15 | /reports
16 | release.properties
17 | .idea
18 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - openjdk7
4 | - oraclejdk7
5 | - oraclejdk8
6 | install: true
7 | script: mvn verify
8 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009-2012 Robert Elliot
3 | * All rights reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
--------------------------------------------------------------------------------
/checkstyle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/findbugsfilter.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/functional-tests/no-system-jar-on-classpath/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | uk.org.lidalia.sysout-over-slf4j
5 | functional-tests
6 | 2.0.0-SNAPSHOT
7 |
8 | no-system-jar-on-classpath
9 | No System Jar on Classpath Tests
10 | Tests demonstrating behaviour when sysout-over-slf4j-context is on the classpath
11 | but sysout-over-slf4j-system is not.
12 |
13 |
14 | uk.org.lidalia
15 | lidalia-test-dependencies
16 | pom
17 |
18 |
19 | ${project.groupId}
20 | sysout-over-slf4j-context
21 | ${project.version}
22 | test
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/functional-tests/no-system-jar-on-classpath/src/test/java/uk/org/lidalia/sysoutslf4j/SysoutOverSlf4jWithNoSystemJarOnClasspathTests.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009-2012 Robert Elliot
3 | * All rights reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package uk.org.lidalia.sysoutslf4j;
26 |
27 | import org.junit.Test;
28 |
29 | import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J;
30 | import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4JSystemJarNotPresentException;
31 |
32 | import static org.junit.Assert.assertEquals;
33 | import static uk.org.lidalia.test.ShouldThrow.shouldThrow;
34 |
35 | public class SysoutOverSlf4jWithNoSystemJarOnClasspathTests {
36 |
37 | @Test
38 | public void sendSystemOutAndErrToSLF4JWithoutSystemJarOnClasspath() {
39 | SysOutOverSLF4JSystemJarNotPresentException exception = shouldThrow(SysOutOverSLF4JSystemJarNotPresentException.class, new Runnable() {
40 | @Override
41 | public void run() {
42 | SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
43 | }
44 | });
45 |
46 | assertEquals(SysOutOverSLF4JSystemJarNotPresentException.MESSAGE, exception.getMessage());
47 | }
48 |
49 | @Test
50 | public void stopSendingSystemOutAndErrToSLF4JWithoutSystemJarOnClasspath() {
51 | SysOutOverSLF4JSystemJarNotPresentException exception = shouldThrow(SysOutOverSLF4JSystemJarNotPresentException.class, new Runnable() {
52 | @Override
53 | public void run() {
54 | SysOutOverSLF4J.stopSendingSystemOutAndErrToSLF4J();
55 | }
56 | });
57 |
58 | assertEquals(SysOutOverSLF4JSystemJarNotPresentException.MESSAGE, exception.getMessage());
59 | }
60 |
61 | @Test
62 | public void restoreOriginalSystemOutputsWithoutSystemJarOnClasspath() {
63 | SysOutOverSLF4JSystemJarNotPresentException exception = shouldThrow(SysOutOverSLF4JSystemJarNotPresentException.class, new Runnable() {
64 | @Override
65 | public void run() {
66 | SysOutOverSLF4J.restoreOriginalSystemOutputs();
67 | }
68 | });
69 |
70 | assertEquals(SysOutOverSLF4JSystemJarNotPresentException.MESSAGE, exception.getMessage());
71 | }
72 |
73 | @Test
74 | public void systemOutputsAreSLF4JPrintStreamsWithoutSystemJarOnClasspath() {
75 | SysOutOverSLF4JSystemJarNotPresentException exception = shouldThrow(SysOutOverSLF4JSystemJarNotPresentException.class, new Runnable() {
76 | @Override
77 | public void run() {
78 | SysOutOverSLF4J.systemOutputsAreSLF4JPrintStreams();
79 | }
80 | });
81 |
82 | assertEquals(SysOutOverSLF4JSystemJarNotPresentException.MESSAGE, exception.getMessage());
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/functional-tests/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | uk.org.lidalia.sysout-over-slf4j
5 | sysout-over-slf4j-parent
6 | 2.0.0-SNAPSHOT
7 |
8 | functional-tests
9 | pom
10 | Functional Tests
11 | Functional tests of sysout-over-slf4j in a variety of circumstances.
12 |
13 | no-system-jar-on-classpath
14 | sysout-over-slf4j-webapps
15 | tests
16 |
17 |
18 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 |
6 | uk.org.lidalia.sysout-over-slf4j
7 | functional-tests
8 | 2.0.0-SNAPSHOT
9 |
10 | sysout-over-slf4j-webapps
11 | pom
12 | Test Web Applications
13 | A collection of web applications using different logging implementations for
14 | functional testing of sysout-over-slf4j's behaviour when several reloadable contexts exist in a JVM.
15 |
16 | webapp-with-jdk14
17 |
18 |
19 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | uk.org.lidalia.sysout-over-slf4j
5 | sysout-over-slf4j-webapps
6 | 2.0.0-SNAPSHOT
7 |
8 | webapp-with-jdk14
9 | war
10 | Webapp using java.util.logging
11 | A webapp configured to use java.util.logging as its logging implementation.
12 |
13 |
14 | org.slf4j
15 | slf4j-api
16 |
17 |
18 | org.slf4j
19 | slf4j-jdk14
20 | ${slf4j.version}
21 | runtime
22 |
23 |
24 | ${project.groupId}
25 | sysout-over-slf4j-context
26 | ${project.version}
27 | runtime
28 |
29 |
30 | ${project.groupId}
31 | sysout-over-slf4j-system
32 | ${project.version}
33 | provided
34 |
35 |
36 | javax
37 | javaee-web-api
38 |
39 |
40 | com.google.guava
41 | guava
42 |
43 |
44 | uk.org.lidalia
45 | lidalia-lang
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/src/main/java/uk/org/lidalia/sysoutslf4j/webapps/LoggerServlet.java:
--------------------------------------------------------------------------------
1 | package uk.org.lidalia.sysoutslf4j.webapps;
2 |
3 | import java.io.IOException;
4 |
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServlet;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 |
10 | import org.slf4j.Logger;
11 | import org.slf4j.LoggerFactory;
12 |
13 | import static com.google.common.base.Optional.fromNullable;
14 |
15 | public class LoggerServlet extends HttpServlet {
16 |
17 | private static final long serialVersionUID = 1;
18 |
19 | @Override
20 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21 | String loggerName = fromNullable(request.getParameter("logger")).or(Logger.ROOT_LOGGER_NAME);
22 | boolean useNative = Boolean.valueOf(request.getParameter("native"));
23 | String message = request.getParameter("message");
24 | if (useNative) {
25 | java.util.logging.Logger.getLogger(loggerName).info(message);
26 | } else {
27 | LoggerFactory.getLogger(loggerName).info(message);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/src/main/java/uk/org/lidalia/sysoutslf4j/webapps/LoggingFilter.java:
--------------------------------------------------------------------------------
1 | package uk.org.lidalia.sysoutslf4j.webapps;
2 |
3 | import java.io.IOException;
4 |
5 | import javax.servlet.Filter;
6 | import javax.servlet.FilterChain;
7 | import javax.servlet.FilterConfig;
8 | import javax.servlet.ServletException;
9 | import javax.servlet.ServletRequest;
10 | import javax.servlet.ServletResponse;
11 | import javax.servlet.http.HttpServletRequest;
12 |
13 | import org.slf4j.Logger;
14 | import org.slf4j.LoggerFactory;
15 |
16 | import static uk.org.lidalia.lang.Exceptions.throwUnchecked;
17 |
18 | public class LoggingFilter implements Filter {
19 |
20 | private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class);
21 |
22 | @Override
23 | public void init(FilterConfig filterConfig) throws ServletException {
24 | // Nothing to do
25 | }
26 |
27 | @Override
28 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
29 | try {
30 | filterChain.doFilter(servletRequest, servletResponse);
31 | } catch (Throwable t) {
32 | LOG.error("Uncaught exception handling request " + requestLine((HttpServletRequest) servletRequest), t);
33 | throwUnchecked(t);
34 | }
35 | }
36 |
37 | private String requestLine(HttpServletRequest httpRequest) {
38 | final StringBuffer requestURL = httpRequest.getRequestURL();
39 | final String queryString = httpRequest.getQueryString();
40 | if (queryString != null && queryString.length() > 0) {
41 | requestURL.append('?').append(queryString);
42 | }
43 | return httpRequest.getMethod() + " " + requestURL;
44 | }
45 |
46 | @Override
47 | public void destroy() {
48 | // Nothing to do
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/src/main/java/uk/org/lidalia/sysoutslf4j/webapps/PrinterServlet.java:
--------------------------------------------------------------------------------
1 | package uk.org.lidalia.sysoutslf4j.webapps;
2 |
3 | import java.io.IOException;
4 |
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServlet;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 |
10 | import uk.org.lidalia.sysoutslf4j.system.SystemOutput;
11 |
12 | import static com.google.common.base.Optional.fromNullable;
13 |
14 | public class PrinterServlet extends HttpServlet {
15 |
16 | private static final long serialVersionUID = 1;
17 |
18 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19 | String name = fromNullable(request.getParameter("output")).or("System.out");
20 | SystemOutput output = SystemOutput.findByName(name);
21 | output.get().println(request.getParameter("message"));
22 | }
23 |
24 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/src/main/webapp/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Class-Path:
3 |
4 |
--------------------------------------------------------------------------------
/functional-tests/sysout-over-slf4j-webapps/webapp-with-jdk14/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 | Webapp With JDK14
7 |
8 |
9 | uk.org.lidalia.sysoutslf4j.context.ServletContextListener
10 |
11 |
12 |
13 | logger
14 | uk.org.lidalia.sysoutslf4j.webapps.LoggerServlet
15 |
16 |
17 | logger
18 | /log
19 |
20 |
21 | printer
22 | uk.org.lidalia.sysoutslf4j.webapps.PrinterServlet
23 |
24 |
25 | printer
26 | /print
27 |
28 |
29 |
--------------------------------------------------------------------------------
/functional-tests/tests/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | functional-tests
7 | uk.org.lidalia.sysout-over-slf4j
8 | 2.0.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | tests
13 | pom
14 | Tests
15 |
16 |
17 | ${project.groupId}
18 | webapp-with-jdk14
19 | ${project.version}
20 | war
21 |
22 |
23 | ${project.groupId}
24 | sysout-over-slf4j-system
25 | ${project.version}
26 |
27 |
28 |
29 |
30 |
31 | org.apache.maven.plugins
32 | maven-surefire-plugin
33 |
34 | true
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-failsafe-plugin
40 |
41 |
42 | org.codehaus.cargo
43 | cargo-maven2-plugin
44 |
45 |
46 |
47 | 8081
48 |
49 |
50 |
51 |
52 | ${project.groupId}
53 | webapp-with-jdk14
54 | war
55 |
56 | /webapp-with-jdk14
57 |
58 |
59 |
60 |
61 |
62 |
63 | start-container
64 | pre-integration-test
65 |
66 | start
67 |
68 |
69 |
70 | stop-container
71 | post-integration-test
72 |
73 | stop
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 |
6 |
7 | uk.org.lidalia
8 | parent
9 | 1.3.0
10 |
11 |
12 | uk.org.lidalia.sysout-over-slf4j
13 | sysout-over-slf4j-parent
14 | 2.0.0-SNAPSHOT
15 | pom
16 |
17 | System Out and Err redirected to SLF4J
18 | http://projects.lidalia.org.uk/sysout-over-slf4j/
19 | Library to send raw System.out and System.err output to SLF4J for proper logging control
20 |
21 |
22 |
23 | github
24 | file:///tmp/${project.name}
25 |
26 |
27 |
28 |
29 | sysout-over-slf4j-context
30 | sysout-over-slf4j-system
31 | functional-tests
32 |
33 |
34 |
35 | GitHub Issues
36 | https://github.com/Mahoney/sysout-over-slf4j/issues
37 |
38 |
39 |
40 |
41 | Robert Elliot
42 | rob@lidalia.org.uk
43 |
44 |
45 |
46 |
47 |
48 | MIT X11 License
49 | file://${project.basedir}/LICENSE.txt
50 | repo
51 |
52 |
53 |
54 |
55 | scm:git:git@github.com:Mahoney/sysout-over-slf4j.git
56 | scm:git:git@github.com:Mahoney/sysout-over-slf4j.git
57 | https://github.com/Mahoney/sysout-over-slf4j
58 |
59 |
60 |
61 |
62 | slf4j-announcements list
63 | http://www.qos.ch/mailman/listinfo/announce
64 | http://www.qos.ch/mailman/options/announce
65 | http://www.qos.ch/pipermail/announce/
66 |
67 | http://marc.theaimsgroup.com/?l=slf4j-announce
68 |
69 |
70 |
71 | slf4j-user list
72 | http://www.qos.ch/mailman/listinfo/slf4j-user
73 | http://www.qos.ch/mailman/options/slf4j-user
74 | slf4j-user@qos.ch
75 | http://www.qos.ch/pipermail/slf4j-user/
76 |
77 | http://news.gmane.org/gmane.comp.java.slf4j.user
78 | http://marc.theaimsgroup.com/?l=slf4j-user
79 | http://www.nabble.com/Slf4J---user-f13203.html
80 | http://www.mail-archive.com/user%40slf4j.org/
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 | uk.org.lidalia
89 | lidalia-test-dependencies
90 | 1.1.0
91 | pom
92 | test
93 |
94 |
95 | commons-io
96 | commons-io
97 | ${commons-io.version}
98 |
99 |
100 | ch.qos.logback
101 | logback-core
102 | ${logback.version}
103 |
104 |
105 | uk.org.lidalia
106 | lidalia-lang
107 | 1.0.0
108 |
109 |
110 | uk.org.lidalia
111 | lidalia-slf4j-ext
112 | 1.0.0
113 |
114 |
115 | javax.servlet
116 | servlet-api
117 | 2.3
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/src/site/apt/index.apt.vm:
--------------------------------------------------------------------------------
1 | System Out and Err Redirected to SLF4J
2 |
3 | The ${project.artifactId} module allows a user to redirect all calls to
4 | System.out and System.err to an SLF4J defined logger with the name of
5 | the fully qualified class in which the System.out.println (or similar)
6 | call was made, at configurable levels.
7 |
8 | *What are the intended use cases?
9 |
10 | The ${project.artifactId} module is for cases where your legacy codebase,
11 | or a third party module you use, prints directly to the console and
12 | you would like to get the benefits of a proper logging framework,
13 | with automatic capture of information like timestamp and the ability
14 | to filter which messages you are interested in seeing and control where
15 | they are sent.
16 |
17 | The ${project.artifactId} module is explicitly not intended to encourage
18 | the use of System.out or System.err for logging purposes. There is a
19 | significant performance overhead attached to its use, and as such it
20 | should be considered a stop-gap for your own code until you can alter
21 | it to use SLF4J directly, or a work-around for poorly behaving third
22 | party modules.
23 |
24 | *Getting started
25 |
26 | Follow the {{{quickstart.html}Quick start}} guide.
27 |
--------------------------------------------------------------------------------
/src/site/apt/quickstart.apt.vm:
--------------------------------------------------------------------------------
1 | Quick Start
2 |
3 | *Add ${project.artifactId} to the classpath
4 |
5 | Download the ${project.artifactId}-${project.version}.jar on the {{{http://github.com/Mahoney/sysout-over-slf4j/downloads}Downloads page}}.
6 |
7 | Alternatively, add the following to your maven pom dependencies:
8 |
9 | ----------------------------------------
10 |
11 | ${project.groupId}
12 | ${project.artifactId}
13 | ${project.version}
14 |
15 | ----------------------------------------
16 |
17 | *In a Standalone Application
18 |
19 | Make the following call as early as possible in the startup of your application:
20 |
21 | ----------------------------------------
22 | SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
23 | ----------------------------------------
24 |
25 | *In a Servlet Container
26 |
27 | Add the following to your web.xml:
28 |
29 | ----------------------------------------
30 |
31 | uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4JServletContextListener
32 |
33 | ----------------------------------------
34 |
--------------------------------------------------------------------------------
/src/site/fml/faq.fml.vm:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 | General
10 |
11 |
12 |
13 | Help - I'm getting a StackOverFlowError every time I log or println!
14 |
15 |
16 |
You are probably using an SLF4J implementation that tries to write to the console using one of the print(ln) methods
17 | on PrintStream. These are in turn redirected back to your SLF4J implementation by ${project.artifactId}, resulting in an
18 | infinite loop. To prevent this, add the following call early in the lifecycle of your application:
${project.artifactId} will then recognise all attempts to write to system out or err from your logging system and will
23 | delegate them to the original system out or err PrintStream.
24 |
25 |
Note that you should ONLY register a logging system if you need to - many logging systems (including Logback, Log4J and
26 | JULI) use the write methods on PrintStream, which always delegate directly to the original PrintStream, and so do not have
27 | this problem.
28 |
29 |
30 |
31 |
32 |
33 | What if I want System.out calls to log as debug rather than info?
34 | Or System.err calls to log as warn rather than error?
35 |
36 |
37 |
You can customise the levels at which ${project.artifactId} logs when initially configuring it at application startup.
38 | Simply call:
instead of the no argument version. The first argument represents the level of calls to System.out, and the
41 | second argument represents the level of calls to System.err.
42 |
43 |
44 |
45 |
46 |
47 | Why does ${project.artifactId} log a separate log statement for every element in the stack in a stacktrace?
48 |
49 |
50 |
When Throwable.printStackTrace() is called it iterates over the frames in the Throwable's stack trace and
51 | prints each to the System.err as a separate method call. This leaves no reliable means of reconstituting the
52 | original Throwable, or recognising the last frame of the stack trace. Whilst logging each frame separately is
53 | clearly sub-optimal, it is at present the only known way of reliably retaining all the information from a
54 | printStackTrace call and maintaining its ordering.
55 |
In recognition that this is unsatisfactory ${project.artifactId} allows you to substitute in your own exception
56 | handling mechanism by implementing ExceptionHandlingStrategyFactory and passing an instance of your implementation
57 | in when configuring ${project.artifactId} at startup:
58 | ExceptionHandlingStrategyFactory factory = new CustomExceptionHandlingStrategyFactory();
59 | SysOutOverSLF4J.sendSystemOutAndErrToSLF4J(factory);
60 |
61 |
62 |
63 |
64 |
65 | Don't most logging systems print to the console? Won't that mean infinite recursion?
66 |
67 |
68 |
As mentioned above, the most common SLF4J implementations use the write methods on PrintStream, which always delegate
69 | directly to the original PrintStream, and so are not affected by use of sysout-over-slf4j.
70 |
71 |
72 |
73 |
74 | What are the performance implications of using sysout-over-slf4j?
75 |
76 |
77 |
The overhead for Log4J, JULI and Logback when printing to the
78 | console should be minimal, because SLF4J simply proxies calls to
79 | the write methods through to the original PrintStreams without
80 | doing any work.
81 |
82 |
The overhead for some other SLF4J implementation that does not use the PrintStream write methods, and so needs to be
83 | registered, will be greater; on every attempt by it to print to the console its fully qualified class name has to be
84 | matched against registered package names in order to determine whether it should be permitted direct access.
85 |
86 |
Finally, the overhead of actual System.out and System.err calls will be much greater, due to the expense of generating
87 | the thread's stacktrace and examining it to determine the origin of the call. It would be much better
88 | if all logging were done via SLF4J directly and this module were not necessary.
89 |
90 |
91 |
92 |
93 |
94 | How does it work?
95 |
96 |
97 |
The System.out and System.err PrintStreams are replaced with
98 | new SLF4JPrintStreams. Each time a call to System.out.println (or
99 | similar) is made, the current thread's stacktrace is examined to
100 | determine which class made the call. An SLF4J Logger named after
101 | that class's fully qualified name is retrieved and the message
102 | logged at the configured level on that logger (by default info for
103 | System.out calls and error for System.err calls).
104 |
105 |
Calls to Throwable.printStackTrace() are likewise logged at
106 | the configured level for each System output. By default there
107 | will be a message logged for every line of the stack trace; this
108 | is an unfortunate side effect of not being able to
109 | retrieve the original exception that is being printed reliably.
110 |
111 |
A servlet container may contain multiple web applications.
112 | If it has child first class loading and these applications
113 | package SLF4J in the web-app/lib directory then there will be
114 | multiple SLF4J instances running in the JVM. However, there
115 | is only one System.out and one System.err for the whole JVM.
116 | In order to ensure that the correct SLF4J instance is used
117 | for the correct web application, inside the new PrintStreams
118 | SLF4J instances are mapped against the context
119 | class loader to ensure that the same SLF4J instance used in
120 | "normal" logging is also used when calling System.out.println.
121 |
122 |
In order to prevent class loader leaks when contexts
123 | are reloaded the new PrintStreams are created by a special
124 | class loader so that they do not themselves maintain a reference
125 | to the context class loader. The PrintStreams only maintain a weak
126 | reference to the class loaders and their SLF4J instances, so that
127 | no class loader leak occurs if the class loader is discarded.
128 |
129 |
130 |
131 |
132 |
133 |
--------------------------------------------------------------------------------
/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
21 |
22 |
23 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 |
6 |
7 | uk.org.lidalia.sysout-over-slf4j
8 | sysout-over-slf4j-parent
9 | 2.0.0-SNAPSHOT
10 |
11 | sysout-over-slf4j-context
12 |
13 | Context Jar
14 |
15 | Contains the API for using sysout-over-slf4j and all the classes that can safely be loaded in the context of a classloader
16 | than can be discarded (such as a webapp in a servlet container).
17 |
18 |
19 |
20 |
21 | uk.org.lidalia
22 | lidalia-lang
23 |
24 |
25 | com.google.guava
26 | guava
27 |
28 |
29 | uk.org.lidalia
30 | lidalia-slf4j-ext
31 |
32 |
33 |
34 |
35 | ch.qos.logback
36 | logback-core
37 | compile
38 | true
39 |
40 |
41 | log4j
42 | log4j
43 | 1.2.13
44 | compile
45 | true
46 |
47 |
48 |
49 |
50 | javax.servlet
51 | servlet-api
52 | provided
53 | true
54 |
55 |
56 | ${project.groupId}
57 | sysout-over-slf4j-system
58 | ${project.version}
59 | provided
60 |
61 |
62 |
63 |
64 | uk.org.lidalia
65 | lidalia-test-dependencies
66 | pom
67 |
68 |
69 | commons-io
70 | commons-io
71 | test
72 |
73 |
74 | org.apache.commons
75 | commons-lang3
76 | test
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/.gitignore:
--------------------------------------------------------------------------------
1 | /resources
2 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/java/uk/org/lidalia/sysoutslf4j/context/CallOrigin.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009-2012 Robert Elliot
3 | * All rights reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package uk.org.lidalia.sysoutslf4j.context;
26 |
27 | import com.google.common.base.Optional;
28 | import com.google.common.base.Supplier;
29 | import uk.org.lidalia.sysoutslf4j.system.PerContextPrintStream;
30 |
31 | import java.util.List;
32 |
33 | import static java.util.Arrays.asList;
34 |
35 | final class CallOrigin {
36 |
37 | private static final Supplier THROW_ILLEGAL_STATE_EXCEPTION = new Supplier() {
38 | @Override
39 | public Integer get() {
40 | throw new IllegalStateException("Must be called from down stack of " + PerContextPrintStream.class.getName());
41 | }
42 | };
43 | private final boolean printingStackTrace;
44 | private final String className;
45 | private final boolean inLoggingSystem;
46 |
47 | private CallOrigin(final boolean isStacktrace, final boolean inLoggingSystem, final String className) {
48 | this.printingStackTrace = isStacktrace;
49 | this.className = className;
50 | this.inLoggingSystem = inLoggingSystem;
51 | }
52 |
53 | boolean isPrintingStackTrace() {
54 | return printingStackTrace;
55 | }
56 |
57 | String getClassName() {
58 | return className;
59 | }
60 |
61 | boolean isInLoggingSystem() {
62 | return inLoggingSystem;
63 | }
64 |
65 | @Override
66 | public String toString() {
67 | return "CallOrigin{" +
68 | "printingStackTrace=" + printingStackTrace +
69 | ", className='" + className + '\'' +
70 | ", inLoggingSystem=" + inLoggingSystem +
71 | '}';
72 | }
73 |
74 | static CallOrigin getCallOrigin(LoggingSystemRegister loggingSystemRegister) {
75 | Thread currentThread = Thread.currentThread();
76 | final List stackTraceElements = asList(currentThread.getStackTrace());
77 | int firstPerContextPrintStreamFrame = frameWithPrintStreamClassName(stackTraceElements, 0).or(THROW_ILLEGAL_STATE_EXCEPTION);
78 | int secondPerContextPrintStreamFrame = frameWithPrintStreamClassName(stackTraceElements, firstPerContextPrintStreamFrame + 1).or(stackTraceElements.size());
79 |
80 | final List interestingStackTraceElements = stackTraceElements.subList(firstPerContextPrintStreamFrame + 1, secondPerContextPrintStreamFrame);
81 |
82 | for (int i = interestingStackTraceElements.size() - 2; i >= 0; i--) {
83 | StackTraceElement stackTraceElement = interestingStackTraceElements.get(i);
84 | String currentClassName = stackTraceElement.getClassName();
85 | if (currentClassName.equals(Throwable.class.getName()) && stackTraceElement.getMethodName().equals("printStackTrace")) {
86 | return new CallOrigin(true, false, getOuterClassName(interestingStackTraceElements.get(i + 1).getClassName()));
87 | }
88 | if (loggingSystemRegister.isInLoggingSystem(currentClassName)) {
89 | return new CallOrigin(false, true, null);
90 | }
91 | }
92 | return new CallOrigin(false, false, getOuterClassName(interestingStackTraceElements.get(0).getClassName()));
93 | }
94 |
95 | private static Optional frameWithPrintStreamClassName(final List stackTraceElements, int startPoint) {
96 | for (int i = startPoint; i < stackTraceElements.size(); i++) {
97 | StackTraceElement stackTraceElement = stackTraceElements.get(i);
98 | String currentClassName = stackTraceElement.getClassName();
99 | if (currentClassName.equals(PerContextPrintStream.class.getName())) {
100 | return Optional.of(i);
101 | }
102 | }
103 | return Optional.absent();
104 | }
105 |
106 | private static String getOuterClassName(final String className) {
107 | final int startOfInnerClassName = className.indexOf('$');
108 | final String outerClassName;
109 | if (startOfInnerClassName == -1) {
110 | outerClassName = className;
111 | } else {
112 | outerClassName = className.substring(0, startOfInnerClassName);
113 | }
114 | return outerClassName;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/java/uk/org/lidalia/sysoutslf4j/context/LoggingMessages.java:
--------------------------------------------------------------------------------
1 | package uk.org.lidalia.sysoutslf4j.context;
2 |
3 | public final class LoggingMessages {
4 | public static final String PERFORMANCE_WARNING = "A logging system is sending data to the console. This will work but with a significant performance hit. " +
5 | "Visit http://projects.lidalia.org.uk/sysout-over-slf4j/performance.html for details of how to avoid this.";
6 |
7 | private LoggingMessages() {
8 | throw new UnsupportedOperationException("Not instantiable");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/java/uk/org/lidalia/sysoutslf4j/context/LoggingOutputStream.java:
--------------------------------------------------------------------------------
1 | package uk.org.lidalia.sysoutslf4j.context;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.PrintStream;
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.concurrent.atomic.AtomicBoolean;
9 |
10 | import uk.org.lidalia.slf4jext.Level;
11 | import uk.org.lidalia.slf4jext.Logger;
12 | import uk.org.lidalia.slf4jext.LoggerFactory;
13 | import uk.org.lidalia.sysoutslf4j.context.exceptionhandlers.ExceptionHandlingStrategy;
14 |
15 | import static uk.org.lidalia.sysoutslf4j.context.CallOrigin.getCallOrigin;
16 |
17 | class LoggingOutputStream extends ByteArrayOutputStream {
18 |
19 | private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoggingOutputStream.class);
20 |
21 | private final Level level;
22 | private final ExceptionHandlingStrategy exceptionHandlingStrategy;
23 | private final PrintStream originalPrintStream;
24 | private final LoggingSystemRegister loggingSystemRegister;
25 |
26 | LoggingOutputStream(final Level level, final ExceptionHandlingStrategy exceptionHandlingStrategy,
27 | final PrintStream originalPrintStream, final LoggingSystemRegister loggingSystemRegister) {
28 | super();
29 | this.level = level;
30 | this.exceptionHandlingStrategy = exceptionHandlingStrategy;
31 | this.originalPrintStream = originalPrintStream;
32 | this.loggingSystemRegister = loggingSystemRegister;
33 | }
34 |
35 | @Override
36 | public synchronized void flush() throws IOException {
37 | final CallOrigin callOrigin = getCallOrigin(loggingSystemRegister);
38 | if (callOrigin.isInLoggingSystem()) {
39 | writeToOriginalPrintStream();
40 | } else {
41 | String bufferAsString = new String(toByteArray());
42 | if (bufferAsString.endsWith("\n")) {
43 | reset();
44 | log(callOrigin, bufferAsString);
45 | } else if (bufferAsString.contains("\n")) {
46 | reset();
47 | List messages = Arrays.asList(bufferAsString.split("\n"));
48 | List messagesToLog = messages.subList(0, messages.size() - 1);
49 | for (String messageToLog : messagesToLog) {
50 | log(callOrigin, messageToLog);
51 | }
52 | String lastMessage = messages.get(messages.size() - 1);
53 | write(lastMessage.getBytes());
54 | }
55 | }
56 | }
57 |
58 | private void writeToOriginalPrintStream() throws IOException {
59 | exceptionHandlingStrategy.notifyNotStackTrace();
60 | warnAboutPerformance();
61 | writeTo(originalPrintStream);
62 | originalPrintStream.flush();
63 | reset();
64 | }
65 |
66 | private static final AtomicBoolean warned = new AtomicBoolean(false);
67 |
68 | private static void warnAboutPerformance() {
69 | if (warned.compareAndSet(false, true)) {
70 | log.warn(LoggingMessages.PERFORMANCE_WARNING);
71 | }
72 | }
73 |
74 | private void log(final CallOrigin callOrigin, String bufferAsString) {
75 | String valueToLog = StringUtils.stripEnd(bufferAsString, " \r\n");
76 | try {
77 | if (valueToLog.length() > 0) {
78 | if (callOrigin.isPrintingStackTrace()) {
79 | exceptionHandlingStrategy.handleExceptionLine(valueToLog, org.slf4j.LoggerFactory.getLogger(callOrigin.getClassName()));
80 | } else {
81 | exceptionHandlingStrategy.notifyNotStackTrace();
82 | LoggerFactory.getLogger(callOrigin.getClassName()).log(level, valueToLog);
83 | }
84 | }
85 | } catch (StackOverflowError stackOverflowError) {
86 | throw new IllegalStateException("Logging system " + org.slf4j.LoggerFactory.getLogger(Object.class).getClass() +
87 | " is sending data to the console - please register it by calling SysOutOverSLF4J.registerLoggingSystem. " +
88 | "Original message: " + System.getProperty("line.separator") + valueToLog, stackOverflowError);
89 | }
90 | }
91 |
92 | protected void finalize() throws Throwable {
93 | super.finalize();
94 | String bufferAsString = StringUtils.stripEnd(new String(toByteArray()), " \r\n");
95 | if (bufferAsString.length() > 0) {
96 | Logger logger = LoggerFactory.getLogger(SysOutOverSLF4J.class);
97 | logger.log(level, bufferAsString);
98 | }
99 | reset();
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/java/uk/org/lidalia/sysoutslf4j/context/LoggingSystemRegister.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009-2012 Robert Elliot
3 | * All rights reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package uk.org.lidalia.sysoutslf4j.context;
26 |
27 | import java.util.Set;
28 | import java.util.concurrent.CopyOnWriteArraySet;
29 |
30 | import org.slf4j.Logger;
31 | import org.slf4j.LoggerFactory;
32 |
33 | import static java.util.Arrays.asList;
34 |
35 | class LoggingSystemRegister {
36 |
37 | private static final Logger LOG = LoggerFactory.getLogger(SysOutOverSLF4J.class);
38 | private final Set loggingSystemNameFragments = new CopyOnWriteArraySet(asList(
39 | "org.x4juli.",
40 | "org.grlea.log.",
41 | "org.slf4j.impl.SimpleLogger",
42 | "ch.qos.logback.",
43 | "org.slf4j.impl.Log4jLoggerAdapter",
44 | "org.slf4j.impl.JDK14LoggerAdapter",
45 | "org.apache.log4j.",
46 | "java.util.logging.",
47 | "uk.org.lidalia.slf4jtest."));
48 |
49 | void registerLoggingSystem(final String packageName) {
50 | loggingSystemNameFragments.add(packageName);
51 | LOG.info("Package {} registered; all classes within it or subpackages of it will " +
52 | "be allowed to print to System.out and System.err", packageName);
53 | }
54 |
55 | void unregisterLoggingSystem(final String packageName) {
56 | if (loggingSystemNameFragments.remove(packageName)) {
57 | LOG.info("Package {} unregistered; all classes within it or subpackages of it will " +
58 | "have System.out and System.err redirected to SLF4J", packageName);
59 | }
60 | }
61 |
62 | boolean isInLoggingSystem(final String className) {
63 | for (String packageName : loggingSystemNameFragments) {
64 | if (className.startsWith(packageName)) {
65 | return true;
66 | }
67 | }
68 | return false;
69 | }
70 |
71 | LoggingSystemRegister() {
72 | super();
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/sysout-over-slf4j-context/src/main/java/uk/org/lidalia/sysoutslf4j/context/ReferenceHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009-2012 Robert Elliot
3 | * All rights reserved.
4 | *
5 | * Permission is hereby granted, free of charge, to any person obtaining
6 | * a copy of this software and associated documentation files (the
7 | * "Software"), to deal in the Software without restriction, including
8 | * without limitation the rights to use, copy, modify, merge, publish,
9 | * distribute, sublicense, and/or sell copies of the Software, and to
10 | * permit persons to whom the Software is furnished to do so, subject to
11 | * the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be
14 | * included in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package uk.org.lidalia.sysoutslf4j.context;
26 |
27 | import java.util.Collections;
28 | import java.util.IdentityHashMap;
29 | import java.util.Map;
30 |
31 | final class ReferenceHolder {
32 |
33 | private static final Map