日志系統(tǒng)采用logback今野,相對(duì)于log4j耘沼,logback有以下優(yōu)點(diǎn):
https://blog.csdn.net/caisini_vc/article/details/48551287
- 更快的執(zhí)行速度
- 充分的測(cè)試
- 自動(dòng)重新載入配置文件
- 優(yōu)雅地從I/O錯(cuò)誤中恢復(fù)
- 自動(dòng)清除舊的日志歸檔文件
- 自動(dòng)壓縮歸檔日志文件
sigma代碼結(jié)構(gòu):
SigmaLogEntity定義日志實(shí)體
@Data
public class SigmaLogEntity implements Serializable {
/**
* 系統(tǒng)代碼
*/
private String systemCode;
/**
* 子業(yè)務(wù)
*/
private String source;
/**
* 消息
*/
private String message = "";
/**
* 詳細(xì)
*/
private String detail;
/**
*
*/
private String appDomainName = "";
/**
* 進(jìn)程Id
*/
private Long processId;
/**
* 進(jìn)程名稱
*/
private String processName;
/**
* ip地址
*/
private String ipAddress;
/**
* 線程Id
*/
private Long threadId;
/**
* 創(chuàng)建日期
*/
private String createTime;
/**
* 機(jī)器名稱
*/
private String machineName;
/**
* 日誌級(jí)別
*/
@JsonIgnore
private Level level;
}
SigmaLoggingAppender繼承BaseSigmaLoggingAppender,重寫convert()和send()兩個(gè)方法
convert():
/**
* 日誌轉(zhuǎn)換
*
* @param loggingEvent 日誌對(duì)象
* @return 構(gòu)造對(duì)象
*/
@Override
protected SigmaLogEntity convert(ILoggingEvent loggingEvent) {
DateTimeFormatter simpleDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
var sigmaLogEntity = new SigmaLogEntity();
sigmaLogEntity.setSystemCode(systemCode);
sigmaLogEntity.setSource(sourceCode);
sigmaLogEntity.setCreateTime(simpleDateFormat.format(LocalDateTime.now()));
sigmaLogEntity.setLevel(loggingEvent.getLevel());
sigmaLogEntity.setMessage(loggingEvent.getMessage());
sigmaLogEntity.setThreadId(Thread.currentThread().getId());
sigmaLogEntity.setMachineName(machineName);
sigmaLogEntity.setDetail(loggingEvent.getFormattedMessage());
sigmaLogEntity.setIpAddress(ipAddress);
sigmaLogEntity.setAppDomainName("");
sigmaLogEntity.setProcessId(Long.parseLong(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]));
sigmaLogEntity.setProcessName(ManagementFactory.getRuntimeMXBean().getName());
return sigmaLogEntity;
}
/**
* 日誌發(fā)送
*
* @param entity 對(duì)象
*/
protected void send(SigmaLogEntity entity) {
var response = restTemplate.postForObject(getUrlByLevel(entity.getLevel()), entity, String.class);
System.out.println("send to es success:" + response);
}
TravelPort1.0中應(yīng)用logback顽腾,并通過sigma發(fā)送日志到日志系統(tǒng)(spring boot 集成ELK)
pom.xml加入sigma依賴:
<dependency>
<groupId>com.sigma</groupId>
<artifactId>sigma-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.sigma</groupId>
<artifactId>sigma-logging</artifactId>
<version>1.0.0</version>
</dependency>
在resources下加入logback-spring.xml文件,
測(cè)試代碼:
@SpringBootTest
@RunWith(SpringRunner.class)
public class TravelportConfigTest {
@Autowired
TravelportConfig travelportConfig;
private static Logger LOGGER = (Logger) LoggerFactory.getLogger(TravelportConfigTest.class);
@Test
public void displayTravelConfig() {
LOGGER.debug("test debug----------------------------------");
LOGGER.info("test info------------------------------------");
LOGGER.error("test error----------------------------------");
LOGGER.trace("test trace----------------------------------");
System.out.println(travelportConfig);
}
}
運(yùn)行結(jié)束后即可在kibana中查看到日志。
日志系統(tǒng)流程圖: