|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache license, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the license for the specific language governing permissions and |
| 15 | + * limitations under the license. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.logging.log4j.redis.appender; |
| 19 | + |
| 20 | +import org.apache.logging.log4j.core.Appender; |
| 21 | +import org.apache.logging.log4j.core.Filter; |
| 22 | +import org.apache.logging.log4j.core.Layout; |
| 23 | +import org.apache.logging.log4j.core.LogEvent; |
| 24 | +import org.apache.logging.log4j.core.appender.AbstractAppender; |
| 25 | +import org.apache.logging.log4j.core.config.Node; |
| 26 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 27 | +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; |
| 28 | +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; |
| 29 | +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; |
| 30 | +import org.apache.logging.log4j.core.layout.AbstractStringLayout; |
| 31 | + |
| 32 | +import java.io.Serializable; |
| 33 | +import java.nio.charset.Charset; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +/** |
| 38 | + * Sends log events to a Redis Queue. All logs are appended Redis lists via the RPUSH command. |
| 39 | + */ |
| 40 | +@Plugin(name = "Redis", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true) |
| 41 | +public final class RedisAppender extends AbstractAppender { |
| 42 | + |
| 43 | + private RedisAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, |
| 44 | + final boolean ignoreExceptions, final RedisManager manager) { |
| 45 | + super(name, filter, layout, ignoreExceptions); |
| 46 | + this.manager = Objects.requireNonNull(manager, "Redis Manager"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Builds RedisAppender instances. |
| 51 | + * @param <B> The type to build |
| 52 | + */ |
| 53 | + public static class Builder<B extends Builder<B>> extends AbstractAppender.Builder<B> |
| 54 | + implements org.apache.logging.log4j.core.util.Builder<RedisAppender> { |
| 55 | + |
| 56 | + @PluginAttribute("keys") |
| 57 | + private String[] keys; |
| 58 | + |
| 59 | + @PluginAttribute(value = "host") |
| 60 | + @Required(message = "No Redis hostname provided") |
| 61 | + private String host; |
| 62 | + |
| 63 | + @PluginAttribute(value = "port") |
| 64 | + private int port; |
| 65 | + |
| 66 | + @PluginAttribute(value = "ssl") |
| 67 | + private boolean ssl = false; |
| 68 | + |
| 69 | + @SuppressWarnings("resource") |
| 70 | + @Override |
| 71 | + public RedisAppender build() { |
| 72 | + return new RedisAppender(getName(), getLayout(), getFilter(), isIgnoreExceptions(), getRedisManager()); |
| 73 | + } |
| 74 | + |
| 75 | + public Charset getCharset() { |
| 76 | + if (getLayout() instanceof AbstractStringLayout) { |
| 77 | + return ((AbstractStringLayout) getLayout()).getCharset(); |
| 78 | + } else { |
| 79 | + return Charset.defaultCharset(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + String[] getKeys() { |
| 84 | + return keys; |
| 85 | + } |
| 86 | + |
| 87 | + String getHost() { |
| 88 | + return host; |
| 89 | + } |
| 90 | + |
| 91 | + boolean getSsl() { |
| 92 | + return ssl; |
| 93 | + } |
| 94 | + |
| 95 | + int getPort() { |
| 96 | + return port; |
| 97 | + } |
| 98 | + |
| 99 | + public B withKeys(final String key) { |
| 100 | + this.keys = new String[]{key}; |
| 101 | + return asBuilder(); |
| 102 | + } |
| 103 | + |
| 104 | + public B withKeys(final String[] keys) { |
| 105 | + this.keys = keys; |
| 106 | + return asBuilder(); |
| 107 | + } |
| 108 | + |
| 109 | + public B withHost(final String host) { |
| 110 | + this.host = host; |
| 111 | + return asBuilder(); |
| 112 | + } |
| 113 | + |
| 114 | + public B withPort(final int port) { |
| 115 | + this.port = port; |
| 116 | + return asBuilder(); |
| 117 | + } |
| 118 | + |
| 119 | + public B withSsl(final boolean ssl) { |
| 120 | + this.ssl = ssl; |
| 121 | + return asBuilder(); |
| 122 | + } |
| 123 | + |
| 124 | + RedisManager getRedisManager() { |
| 125 | + return new RedisManager( |
| 126 | + getConfiguration().getLoggerContext(), |
| 127 | + getName(), |
| 128 | + getKeys(), |
| 129 | + getHost(), |
| 130 | + getPort(), |
| 131 | + getSsl(), |
| 132 | + getCharset() |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Creates a builder for a RedisAppender. |
| 139 | + * @return a builder for a RedisAppender. |
| 140 | + */ |
| 141 | + @PluginBuilderFactory |
| 142 | + public static <B extends Builder<B>> B newBuilder() { |
| 143 | + return new Builder<B>().asBuilder(); |
| 144 | + } |
| 145 | + |
| 146 | + private final RedisManager manager; |
| 147 | + |
| 148 | + @Override |
| 149 | + public void append(final LogEvent event) { |
| 150 | + if (event.getLoggerName() != null && event.getLoggerName().startsWith("org.apache.redis")) { |
| 151 | + LOGGER.warn("Recursive logging from [{}] for appender [{}].", event.getLoggerName(), getName()); |
| 152 | + } else { |
| 153 | + try { |
| 154 | + tryAppend(event); |
| 155 | + } catch (final Exception e) { |
| 156 | + error("Unable to write to Redis in appender [" + getName() + "]", event, e); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void tryAppend(final LogEvent event) { |
| 162 | + final Layout<? extends Serializable> layout = getLayout(); |
| 163 | + final byte[] header = layout.getHeader(); |
| 164 | + final byte[] body = layout.toByteArray(event); |
| 165 | + |
| 166 | + int len = (header != null ? header.length : 0) + body.length; |
| 167 | + byte[] data = new byte[len]; |
| 168 | + if (header != null) { |
| 169 | + System.arraycopy(header, 0, data, 0, header.length); |
| 170 | + System.arraycopy(body, 0, data, header.length, body.length); |
| 171 | + } else { |
| 172 | + System.arraycopy(body, 0, data, 0, body.length); |
| 173 | + } |
| 174 | + manager.send(data); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void start() { |
| 179 | + setStarting(); |
| 180 | + manager.startup(); |
| 181 | + setStarted(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean stop(final long timeout, final TimeUnit timeUnit) { |
| 186 | + setStopping(); |
| 187 | + boolean stopped = super.stop(timeout, timeUnit, false); |
| 188 | + stopped &= manager.stop(timeout, timeUnit); |
| 189 | + setStopped(); |
| 190 | + return stopped; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public String toString() { |
| 195 | + return "RedisAppender{" + |
| 196 | + "name=" + getName() + |
| 197 | + ", host=" + manager.getHost() + |
| 198 | + ", port=" + manager.getPort() + |
| 199 | + ", keys=" + manager.getKeysAsString() + |
| 200 | + '}'; |
| 201 | + } |
| 202 | +} |
0 commit comments