Skip to content

Commit

Permalink
Merge pull request #175 from hjyun328/hjyun/develop
Browse files Browse the repository at this point in the history
FEATURE: Adding support for SLF4J logging
  • Loading branch information
minkikim89 authored Sep 28, 2018
2 parents 48cc6ec + 33a1848 commit 83f1db1
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 13 deletions.
43 changes: 41 additions & 2 deletions docs/02-arcus-java-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ ArcusClient client = ArcusClient.createArcusClient(SERVICE_CODE, cfb);

##### Logger 설정

Arcus client 사용 시에 ArcusClient 자체 logger(DefaultLogger), log4j, JDK logger 등
3가지 종류의 Logger를 사용할 수 있다.
Arcus client 사용 시에 ArcusClient 자체 logger(DefaultLogger), log4j, slf4j, JDK logger 등
4가지 종류의 Logger를 사용할 수 있다.
사용할 logger를 지정하지 않으면 ArcusClient는 DefaultLogger를 기본으로 사용하며,
DefaultLogger는 INFO level 이상의 로그를 stderr (System.err) 로 출력한다. (변경 불가)

Expand Down Expand Up @@ -331,6 +331,45 @@ Ascii Protocol에 대한 자세한 내용은 [Arcus 서버 명령 프로토콜](

기타 log4j의 자세한 설정 방법은 [log4j 설정 방법](http://logging.apache.org/log4j/1.2/manual.html)을 확인하기 바란다.

##### SLF4JLogger 사용시 유의 사항

slf4j와 호환되는 log4j 이외의 라이브러리(logback, log4j2, ...)를 쓸 경우, net.spy.memcached.compat.log.SLF4JLogger 클래스를 사용할 것이다. 이 클래스를 사용하기 전에 필수적으로 해야 하는 작업이 있다. (SLF4JLogger와 log4j를 조합해서 사용한다면 하지 않아도 된다.)

ArcusClient는 기본적으로 Zookeeper에 의해서 slf4j의 구현 라이브러리인 slf4j-log4j12를 기본 dependency로 가진다. 따라서 log4j 이외의 라이브러리를 SLF4JLogger와 조합해서 사용하려면 ArcusClient dependency의 exclusion에 slf4j-log4j12를 추가해야 한다.

예를 들어 ArcusClient 사용자가 SLF4JLogger와 logback을 조합해서 사용할 경우 dependency 설정을 다음과 같이 해야 한다.

```
<dependency>
<groupId>com.navercorp.arcus</groupId>
<artifactId>arcus-java-client</artifactId>
<version>${arcus-java-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
```

2개 이상의 slf4j의 구현 라이브러리(logback-classic, slf4j-log4j12, ...)들이 같은 classpath에 존재할 경우, SLF4J에서 [multiple binding error](http://www.slf4j.org/codes.html#multiple_bindings)가 발생하므로 반드시 exclusion 키워드를 이용해 slf4j 구현 라이브러리가 하나만 존재하도록 하여야 한다.

```
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
```

##### Transparent Front Cache 사용

Arcus는 기본적으로 원격 Cache 시스템이므로 요청에 대한 응답을 받을 때마다 데이터를 객체화해야 하는 단점이 있다.
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,26 @@
<version>1.2.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
Expand All @@ -54,6 +70,14 @@
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
54 changes: 53 additions & 1 deletion src/main/java/net/spy/memcached/compat/log/AbstractLogger.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// Copyright (c) 2002 SPY internetworking <[email protected]>
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.compat.log;

Expand Down Expand Up @@ -58,6 +79,37 @@ public Throwable getThrowable(Object args[]) {
*/
public abstract boolean isInfoEnabled();

/**
* Log a message at trace level.
*
* @param message the message to log
* @param exception the exception that caused the message to be generated
*/
public void trace(Object message, Throwable exception) {
log(Level.TRACE, message, exception);
}

/**
* Log a formatted message at trace level.
*
* @param message the message to log
* @param args the arguments for that message
*/
public void trace(String message, Object... args) {
if (isDebugEnabled()) {
trace(String.format(message, args), getThrowable(args));
}
}

/**
* Log a message at trace level.
*
* @param message the message to log
*/
public void trace(Object message) {
trace(message, null);
}

/**
* Log a message at debug level.
*
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/net/spy/memcached/compat/log/DefaultLogger.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// Copyright (c) 2002 SPY internetworking <[email protected]>
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.compat.log;

Expand All @@ -23,6 +44,14 @@ public DefaultLogger(String name) {
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
}

/**
* False.
*/
@Override
public boolean isTraceEnabled() {
return (false);
}

/**
* False.
*/
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/net/spy/memcached/compat/log/Level.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// Copyright (c) 2002 Dustin Sallings <[email protected]>
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.compat.log;

Expand All @@ -7,6 +28,10 @@
*/
public enum Level {

/**
* Trace level.
*/
TRACE,
/**
* Debug level.
*/
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/net/spy/memcached/compat/log/Log4JLogger.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// Copyright (c) 2002 Dustin Sallings <[email protected]>
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.compat.log;

Expand All @@ -22,6 +43,14 @@ public Log4JLogger(String name) {
l4jLogger = org.apache.log4j.Logger.getLogger(name);
}

/**
* True if the underlying logger would allow trace messages through.
*/
@Override
public boolean isTraceEnabled() {
return (l4jLogger.isTraceEnabled());
}

/**
* True if the underlying logger would allow debug messages through.
*/
Expand All @@ -41,7 +70,7 @@ public boolean isInfoEnabled() {
/**
* Wrapper around log4j.
*
* @param level net.spy.compat.log.AbstractLogger level.
* @param level net.spy.compat.log.Level level.
* @param message object message
* @param e optional throwable
*/
Expand All @@ -50,6 +79,9 @@ public void log(Level level, Object message, Throwable e) {
org.apache.log4j.Level pLevel = org.apache.log4j.Level.DEBUG;

switch (level == null ? Level.FATAL : level) {
case TRACE:
pLevel = org.apache.log4j.Level.TRACE;
break;
case DEBUG:
pLevel = org.apache.log4j.Level.DEBUG;
break;
Expand Down
53 changes: 52 additions & 1 deletion src/main/java/net/spy/memcached/compat/log/Logger.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
// Copyright (c) 2002 SPY internetworking <[email protected]>
/**
* Copyright (C) 2006-2009 Dustin Sallings
* Copyright (C) 2009-2013 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.compat.log;

Expand Down Expand Up @@ -30,6 +51,13 @@ public interface Logger {
* @return true if info messages would be displayed
*/
boolean isInfoEnabled();

/**
* True if info is enabled for this logger.
*
* @return true if trace messages would be displayed
*/
boolean isTraceEnabled();

/**
* Log a message at the specified level.
Expand All @@ -48,6 +76,29 @@ public interface Logger {
*/
void log(Level level, Object message);

/**
* Log a message at trace level.
*
* @param message the message to log
* @param exception the exception that caused the message to be generated
*/
void trace(Object message, Throwable exception);

/**
* Log a message at trace level.
*
* @param message the message to log
*/
void trace(Object message);

/**
* Log a formatted message at trace level.
*
* @param message the message to log
* @param args the arguments for that message
*/
void trace(String message, Object... args);

/**
* Log a message at debug level.
*
Expand Down
Loading

0 comments on commit 83f1db1

Please sign in to comment.