Skip to content

Commit 35ea1a1

Browse files
authored
feat: Show container log timestamps (#11785)
1 parent b0a053b commit 35ea1a1

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

agent/app/api/v2/container.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func (b *BaseApi) DownloadContainerLogs(c *gin.Context) {
529529
if err := helper.CheckBindAndValidate(&req, c); err != nil {
530530
return
531531
}
532-
err := containerService.DownloadContainerLogs(req.ContainerType, req.Container, req.Since, strconv.Itoa(int(req.Tail)), c)
532+
err := containerService.DownloadContainerLogs(req.ContainerType, req.Container, req.Since, strconv.Itoa(int(req.Tail)), req.Timestamp, c)
533533
if err != nil {
534534
helper.InternalServer(c, err)
535535
}
@@ -757,6 +757,7 @@ func (b *BaseApi) LoadComposeEnv(c *gin.Context) {
757757
// @Param since query string false "时间筛选"
758758
// @Param follow query string false "是否追踪"
759759
// @Param tail query string false "显示行号"
760+
// @Param timestamp query string false "是否显示时间"
760761
// @Success 200
761762
// @Security ApiKeyAuth
762763
// @Security Timestamp
@@ -770,6 +771,7 @@ func (b *BaseApi) ContainerStreamLogs(c *gin.Context) {
770771
since := c.Query("since")
771772
follow := c.Query("follow") == "true"
772773
tail := c.Query("tail")
774+
timestamp := c.Query("timestamp") == "true"
773775

774776
container := c.Query("container")
775777
compose := c.Query("compose")
@@ -779,6 +781,7 @@ func (b *BaseApi) ContainerStreamLogs(c *gin.Context) {
779781
Since: since,
780782
Follow: follow,
781783
Tail: tail,
784+
Timestamp: timestamp,
782785
Type: "container",
783786
}
784787
if compose != "" {

agent/app/dto/container.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ type ContainerLog struct {
305305
Container string `json:"container" validate:"required"`
306306
Since string `json:"since"`
307307
Tail uint `json:"tail"`
308+
Timestamp bool `json:"timestamp"`
308309
ContainerType string `json:"containerType"`
309310
}
310311

@@ -314,5 +315,6 @@ type StreamLog struct {
314315
Since string
315316
Follow bool
316317
Tail string
318+
Timestamp bool
317319
Type string
318320
}

agent/app/service/container.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type IContainerService interface {
8080
ContainerCommit(req dto.ContainerCommit) error
8181
ContainerLogClean(req dto.OperationWithName) error
8282
ContainerOperation(req dto.ContainerOperation) error
83-
DownloadContainerLogs(containerType, container, since, tail string, c *gin.Context) error
83+
DownloadContainerLogs(containerType, container, since, tail string, timestamp bool, c *gin.Context) error
8484
ContainerStats(id string) (*dto.ContainerStats, error)
8585

8686
Inspect(req dto.InspectReq) (string, error)
@@ -956,6 +956,9 @@ func collectLogs(done <-chan struct{}, params dto.StreamLog, messageChan chan<-
956956
if params.Follow {
957957
cmdArgs = append(cmdArgs, "-f")
958958
}
959+
if params.Timestamp {
960+
cmdArgs = append(cmdArgs, "-t")
961+
}
959962
if params.Tail != "0" {
960963
cmdArgs = append(cmdArgs, "--tail", params.Tail)
961964
}
@@ -1052,7 +1055,7 @@ func collectLogs(done <-chan struct{}, params dto.StreamLog, messageChan chan<-
10521055
_ = dockerCmd.Wait()
10531056
}
10541057

1055-
func (u *ContainerService) DownloadContainerLogs(containerType, container, since, tail string, c *gin.Context) error {
1058+
func (u *ContainerService) DownloadContainerLogs(containerType, container, since, tail string, timestamp bool, c *gin.Context) error {
10561059
if cmd.CheckIllegal(container, since, tail) {
10571060
return buserr.New("ErrCmdIllegal")
10581061
}
@@ -1080,6 +1083,9 @@ func (u *ContainerService) DownloadContainerLogs(containerType, container, since
10801083
commandArg = append(commandArg, "--since")
10811084
commandArg = append(commandArg, since)
10821085
}
1086+
if timestamp {
1087+
commandArg = append(commandArg, "-t")
1088+
}
10831089
var dockerCmd *exec.Cmd
10841090
if containerType == "compose" && dockerCommand == "docker-compose" {
10851091
dockerCmd = exec.Command("docker-compose", commandArg...)

core/cmd/server/docs/docs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,6 +4594,12 @@ const docTemplate = `{
45944594
"in": "query",
45954595
"name": "tail",
45964596
"type": "string"
4597+
},
4598+
{
4599+
"description": "是否显示时间",
4600+
"in": "query",
4601+
"name": "timestamp",
4602+
"type": "string"
45974603
}
45984604
],
45994605
"responses": {

core/cmd/server/docs/swagger.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,6 +4590,12 @@
45904590
"in": "query",
45914591
"name": "tail",
45924592
"type": "string"
4593+
},
4594+
{
4595+
"description": "是否显示时间",
4596+
"in": "query",
4597+
"name": "timestamp",
4598+
"type": "string"
45934599
}
45944600
],
45954601
"responses": {

frontend/src/components/log/container/index.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
{{ $t('commons.button.watch') }}
1818
</el-checkbox>
1919
</div>
20+
<div class="margin-button float-left">
21+
<el-checkbox border @change="searchLogs" v-model="logSearch.isShowTimestamp">
22+
{{ $t('commons.table.date') }}
23+
</el-checkbox>
24+
</div>
2025
<el-button class="margin-button" @click="onDownload" icon="Download">
2126
{{ $t('commons.button.download') }}
2227
</el-button>
@@ -79,6 +84,10 @@ const props = defineProps({
7984
type: Boolean,
8085
default: false,
8186
},
87+
defaultIsShowTimestamp: {
88+
type: Boolean,
89+
default: false,
90+
},
8291
});
8392
8493
const styleVars = computed(() => ({
@@ -91,6 +100,7 @@ const logs = ref<string[]>([]);
91100
let eventSource: EventSource | null = null;
92101
const logSearch = reactive({
93102
isWatch: props.defaultFollow ? true : true,
103+
isShowTimestamp: props.defaultIsShowTimestamp,
94104
container: '',
95105
mode: 'all',
96106
tail: props.defaultFollow ? 0 : 100,
@@ -152,9 +162,9 @@ const searchLogs = async () => {
152162
if (props.node && props.node !== '') {
153163
currentNode = props.node;
154164
}
155-
let url = `/api/v2/containers/search/log?container=${logSearch.container}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}&operateNode=${currentNode}`;
165+
let url = `/api/v2/containers/search/log?container=${logSearch.container}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}&timestamp=${logSearch.isShowTimestamp}&operateNode=${currentNode}`;
156166
if (logSearch.compose !== '') {
157-
url = `/api/v2/containers/search/log?compose=${logSearch.compose}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}&operateNode=${currentNode}`;
167+
url = `/api/v2/containers/search/log?compose=${logSearch.compose}&since=${logSearch.mode}&tail=${logSearch.tail}&follow=${logSearch.isWatch}&timestamp=${logSearch.isShowTimestamp}&operateNode=${currentNode}`;
158168
}
159169
eventSource = new EventSource(url);
160170
eventSource.onmessage = (event: MessageEvent) => {
@@ -192,6 +202,7 @@ const onDownload = async () => {
192202
container: container,
193203
since: logSearch.mode,
194204
tail: logSearch.tail,
205+
timestamp: logSearch.isShowTimestamp,
195206
containerType: containerType,
196207
};
197208
let addItem = {};

0 commit comments

Comments
 (0)