mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-17 08:25:53 +08:00
规则脚本增加日志
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="/logs" />
|
||||
<property name="log.path" value="D:/logs" />
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
|
||||
|
||||
@@ -95,6 +95,24 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 规则引擎日志输出 -->
|
||||
<appender name="script" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/rule/script.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 按天回滚 daily -->
|
||||
<fileNamePattern>${log.path}/rule/script.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 3天 -->
|
||||
<maxHistory>3</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%method,%line] - %msg%n</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>INFO</level>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
|
||||
<!-- 系统模块日志级别控制 -->
|
||||
<logger name="com.fastbee" level="debug" />
|
||||
<!-- Spring日志级别控制 -->
|
||||
@@ -115,4 +133,10 @@
|
||||
<logger name="sys-user" level="info">
|
||||
<appender-ref ref="sys-user"/>
|
||||
</logger>
|
||||
|
||||
<!--规则引擎日志-->
|
||||
<logger name="script" level="info">
|
||||
<appender-ref ref="script"/>
|
||||
</logger>
|
||||
|
||||
</configuration>
|
||||
|
||||
@@ -85,6 +85,16 @@ public class ScriptController extends BaseController
|
||||
return toAjax(scriptService.updateRuleScript(ruleScript));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取规则引擎脚本日志
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('iot:script:query')")
|
||||
@GetMapping(value = "/log/{scriptId}")
|
||||
public AjaxResult getScriptLog(@PathVariable("scriptId") String scriptId) {
|
||||
return success(scriptService.selectRuleScriptLog("script", scriptId));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除规则引擎脚本
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@@ -11,11 +13,14 @@ import lombok.NoArgsConstructor;
|
||||
@AllArgsConstructor
|
||||
public class MsgContext {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("script");
|
||||
|
||||
/** 消息主题 */
|
||||
private String topic;
|
||||
|
||||
/** 消息内容 */
|
||||
private String payload;
|
||||
|
||||
/**
|
||||
* 设备编号
|
||||
*/
|
||||
|
||||
@@ -22,6 +22,14 @@ public interface IScriptService
|
||||
*/
|
||||
public Script selectRuleScriptById(String scriptId);
|
||||
|
||||
/**
|
||||
* 查询规则引擎脚本日志
|
||||
*
|
||||
* @param id 规则引擎脚本主键
|
||||
* @return 规则引擎脚本
|
||||
*/
|
||||
public String selectRuleScriptLog(String type, String id);
|
||||
|
||||
/**
|
||||
* 查询规则引擎脚本列表
|
||||
*
|
||||
|
||||
@@ -12,10 +12,18 @@ import com.fastbee.iot.model.ScriptCondition;
|
||||
import com.fastbee.iot.service.IScriptService;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.script.ScriptExecutorFactory;
|
||||
import org.apache.commons.io.input.ReversedLinesFileReader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -50,6 +58,38 @@ public class ScriptServiceImpl implements IScriptService
|
||||
return ruleScriptMapper.selectRuleScriptById(scriptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询规则引擎脚本日志
|
||||
*
|
||||
* @param id 规则引擎脚本主键
|
||||
* @return 规则引擎脚本
|
||||
*/
|
||||
|
||||
@Override
|
||||
public String selectRuleScriptLog(String type, String id) {
|
||||
// 获取日志存储路径
|
||||
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
String path = loggerContext.getProperty("log.path");
|
||||
|
||||
// 倒叙读取500条日志
|
||||
try {
|
||||
List<String> lines = new ArrayList<>();
|
||||
ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(path + "/rule/" + type + ".log"));
|
||||
String line = "";
|
||||
while ((line = reader.readLine()) != null && lines.size() < 500) {
|
||||
String requestId = type + "/" + id;
|
||||
if (line.contains(requestId)) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
Collections.reverse(lines);
|
||||
return String.join("\n", lines);
|
||||
} catch (IOException e) {
|
||||
return "暂无日志,详情如下:\n" + e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询规则引擎脚本列表
|
||||
*
|
||||
|
||||
@@ -17,6 +17,15 @@ export function getScript(scriptId) {
|
||||
});
|
||||
}
|
||||
|
||||
// 查询规则引擎脚本日志
|
||||
export function getScriptLog(scriptId) {
|
||||
return request({
|
||||
url: '/iot/script/log/' + scriptId,
|
||||
method: 'get',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 新增规则引擎脚本
|
||||
export function addScript(data) {
|
||||
return request({
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
<el-table-column label="执行顺序" align="center" prop="scriptOrder" />
|
||||
<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
|
||||
<template slot-scope="scope">
|
||||
<el-button size="small" type="text" icon="el-icon-date" @click="handleLog(scope.row.scriptId)" v-hasPermi="['iot:script:query']">日志</el-button>
|
||||
<el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)"
|
||||
v-hasPermi="['iot:script:query']">查看</el-button>
|
||||
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
|
||||
@@ -140,13 +141,33 @@
|
||||
</div>
|
||||
</el-dialog>
|
||||
</el-card>
|
||||
|
||||
<el-dialog :title="title" :visible.sync="openLog" width="700px" append-to-body :close-on-click-modal="false" :close-on-press-escape="false">
|
||||
<div
|
||||
ref="logContainer"
|
||||
v-loading="logLoading"
|
||||
:element-loading-text="加载中"
|
||||
element-loading-spinner="el-icon-loading"
|
||||
element-loading-background="rgba(0, 0, 0, 0.8)"
|
||||
style="border: 1px solid #ccc; border-radius: 4px; height: 450px; background-color: #181818; color: #fff; padding: 10px; line-height: 20px; overflow: auto"
|
||||
>
|
||||
<pre>
|
||||
{{ logs }}
|
||||
</pre
|
||||
>
|
||||
</div>
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button @click="cancelLog">关闭</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
||||
<!-- 产品列表 -->
|
||||
<productList ref="productList" @productEvent="getSelectProduct($event)"></productList>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { listScript, getScript, delScript, addScript, updateScript, validateScript } from '@/api/iot/script';
|
||||
import { listScript, getScript, getScriptLog, delScript, addScript, updateScript, validateScript } from '@/api/iot/script';
|
||||
import AceEditor from '@/views/components/editor/editor.vue';
|
||||
import productList from './product-list';
|
||||
export default {
|
||||
@@ -167,6 +188,12 @@ export default {
|
||||
}
|
||||
};
|
||||
return {
|
||||
// 日志
|
||||
logs: '',
|
||||
// 日志遮罩层
|
||||
logLoading: true,
|
||||
// 是否显示日志弹窗
|
||||
openLog: false,
|
||||
// 脚本数据验证
|
||||
isValidate: false,
|
||||
// 脚本数据验证结果
|
||||
@@ -235,6 +262,11 @@ export default {
|
||||
this.open = false;
|
||||
this.reset();
|
||||
},
|
||||
// 取消日志按钮
|
||||
cancelLog() {
|
||||
this.logs = '';
|
||||
this.openLog = false;
|
||||
},
|
||||
// 表单重置
|
||||
reset() {
|
||||
this.validateMsg = '';
|
||||
@@ -266,7 +298,7 @@ String payload = msgContext.getPayload();
|
||||
|
||||
|
||||
// 2. 数据转换(自己处理)
|
||||
println ("根据情况转换数据")
|
||||
msgContext.logger.debug("数据转换处理")
|
||||
String NewTopic = topic;
|
||||
String NewPayload = payload;
|
||||
|
||||
@@ -309,6 +341,29 @@ msgContext.setPayload(NewPayload);`,
|
||||
this.title = '修改规则引擎脚本';
|
||||
});
|
||||
},
|
||||
/** 日志按钮操作 */
|
||||
handleLog(scriptId) {
|
||||
this.logLoading = true;
|
||||
getScriptLog(scriptId).then((response) => {
|
||||
this.logs = response.msg;
|
||||
this.form.scriptId = scriptId;
|
||||
this.openLog = true;
|
||||
this.title = "脚本日志";
|
||||
this.logLoading = false;
|
||||
// 滑动到底部
|
||||
this.$nextTick(function () {
|
||||
let messageContent = this.$refs.logContainer;
|
||||
messageContent.scroll({
|
||||
top: messageContent.scrollHeight,
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
/** 日志刷新操作 */
|
||||
refreshLog() {
|
||||
this.handleLog(this.form.scriptId);
|
||||
},
|
||||
|
||||
/**选择产品 */
|
||||
handleSelectProduct(data) {
|
||||
// 刷新子组建
|
||||
|
||||
Reference in New Issue
Block a user