Created
April 15, 2015 12:24
-
-
Save greycode/dd8e37d0ba4dff930f6f to your computer and use it in GitHub Desktop.
一个简单的log4j2的redis appender ,依赖于Jedis (A simple log4j2 redis appender which uses Jedis)
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
<?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> |
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
<!-- 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> |
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
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(); | |
} | |
} |
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
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
Do not forget to: this.jedis.close(); after insertObject, give resource back to the pool.