logback 日志处理
今天项目上接到一个需求 要求改日志信息
BL##告警级别#告警编码#告警对象#告警产生时间#主机名|IP#业务系统名称#子系统名称#模块名称#实例名称#告警类别#告警内容#业务相关方#详细信息##LB
开头结尾 加个配置拼上去就行了
设置ip及地址
package com.ztesoft.hjreport.hjreportconfig.utils;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LogIpConfig extends ClassicConverter {
private static final Logger LOGGER = LoggerFactory.getLogger(LogIpConfig .class);
@Override
public String convert(ILoggingEvent event) {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
LOGGER.error("获取日志Ip异常", e);
}
return null;
}
}
%ip
主机名称
package com.ztesoft.hjreport.hjreportconfig.configuration;
import ch.qos.logback.core.PropertyDefinerBase;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetLogBackHostNameConfiguration extends PropertyDefinerBase {
@Override
public String getPropertyValue() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
}
${HOST_NAME}
告警级别
Output just one letter for the level
Instead of printing TRACE, DEBUG, WARN, INFO or ERROR for the level, you may want to print just T, D, W, I and E. You could write a custom converter [^ 自定义转换器] for this purpose, or simply make use of format modifiers (just discussed) to shorten the level value to a single character. The appropriate conversion specifier would be "%.-1level
".
http://logback.qos.ch/manual/layouts.html#customConversionSpecifier
package com.ztesoft.hjreport.hjreportconfig.utils;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
public class MySampleConverter extends ClassicConverter {
long start = System.nanoTime();
@Override
public String convert(ILoggingEvent event) {
Level level = event.getLevel();
long nowInNanos = System.nanoTime();
if(level.levelStr.equals("WARN")){
return "WARNING";
}
if(level.levelStr.equals("ERROR")){
return "SERIOUS";
}
return level.levelStr;
}
}
%nanos
主要的日志级别
/**
* The OFF
is used to turn off logging.
*/
public static final Level OFF = new Level(OFF_INT, "OFF");
/**
* The ERROR
level designates error events which may or not
* be fatal to the application.
*/
public static final Level ERROR = new Level(ERROR_INT, "ERROR");
/**
* The WARN
level designates potentially harmful situations.
*/
public static final Level WARN = new Level(WARN_INT, "WARN");
/**
* The INFO
level designates informational messages
* highlighting overall progress of the application.
*/
public static final Level INFO = new Level(INFO_INT, "INFO");
/**
* The DEBUG
level designates informational events of lower
* importance.
*/
public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
/**
* The TRACE
level designates informational events of very low
* importance.
*/
public static final Level TRACE = new Level(TRACE_INT, "TRACE");
/**
* The ALL
is used to turn on all logging.
*/
public static final Level ALL = new Level(ALL_INT, "ALL");
修改后的结果 (logback.xml)
${STARTLOG}##%nanos###%d{yyyyMMddHHmmssSSS}#%contextName|%ip#${AppName}#${AppName}#${AppName}#${AppName}#App#%msg###${ENDTLOG} %n
最后得到的日志格式
BL##INFO###20200804162706981#default|10.45.219.168#cmibs-hjreport#cmibs-hjreport#cmibs-hjreport#cmibs-hjreport#App#Multiple Spring Data modules found, entering strict repository configuration mode!###LB