我是靠谱客的博主 害羞小丸子,这篇文章主要介绍一个简单的log4j2的redis appender ,依赖于Jedis (A simple log4j2 redis appender which uses Jedis),现在分享给大家,希望可以做个参考。
| <?xml version="1.0" encoding="UTF-8"?> | |
| <Configuration status="info" name="sd" packages="org.greycode.sd.base.log"> | |
| <Properties> | |
| <Property name="logPattern">[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] %m%n</Property> | |
| </Properties> | |
| <Appenders> | |
| <Console name="STDOUT" target="SYSTEM_OUT"> | |
| <PatternLayout pattern="${logPattern}" /> | |
| </Console> | |
| <NoSql name="redis"> | |
| <!-- <Redis host="localhost" port="" timeout="" password="" database="" clientName=""/> --> | |
| <Redis host="localhost" /> | |
| </NoSql> | |
| </Appenders> | |
| <Loggers> | |
| <asyncRoot level="info" includeLocation="true"> | |
| <AppenderRef ref="STDOUT" /> | |
| <AppenderRef ref="redis" /> | |
| </asyncRoot> | |
| </Loggers> | |
| </Configuration> |
Raw
pom.xml
| <!-- LOG begin --> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-core</artifactId> | |
| <version>${log4j2.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-jcl</artifactId> | |
| <version>${log4j2.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-web</artifactId> | |
| <version>${log4j2.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-slf4j-impl</artifactId> | |
| <version>${log4j2.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.logging.log4j</groupId> | |
| <artifactId>log4j-nosql</artifactId> | |
| <version>${log4j2.version}</version> | |
| </dependency> | |
| <!-- LOG end --> | |
| <dependency> | |
| <groupId>com.lmax</groupId> | |
| <artifactId>disruptor</artifactId> | |
| <version>${disruptor.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>redis.clients</groupId> | |
| <artifactId>jedis</artifactId> | |
| <version>${jedis.version}</version> | |
| </dependency> |
Raw
RedisConnection.java
| package org.greycode.sd.base.log; | |
| import java.util.Map; | |
| import java.util.concurrent.atomic.AtomicBoolean; | |
| import org.apache.logging.log4j.core.appender.AppenderLoggingException; | |
| import org.apache.logging.log4j.nosql.appender.DefaultNoSqlObject; | |
| import org.apache.logging.log4j.nosql.appender.NoSqlConnection; | |
| import org.apache.logging.log4j.nosql.appender.NoSqlObject; | |
| import redis.clients.jedis.Jedis; | |
| import redis.clients.jedis.JedisPool; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| public class RedisConnection implements NoSqlConnection<Map<String, Object>, DefaultNoSqlObject> { | |
| private static final ObjectMapper mapper = new ObjectMapper(); | |
| private final JedisPool pool; | |
| private final Jedis jedis; | |
| private final AtomicBoolean closed = new AtomicBoolean(false); | |
| public RedisConnection (JedisPool pool) { | |
| this.jedis = pool.getResource(); | |
| this.pool = pool; | |
| } | |
| @Override | |
| public DefaultNoSqlObject createObject() { | |
| return new DefaultNoSqlObject(); | |
| } | |
| @Override | |
| public DefaultNoSqlObject[] createList(int length) { | |
| // TODO Auto-generated method stub | |
| return new DefaultNoSqlObject[length]; | |
| } | |
| @Override | |
| public void insertObject(NoSqlObject<Map<String, Object>> object) { | |
| try { | |
| jedis.lpush("redis-log4j2", mapper.writeValueAsString(object.unwrap())); | |
| } catch (Exception e) { | |
| throw new AppenderLoggingException("往Redis中插入数据时发生错误: " + e.getMessage(), e); | |
| } | |
| } | |
| @Override | |
| public void close() { | |
| if (closed.compareAndSet(false, true)) { | |
| this.pool.close(); | |
| } | |
| } | |
| @Override | |
| public boolean isClosed() { | |
| return closed.get(); | |
| } | |
| } |
Raw
RedisProvider.java
| package org.greycode.sd.base.log; | |
| import org.apache.logging.log4j.Logger; | |
| import org.apache.logging.log4j.core.config.plugins.Plugin; | |
| import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | |
| import org.apache.logging.log4j.core.config.plugins.PluginFactory; | |
| import org.apache.logging.log4j.nosql.appender.NoSqlProvider; | |
| import org.apache.logging.log4j.status.StatusLogger; | |
| import redis.clients.jedis.Jedis; | |
| import redis.clients.jedis.JedisPool; | |
| import redis.clients.jedis.JedisPoolConfig; | |
| import redis.clients.jedis.Protocol; | |
| @Plugin(name = "Redis", category = "Core", printObject = true) | |
| public class RedisProvider implements NoSqlProvider<RedisConnection> { | |
| private static final Logger LOGGER = StatusLogger.getLogger(); | |
| private final JedisPool pool; | |
| private RedisProvider (JedisPool pool) { | |
| this.pool = pool; | |
| } | |
| @PluginFactory | |
| public static RedisProvider createNoSqlProvider ( | |
| @PluginAttribute("host") final String host, | |
| @PluginAttribute("port") int port, | |
| @PluginAttribute("timeout") int timeout, | |
| @PluginAttribute("password") final String password, | |
| @PluginAttribute("database") int database, | |
| @PluginAttribute("clientName") final String clientName | |
| ) { | |
| if (host != null && host.length() > 0) { | |
| try { | |
| if (port <= 0) port = 6379; | |
| if (timeout <= 0) timeout = Protocol.DEFAULT_TIMEOUT; | |
| if (database <= 0) database = Protocol.DEFAULT_DATABASE; | |
| JedisPool pool = null; | |
| pool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password, database, clientName); | |
| try (Jedis jedis = pool.getResource()) { } | |
| return new RedisProvider(pool); | |
| } catch (Exception e) { | |
| LOGGER.error("与Redis服务器建立连接时发生错误[{}:{}]:{}",e.getMessage(), host, port); | |
| return null; | |
| } | |
| } | |
| LOGGER.error("Redis 服务器地址[host]不能为空"); | |
| return null; | |
| } | |
| @Override | |
| public RedisConnection getConnection() { | |
| return new RedisConnection(pool); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub. Already have an account?
Sign in to comment
最后
以上就是害羞小丸子最近收集整理的关于一个简单的log4j2的redis appender ,依赖于Jedis (A simple log4j2 redis appender which uses Jedis)的全部内容,更多相关一个简单的log4j2的redis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复