You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2017/05/04 15:53:44 UTC
[2/2] logging-log4j2 git commit: HttpManager as facade to enable
multiple implementations
HttpManager as facade to enable multiple implementations
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fd1d7ea2
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fd1d7ea2
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fd1d7ea2
Branch: refs/heads/LOG4J2-1442
Commit: fd1d7ea204df40bd079750b2c2f4df4047afa2fe
Parents: 857c3e1
Author: Mikael Ståldal <mi...@magine.com>
Authored: Thu May 4 17:53:37 2017 +0200
Committer: Mikael Ståldal <mi...@magine.com>
Committed: Thu May 4 17:53:37 2017 +0200
----------------------------------------------------------------------
.../log4j/core/appender/HttpAppender.java | 9 ++-
.../log4j/core/appender/HttpManager.java | 56 +++-----------
.../core/appender/HttpURLConnectionManager.java | 81 ++++++++++++++++++++
3 files changed, 97 insertions(+), 49 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd1d7ea2/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
index e0f1b27..c43c63d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
@@ -17,7 +17,6 @@
package org.apache.logging.log4j.core.appender;
-import java.io.IOException;
import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@@ -65,7 +64,7 @@ public final class HttpAppender extends AbstractAppender {
@Override
public HttpAppender build() {
- final HttpManager httpManager = new HttpManager(getConfiguration(), getConfiguration().getLoggerContext(),
+ final HttpManager httpManager = new HttpURLConnectionManager(getConfiguration(), getConfiguration().getLoggerContext(),
getName(), url, method, connectTimeoutMillis, readTimeoutMillis, headers);
return new HttpAppender(getName(), getLayout(), getFilter(), isIgnoreExceptions(), httpManager);
}
@@ -134,6 +133,12 @@ public final class HttpAppender extends AbstractAppender {
}
@Override
+ public void start() {
+ super.start();
+ manager.startup();
+ }
+
+ @Override
public void append(final LogEvent event) {
try {
manager.send(getLayout(), event);
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd1d7ea2/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpManager.java
index 8f69659..dc3d218 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpManager.java
@@ -17,66 +17,28 @@
package org.apache.logging.log4j.core.appender;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
import java.util.Objects;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationException;
-import org.apache.logging.log4j.core.config.Property;
-
-public class HttpManager extends AbstractManager {
+public abstract class HttpManager extends AbstractManager {
private final Configuration configuration;
- private final URL url;
- private final String method;
- private final int connectTimeoutMillis;
- private final int readTimeoutMillis;
- private final Property[] headers;
- public HttpManager(final Configuration configuration, LoggerContext loggerContext, final String name,
- final String url, final String method, final int connectTimeoutMillis, final int readTimeoutMillis,
- final Property[] headers) {
+ protected HttpManager(final Configuration configuration, final LoggerContext loggerContext, final String name) {
super(loggerContext, name);
this.configuration = Objects.requireNonNull(configuration);
- try {
- this.url = new URL(url);
- } catch (MalformedURLException e) {
- throw new ConfigurationException(e);
- }
- this.method = Objects.requireNonNull(method, "method");
- this.connectTimeoutMillis = connectTimeoutMillis;
- this.readTimeoutMillis = readTimeoutMillis;
- this.headers = headers != null ? headers : new Property[0];
}
- public void send(final Layout<?> layout, final LogEvent event) throws IOException {
- HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
- urlConnection.setAllowUserInteraction(false);
- urlConnection.setDoOutput(true);
- urlConnection.setDoInput(true);
- urlConnection.setRequestMethod(method);
- if (connectTimeoutMillis > 0) urlConnection.setConnectTimeout(connectTimeoutMillis);
- if (readTimeoutMillis > 0) urlConnection.setReadTimeout(readTimeoutMillis);
- if (layout.getContentType() != null) urlConnection.setRequestProperty("Content-Type", layout.getContentType());
- for (Property header : headers) {
- urlConnection.setRequestProperty(
- header.getName(),
- header.isValueNeedsLookup() ? configuration.getStrSubstitutor().replace(event, header.getValue()) : header.getValue());
- }
- byte[] msg = layout.toByteArray(event);
- urlConnection.setFixedLengthStreamingMode(msg.length);
- urlConnection.connect();
- try (OutputStream os = urlConnection.getOutputStream()) {
- os.write(msg);
- }
- urlConnection.getInputStream().close();
+ public Configuration getConfiguration() {
+ return configuration;
+ }
+
+ public void startup() {
+ // This default implementation does nothing
}
+ public abstract void send(Layout<?> layout, LogEvent event) throws Exception;
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd1d7ea2/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
new file mode 100644
index 0000000..357c5b7
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.logging.log4j.core.appender;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Objects;
+
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationException;
+import org.apache.logging.log4j.core.config.Property;
+
+public class HttpURLConnectionManager extends HttpManager {
+
+ private final URL url;
+ private final String method;
+ private final int connectTimeoutMillis;
+ private final int readTimeoutMillis;
+ private final Property[] headers;
+
+ public HttpURLConnectionManager(final Configuration configuration, LoggerContext loggerContext, final String name,
+ final String url, final String method, final int connectTimeoutMillis, final int readTimeoutMillis,
+ final Property[] headers) {
+ super(configuration, loggerContext, name);
+ try {
+ this.url = new URL(url);
+ } catch (MalformedURLException e) {
+ throw new ConfigurationException(e);
+ }
+ this.method = Objects.requireNonNull(method, "method");
+ this.connectTimeoutMillis = connectTimeoutMillis;
+ this.readTimeoutMillis = readTimeoutMillis;
+ this.headers = headers != null ? headers : new Property[0];
+ }
+
+ @Override
+ public void send(final Layout<?> layout, final LogEvent event) throws IOException {
+ HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
+ urlConnection.setAllowUserInteraction(false);
+ urlConnection.setDoOutput(true);
+ urlConnection.setDoInput(true);
+ urlConnection.setRequestMethod(method);
+ if (connectTimeoutMillis > 0) urlConnection.setConnectTimeout(connectTimeoutMillis);
+ if (readTimeoutMillis > 0) urlConnection.setReadTimeout(readTimeoutMillis);
+ if (layout.getContentType() != null) urlConnection.setRequestProperty("Content-Type", layout.getContentType());
+ for (Property header : headers) {
+ urlConnection.setRequestProperty(
+ header.getName(),
+ header.isValueNeedsLookup() ? getConfiguration().getStrSubstitutor().replace(event, header.getValue()) : header.getValue());
+ }
+ byte[] msg = layout.toByteArray(event);
+ urlConnection.setFixedLengthStreamingMode(msg.length);
+ urlConnection.connect();
+ try (OutputStream os = urlConnection.getOutputStream()) {
+ os.write(msg);
+ }
+ urlConnection.getInputStream().close();
+ }
+
+}