Skip to content

Commit

Permalink
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
ceharris committed Apr 4, 2013
2 parents 26b6bde + 9546288 commit 7a0e0cf
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 0 deletions.
@@ -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();
}

}
@@ -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();
}

}
@@ -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>



@@ -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>



@@ -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>



@@ -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>



0 comments on commit 7a0e0cf

Please sign in to comment.