我正在尝试找到一种很好的方法来为我的日志添加前缀,而不是在每次调用时都传递它,而不再使用instanciate Logger。
目的是单独跟踪Rest调用。 (使用UUID在每次调用时重新生成前缀)
这就像
@RestController
class MyClass {
//Here the prefix is initialise once
//default value is X
Logger LOG = LoggerFactory.getLogger(MyClass.class);
@RequestMapping("/a")
void methodA() {
LOG.debug("foo");
}
@RequestMapping("/b")
void methodB() {
LOG.setPrefix("B");
LOG.debug("bar");
}
使用此输出
[...] [prefix X] foo
[...] [prefix B] bar
答案 0 :(得分:2)
正如您所说的那样,您正在使用Logback,这里有几种方法可以做您尝试做的事情:
每个日志条目都可以有一个"标记"为它建立。 (我见过的最好的文档是SLF4J FAQ。)像:
class MyClass {
Marker methodBMarker = MarkerFactory.getMarker("B");
Logger logger = LoggerFactory.getLogger(MyClass.class);
…
void methodB() {
logger.debug(methodBMarker, "bar");
}
}
您需要更新每种方法中的所有日志条目才能使用相应的标记。然后,您可以在布局中放置%marker
,将日志条目的标记放入日志中。
另一种选择是使用" Mapped Diagnostic Context"用于指定当前" context"的功能对于每个日志条目。
class MyClass {
Logger logger = LoggerFactory.getLogger(MyClass.class);
…
void methodB() {
MDC.put("method", "b");
try {
…
logger.debug("bar");
…
} finally {
MDC.clear();
}
}
}
然后,您将在布局中使用%mdc{method}
来输出该特定的MDC值。请注意,MDC实际上是用于每个线程的值,例如特定于Web连接的内容,因此确保从您不想要的内容中清除MDC非常重要。 #39;离开你想要登录值的上下文。
答案 1 :(得分:1)
有关如何使用MDC的示例,请参阅http://www.slf4j.org/extensions.html#event_logger。您不必使用EventLogger。一旦你在MDC中设置它们,它们就会出现在每个日志记录中。
标记不符合您的标准,因为必须在每次调用时指定它。
答案 2 :(得分:0)
这里我的MDC实施解释为与MDC分享我的实验。
//In this abstract class i'm defining initLogData methods to set MDC context
//It would be inherited by Controller and other classes who needs logging with traced transactions
public abstract class AbstractService {
protected LogData initLogData() {
return LogData.init();
}
protected LogData initLogData(String tName) {
return LogData.init(tName);
}
}
//LogData holds the MDC logic
public class LogData {
private final static int nRandom = 8;
//this keys are defined in logback pattern (see below)
private final static String tIdKey = "TID";
private final static String tNameKey = "TNAME";
//Transaction id
private String tId;
//Transaction name
private String tName;
public String getTId() {
return tId;
}
public void setTId(String tId) {
this.tId = tId;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
//random transaction id
//I'm not using uuid since its too longs and perfect unicity is not critical here
public String createTId(){
Random r = new Random();
StringBuilder sb = new StringBuilder();
while(sb.length() < nRandom){
sb.append(Integer.toHexString(r.nextInt()));
}
return sb.toString().substring(0, nRandom);
}
//private constructors (use init() methods to set LogData)
private LogData(String tId, String tName) {
this.tId = tId;
this.tName = tName;
}
private LogData(String tName) {
this.tId = createTId();
this.tName = tName;
}
private LogData() {
this.tId = createTId();
}
//init MDC with cascading calls processing (using same id/name within same context
//even if init() is called again)
public static LogData init(String tName) {
String previousTId = MDC.get(tIdKey);
String previousTName = MDC.get(tNameKey);
MDC.clear();
LogData logData = null;
if(previousTId != null) {
logData = new LogData(previousTId, previousTName);
} else {
logData = new LogData(tName);
}
MDC.put(tIdKey, logData.getTId());
MDC.put(tNameKey, logData.gettName());
return logData;
}
//init MDC without cascading calls management (new keys are generated for each init() call)
public static LogData init() {
MDC.clear();
LogData logData = new LogData();
MDC.put(tIdKey, logData.getTId());
return logData;
}
}
//logback.xml : values to include in log pattern
[%X{TID}] [%X{TNAME}]
@RestController
@RequestMapping("/test")
public class RestControllerTest extends AbstractRestService {
private final Logger LOG = LoggerFactory.getLogger(ServiceRestEntrypointStatus.class);
@RequestMapping(value="/testA")
public void testA() {
initLogData("testA");
LOG.debug("This is A");
}
@RequestMapping(value="/testB")
public void testB() {
initLogData("testA");
LOG.debug("This is B");
}
@RequestMapping(value="/testC")
public void testC() {
initLogData("testC");
LOG.debug("This is C");
testA();
testB();
}
}
调用RestControllerTest mapped / test / testA会产生:
[fdb5d310] [testA] This is A
调用/ test / testC生成(即使在子方法中调用initLogData,也会保留id和name):
[c7b0af53] [testC] This is C
[c7b0af53] [testC] This is A
[c7b0af53] [testC] This is B