LoggerBuilder.java
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.huaheng.common.logger;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.rolling.RollingFileAppender;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @author ricardo
* data 2019.2.15
* 日志工具类
*/
@Component
public class LoggerBuilder {
private static final Map<String,Logger> container = new HashMap<>();
public Logger getLogger(String name) {
Logger logger = container.get(name);
if(logger != null) {
return logger;
}
synchronized (LoggerBuilder.class) {
logger = container.get(name);
if(logger != null) {
return logger;
}
logger = build(name);
container.put(name,logger);
}
return logger;
}
private static Logger build(String name) {
RollingFileAppender errorAppender =new AppenderTest().getAppender(name,Level.ERROR);
RollingFileAppender infoAppender =new AppenderTest().getAppender(name,Level.INFO);
RollingFileAppender warnAppender =new AppenderTest().getAppender(name,Level.WARN);
RollingFileAppender debugAppender =new AppenderTest().getAppender(name,Level.DEBUG);
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger logger = context.getLogger("FILE-" + name);
//设置不向上级打印信息
logger.setAdditive(false);
logger.addAppender(errorAppender);
logger.addAppender(infoAppender);
logger.addAppender(warnAppender);
logger.addAppender(debugAppender);
return logger;
}
}