Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #100 from ceharris/LOGBACK-826-examples
LOGBACK-826: added SocketRemote examples
- Loading branch information
Showing
6 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
logback-examples/src/main/java/chapters/appenders/socket/SocketRemoteClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* File created on Apr 2, 2013 | ||
* | ||
* Copyright 2008-2011 Virginia Polytechnic Institute and State University | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package chapters.appenders.socket; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.JoranConfigurator; | ||
|
||
/** | ||
* This application loads a configuration containing a | ||
* {@link SocketRemote} and then logs events received from the remote | ||
* appender to the console. | ||
*/ | ||
public class SocketRemoteClient { | ||
|
||
static void usage(String msg) { | ||
System.err.println(msg); | ||
System.err.println("Usage: java " + SocketRemoteClient.class.getName() + | ||
" configFile\n" + | ||
" configFile a logback configuration file" + | ||
" in XML format."); | ||
System.exit(1); | ||
} | ||
|
||
static public void main(String[] args) throws Exception { | ||
if (args.length != 1) { | ||
usage("Wrong number of arguments."); | ||
} | ||
|
||
String configFile = args[0]; | ||
|
||
if (configFile.endsWith(".xml")) { | ||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
JoranConfigurator configurator = new JoranConfigurator(); | ||
lc.stop(); | ||
configurator.setContext(lc); | ||
configurator.doConfigure(configFile); | ||
} | ||
|
||
Thread.sleep(Long.MAX_VALUE); | ||
((LoggerContext) LoggerFactory.getILoggerFactory()).stop(); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
logback-examples/src/main/java/chapters/appenders/socket/SocketRemoteServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* File created on Apr 2, 2013 | ||
* | ||
* Copyright 2008-2011 Virginia Polytechnic Institute and State University | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package chapters.appenders.socket; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.JoranConfigurator; | ||
|
||
/** | ||
* This application loads a configuration containing a | ||
* {@link ServerSocketAppender} and then allows the user to enter messages | ||
* which will be relayed to remote clients via this appender. | ||
*/ | ||
public class SocketRemoteServer { | ||
|
||
static void usage(String msg) { | ||
System.err.println(msg); | ||
System.err.println("Usage: java " + SocketRemoteServer.class.getName() + | ||
" configFile\n" + | ||
" configFile a logback configuration file" + | ||
" in XML format."); | ||
System.exit(1); | ||
} | ||
|
||
static public void main(String[] args) throws Exception { | ||
if (args.length != 1) { | ||
usage("Wrong number of arguments."); | ||
} | ||
|
||
String configFile = args[0]; | ||
|
||
if (configFile.endsWith(".xml")) { | ||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
JoranConfigurator configurator = new JoranConfigurator(); | ||
lc.stop(); | ||
configurator.setContext(lc); | ||
configurator.doConfigure(configFile); | ||
} | ||
|
||
Logger logger = LoggerFactory.getLogger(SocketRemoteServer.class); | ||
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
while (true) { | ||
System.out.println( | ||
"Type a message to send to remote clients. Type 'q' to quit."); | ||
|
||
String s = reader.readLine(); | ||
|
||
if (s.equals("q")) { | ||
break; | ||
} else { | ||
logger.debug(s); | ||
} | ||
} | ||
|
||
((LoggerContext) LoggerFactory.getILoggerFactory()).stop(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
logback-examples/src/main/java/chapters/appenders/socket/remoteClient.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<!-- ==================================================================== --> | ||
<!-- Sample SocketRemote configuration. --> | ||
<!-- ==================================================================== --> | ||
|
||
<configuration debug="true"> | ||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<layout class="ch.qos.logback.classic.PatternLayout"> | ||
<pattern>%date %-5level [%thread] %logger - %message%n</pattern> | ||
</layout> | ||
</appender> | ||
|
||
<remote class="ch.qos.logback.classic.net.SocketRemote"> | ||
<address>${address}</address> | ||
<port>${port}</port> | ||
<reconnectionDelay>5000</reconnectionDelay> | ||
</remote> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
|
||
</configuration> | ||
|
||
|
||
|
22 changes: 22 additions & 0 deletions
22
logback-examples/src/main/java/chapters/appenders/socket/remoteServer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<!-- ==================================================================== --> | ||
<!-- Sample ServerSocketAppender configuration. --> | ||
<!-- ==================================================================== --> | ||
|
||
<configuration debug="true"> | ||
|
||
<appender name="SERVER" | ||
class="ch.qos.logback.classic.net.server.ServerSocketAppender"> | ||
<port>${port}</port> | ||
<includeCallerData>${includeCallerData}</includeCallerData> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="SERVER" /> | ||
</root> | ||
|
||
</configuration> | ||
|
||
|
||
|
34 changes: 34 additions & 0 deletions
34
logback-examples/src/main/java/chapters/appenders/socket/ssl/remoteClient.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<!-- ==================================================================== --> | ||
<!-- Sample SSLSocketRemote configuration. --> | ||
<!-- ==================================================================== --> | ||
|
||
<configuration debug="true"> | ||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<layout class="ch.qos.logback.classic.PatternLayout"> | ||
<pattern>%date %-5level [%thread] %logger - %message%n</pattern> | ||
</layout> | ||
</appender> | ||
|
||
<remote class="ch.qos.logback.classic.net.SSLSocketRemote"> | ||
<address>${address}</address> | ||
<port>${port}</port> | ||
<reconnectionDelay>5000</reconnectionDelay> | ||
<ssl> | ||
<trustStore> | ||
<location>${truststore}</location> | ||
<password>${password}</password> | ||
</trustStore> | ||
</ssl> | ||
</remote> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
|
||
</configuration> | ||
|
||
|
||
|
28 changes: 28 additions & 0 deletions
28
logback-examples/src/main/java/chapters/appenders/socket/ssl/remoteServer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<!-- ==================================================================== --> | ||
<!-- Sample SSLServerSocketAppender configuration. --> | ||
<!-- ==================================================================== --> | ||
|
||
<configuration debug="true"> | ||
|
||
<appender name="SERVER" | ||
class="ch.qos.logback.classic.net.server.SSLServerSocketAppender"> | ||
<port>${port}</port> | ||
<includeCallerData>${includeCallerData}</includeCallerData> | ||
<ssl> | ||
<keyStore> | ||
<location>${keystore}</location> | ||
<password>${password}</password> | ||
</keyStore> | ||
</ssl> | ||
</appender> | ||
|
||
<root level="debug"> | ||
<appender-ref ref="SERVER" /> | ||
</root> | ||
|
||
</configuration> | ||
|
||
|
||
|