0.10.1: loglevel+traceid

This commit is contained in:
gotoeasy 2023-07-09 08:26:27 +08:00
parent 298fef73d5
commit c6731dc87b
11 changed files with 118 additions and 20 deletions

View File

@ -76,7 +76,7 @@ public class GlcAmqpAppender extends AppenderBase<ILoggingEvent> {
// 异步发送日志
executor.execute(() -> {
sendToRabbitMQ(layout.doLayout(event));
sendToRabbitMQ(layout.doLayout(event), event);
});
}
@ -85,7 +85,7 @@ public class GlcAmqpAppender extends AppenderBase<ILoggingEvent> {
*
* @param text 日志
*/
protected void sendToRabbitMQ(String text) {
protected void sendToRabbitMQ(String text, ILoggingEvent event) {
if (text == null) {
return; // ignore
}
@ -95,11 +95,21 @@ public class GlcAmqpAppender extends AppenderBase<ILoggingEvent> {
initConnectionChannel();
}
String traceid = MdcUtil.getTraceId();
String clientip = MdcUtil.getClientIp();
String body = "{\"text\":" + Util.encodeStr(text.trim());
body += ",\"date\":" + Util.encodeStr(Util.getDateString());
body += ",\"date\":\"" + Util.getDateString() + "\"";
body += ",\"system\":" + Util.encodeStr(getSystem());
body += ",\"servername\":" + Util.encodeStr(Util.getServerName());
body += ",\"serverip\":" + Util.encodeStr(Util.getServerIp());
body += ",\"loglevel\":\"" + event.getLevel().toString() + "\"";
if (!"".equals(traceid)) {
body += ",\"traceid\":" + Util.encodeStr(traceid);
}
if (!"".equals(clientip)) {
body += ",\"clientip\":" + Util.encodeStr(clientip);
}
body += "}";
channel.basicPublish("", "glc-log-queue", null, body.getBytes("utf-8"));

View File

@ -46,7 +46,7 @@ public class GlcHttpJsonAppender extends AppenderBase<ILoggingEvent> {
// 异步发送日志到GLC
executor.execute(() -> {
submitToGlogCenter(layout.doLayout(event));
submitToGlogCenter(layout.doLayout(event), event);
});
}
@ -57,7 +57,7 @@ public class GlcHttpJsonAppender extends AppenderBase<ILoggingEvent> {
*
* @param text 日志
*/
protected void submitToGlogCenter(String text) {
protected void submitToGlogCenter(String text, ILoggingEvent event) {
if (text == null) {
return; // ignore
}
@ -65,11 +65,21 @@ public class GlcHttpJsonAppender extends AppenderBase<ILoggingEvent> {
DataOutputStream dos = null;
String body = null;
try {
String traceid = MdcUtil.getTraceId();
String clientip = MdcUtil.getClientIp();
body = "{\"text\":" + Util.encodeStr(text.trim());
body += ",\"date\":" + Util.encodeStr(Util.getDateString());
body += ",\"date\":\"" + Util.getDateString() + "\"";
body += ",\"system\":" + Util.encodeStr(getSystem());
body += ",\"servername\":" + Util.encodeStr(Util.getServerName());
body += ",\"serverip\":" + Util.encodeStr(Util.getServerIp());
body += ",\"loglevel\":\"" + event.getLevel().toString() + "\"";
if (!"".equals(traceid)) {
body += ",\"traceid\":" + Util.encodeStr(traceid);
}
if (!"".equals(clientip)) {
body += ",\"clientip\":" + Util.encodeStr(clientip);
}
body += "}";
URL url = new URL(glcApiUrl);

View File

@ -0,0 +1,46 @@
package top.gotoeasy.framework.glc.logback.appender;
import java.util.UUID;
import org.slf4j.MDC;
public class MdcUtil {
public static final String TRACE_ID = "traceid";
public static final String CLIENT_IP = "clientip";
public static String getClientIp() {
String str = MDC.get(CLIENT_IP);
return str == null ? "" : str;
}
public static String getTraceId() {
String str = MDC.get(TRACE_ID);
return str == null ? "" : str;
}
public static void setClientIp(String clientip) {
MDC.put(CLIENT_IP, clientip);
}
public static void setTraceId(String traceid) {
MDC.put(TRACE_ID, traceid);
}
public static void removeClientIp() {
MDC.remove(CLIENT_IP);
}
public static void removeTraceId() {
MDC.remove(TRACE_ID);
}
public static void clear() {
MDC.clear();
}
public static String generateTraceId() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}

View File

@ -1,7 +1,6 @@
package top.gotoeasy.framework.glc.logback.appender;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.CharacterIterator;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
@ -16,7 +15,7 @@ public class Util {
if ("".equals(serverName)) {
try {
serverName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
} catch (Exception e) {
// ignore
}
}
@ -27,7 +26,7 @@ public class Util {
if ("".equals(serverIp)) {
try {
serverIp = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
} catch (Exception e) {
// ignore
}
}

View File

@ -48,7 +48,8 @@ func (e *Engine) AddLogDataModel(data *logdata.LogDataModel) {
e.logStorage.AddLogDataModel(data)
}
func (e *Engine) Search(searchKey string, system string, minDatetime string, maxDatetime string, pageSize int, currentDocId uint32, forward bool) *search.SearchResult {
func (e *Engine) Search(searchKey string, system string, minDatetime string, maxDatetime string, loglevel string, traceid string,
pageSize int, currentDocId uint32, forward bool) *search.SearchResult {
// 检查修正pageSize
if pageSize < 1 {
@ -60,7 +61,7 @@ func (e *Engine) Search(searchKey string, system string, minDatetime string, max
// 分词后检索
var adds []string
adds = append(adds, system)
adds = append(adds, system, loglevel, traceid)
kws := tokenizer.CutForSearchEx(searchKey, adds, nil) // 检索用关键词处理
if searchKey == "" {

View File

@ -46,7 +46,7 @@ func Test_all(t *testing.T) {
// }
// time.Sleep(time.Duration(5) * time.Second)
rs := engine.Search(` them java `, "", "", "", 5, 0, true)
rs := engine.Search(` them java `, "", "", "", "", "", 5, 0, true)
cmn.Println("共查到", rs.Total, "件")
for _, v := range rs.Data {
cmn.Println(v.Id, v.Text)

View File

@ -23,6 +23,7 @@ type LogDataModel struct {
ClientIp string `json:"client,omitempty"` // 客户端IP
TraceId string `json:"traceid,omitempty"` // 跟踪ID
LogType string `json:"logtype,omitempty"` // 日志类型1:登录日志、2:操作日志)
LogLevel string `json:"loglevel,omitempty"` // 日志级别debug、info、error等
User string `json:"user,omitempty"` // 用户
Module string `json:"module,omitempty"` // 模块
Operation string `json:"action,omitempty"` // 操作

View File

@ -213,8 +213,11 @@ func (s *LogDataStorage) createInvertedIndex() int {
if docm.Operation != "" {
adds = append(adds, "*"+docm.Operation)
}
if docm.LogLevel != "" {
adds = append(adds, "-"+docm.LogLevel)
}
kws := tokenizer.CutForSearchEx(docm.Text+" "+docm.System+" "+docm.ServerName+" "+docm.ServerIp+
" "+docm.ClientIp+" "+docm.TraceId+" "+docm.User+" "+docm.Module+" "+docm.Operation, adds, docm.Sensitives) // 两数组参数的元素可以重复或空白,会被判断整理
" "+docm.ClientIp+" "+docm.TraceId+" "+docm.LogLevel+" "+docm.User+" "+docm.Module+" "+docm.Operation, adds, docm.Sensitives) // 两数组参数的元素可以重复或空白,会被判断整理
// 每个关键词都创建反向索引
for _, word := range kws {

View File

@ -1,3 +1,3 @@
package onstart
const VERSION = "glogcenter 0.10.0"
const VERSION = "glogcenter 0.10.1"

View File

@ -24,12 +24,20 @@ func LogSearchController(req *gweb.HttpRequest) *gweb.HttpResult {
datetimeFrom := req.GetFormParameter("datetimeFrom")
datetimeTo := req.GetFormParameter("datetimeTo")
system := req.GetFormParameter("system")
loglevel := req.GetFormParameter("loglevel")
traceid := req.GetFormParameter("traceid")
if !cmn.IsBlank(system) {
system = "~" + cmn.Trim(system)
}
if !cmn.IsBlank(traceid) {
traceid = "$" + cmn.Trim(traceid)
}
if !cmn.IsBlank(loglevel) {
loglevel = "-" + cmn.Trim(loglevel)
}
eng := ldb.NewEngine(storeName)
rs := eng.Search(searchKey, system, datetimeFrom, datetimeTo, pageSize, currentId, forward)
rs := eng.Search(searchKey, system, datetimeFrom, datetimeTo, loglevel, traceid, pageSize, currentId, forward)
return gweb.Result(rs)
}

View File

@ -10,7 +10,7 @@
<div style="display:flex;justify-content:space-between;width:100%">
<div>
<el-input @keyup.enter="search()" v-model="params.searchKey" placeholder="请输入关键词检索" style="width:600px;">
<el-input @keyup.enter="search()" v-model="params.searchKey" placeholder="请输入关键词检索,支持多关键词" style="width:600px;">
<template #append>
<el-button type="primary" @click="search" class="x-search">
<el-icon>
@ -51,7 +51,7 @@
</el-select>
</el-form-item>
</el-row>
<el-row>
<el-row>
<el-form-item label="系统名">
<el-select v-model="params.system" style="width:420px;" :multiple="false" filterable allow-create default-first-option clearable
:reserve-keyword="true" placeholder="请输入系统名">
@ -59,6 +59,17 @@
</el-select>
</el-form-item>
</el-row>
<el-row>
<el-form-item label="日志级别">
<el-select v-model="params.loglevel" style="width:420px;" :multiple="false" clearable
:reserve-keyword="true" placeholder="请选择...">
<el-option label="ERROR" value="error"/>
<el-option label="WARN" value="warning"/>
<el-option label="INFO" value="info"/>
<el-option label="DEBUG" value="debug"/>
</el-select>
</el-form-item>
</el-row>
<el-row>
<el-form-item label="时间范围">
<el-date-picker v-model="params.datetime" type="datetimerange" :shortcuts="shortcuts"
@ -99,6 +110,8 @@
<el-dropdown-item @click="switchVisible('servername')">{{serverNameVisible?'隐藏':'显示'}}主机名</el-dropdown-item>
<el-dropdown-item @click="switchVisible('serverip')">{{serverIpVisible?'隐藏':'显示'}}主机IP</el-dropdown-item>
<el-dropdown-item @click="switchVisible('date')">{{dateVisible?'隐藏':'显示'}}日期时间</el-dropdown-item>
<el-dropdown-item @click="switchVisible('loglevel')">{{logLevelVisible?'隐藏':'显示'}}Level</el-dropdown-item>
<el-dropdown-item @click="switchVisible('traceid')">{{traceIdVisible?'隐藏':'显示'}}TraceID</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
@ -125,6 +138,8 @@
<el-table-column v-if="serverNameVisible" prop="servername" label="主机名" width="180" :show-overflow-tooltip="true"/>
<el-table-column v-if="serverIpVisible" prop="serverip" label="主机IP" width="130" :show-overflow-tooltip="true"/>
<el-table-column v-if="dateVisible" prop="date" label="日期时间" width="208" :show-overflow-tooltip="true"/>
<el-table-column v-if="logLevelVisible" prop="loglevel" label="Level" width="75" :show-overflow-tooltip="true"/>
<el-table-column v-if="traceIdVisible" prop="traceid" label="TraceId" width="300" :show-overflow-tooltip="true"/>
<el-table-column prop="text" label="日志" :show-overflow-tooltip="true">
<template #default="scope">
<span v-text="scope.row.text"></span>
@ -155,7 +170,6 @@ import { Search, RefreshLeft, ArrowUp, ArrowDown, Check } from '@element-plus/ic
const FixHeight = 215 // 177
export default {
// name: 'dashboard',
components: { },
data() {
return {
@ -164,6 +178,7 @@ export default {
params: {
storeName: '',
searchKey: '',
loglevel: '',
system: '',
systems: [],
datetime: null,
@ -175,6 +190,8 @@ export default {
systemVisible: true,
serverNameVisible: false,
serverIpVisible: false,
logLevelVisible: true,
traceIdVisible: false,
dateVisible: true,
info: '',
storage: ref(''),
@ -304,7 +321,7 @@ export default {
},
computed:{
hasMoreCondition(){
return !this.params.datetime && !this.storage && !this.params.system;
return !this.params.datetime && !this.storage && !this.params.system && !this.params.loglevel;
}
},
methods: {
@ -312,11 +329,14 @@ export default {
name == 'system' && (this.systemVisible = !this.systemVisible);
name == 'servername' && (this.serverNameVisible = !this.serverNameVisible);
name == 'serverip' && (this.serverIpVisible = !this.serverIpVisible);
name == 'loglevel' && (this.logLevelVisible = !this.logLevelVisible);
name == 'traceid' && (this.traceIdVisible = !this.traceIdVisible);
name == 'date' && (this.dateVisible = !this.dateVisible);
},
fnResetSearchForm(){
this.params.searchKey = '';
this.params.system = '';
this.params.loglevel = '';
this.params.datetime = null;
this.storage = '';
this.search();
@ -365,7 +385,7 @@ export default {
this.params.currentId = ''
this.params.datetimeFrom = (this.params.datetime || ['', ''])[0]
this.params.datetimeTo = (this.params.datetime || ['', ''])[1]
// console.info("----------this.params",this.params)
// console.info("----------this.params",this.params)
api.search(this.params).then(rs => {
let res = rs.data
if (res.success) {