自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

通過Gin框架對(duì)接阿里云SLS日志服務(wù),查詢服務(wù)日志,你學(xué)會(huì)了嗎?

開發(fā) 前端
日志服務(wù)一站式提供數(shù)據(jù)采集、加工、查詢與分析、可視化、告警、消費(fèi)與投遞等功能,全面提升研發(fā)、運(yùn)維、運(yùn)營(yíng)、安全等場(chǎng)景的數(shù)字化能力。項(xiàng)目代碼

阿里云SLS

日志服務(wù) Simple Log Service 是服務(wù)于阿里云客戶以及阿里云集團(tuán)內(nèi)部自用的云原生觀測(cè)與分析,一站式可觀測(cè)數(shù)據(jù)的 Data to Insight 平臺(tái),為 Log、Metric、Trace 等數(shù)據(jù)提供大規(guī)模、低成本、實(shí)時(shí)的平臺(tái)化服務(wù)。日志服務(wù)一站式提供數(shù)據(jù)采集、加工、查詢與分析、可視化、告警、消費(fèi)與投遞等功能,全面提升研發(fā)、運(yùn)維、運(yùn)營(yíng)、安全等場(chǎng)景的數(shù)字化能力。

項(xiàng)目代碼

package main


import (
  "fmt"
  "github.com/aliyun/aliyun-log-go-sdk"
  "github.com/gin-gonic/gin"
  "net/http"
  "time"
)


func main() {
  router := gin.Default()


  router.GET("/download-logs", func(c *gin.Context) {
    // 地區(qū)
    endpoint := "cn-beijing.log.aliyuncs.com"
    // key
    accessKeyID := "LTAI5tShzYy1VMGR17H"
    // 密鑰
    accessKeySecret := "QELEeTCudJHYhYY3mpuD"


    provider := sls.NewStaticCredentialsProvider(accessKeyID, accessKeySecret, "")
    client := sls.CreateNormalInterfaceV2(endpoint, provider)


    ProjectName := "dean"
    LogStoreName := "nginx"


    logstore, err := client.GetLogStore(ProjectName, LogStoreName)
    if err != nil {
      fmt.Println("出現(xiàn)報(bào)錯(cuò)了")
      panic(err)
    }
    fmt.Println("get logstore successfully:", logstore.Name)


    // 設(shè)置查詢的時(shí)間范圍
    fromTime := time.Now().Unix() - (60 * 60 * 24) // 24小時(shí)之前
    toTime := time.Now().Unix()                    // 當(dāng)前時(shí)間


    // 設(shè)置查詢的參數(shù)
    query := "*"
    line := int64(100) // 每頁(yè)日志條數(shù)
    offset := int64(0) // 偏移量


    // 獲取日志
    resp, err := logstore.GetLogs("", fromTime, toTime, query, line, offset, false)
    if err != nil {
      fmt.Println(err)
      c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get logs"})
      return
    }
    logs := resp.Logs


    // 將所有日志轉(zhuǎn)換為JSON格式返回
    c.JSON(http.StatusOK, logs)


    //// 設(shè)置文件名(可以根據(jù)需要自定義)
    //filename := "access_log_" + time.Now().Format("20060102150405") + ".txt"
    //
    //// 設(shè)置HTTP頭信息,指示這是一個(gè)文件下載響應(yīng)
    //c.Header("Content-Disposition", "attachment; filename=\""+filename+"\"")
    //c.Header("Content-Type", "text/plain")
    //
    //// 寫入文件內(nèi)容
    //c.String(http.StatusOK, logs)


    // 設(shè)置下載相關(guān)的頭部信息
    c.Header("Content-Description", "File Transfer")
    c.Header("Content-Disposition", "attachment; filename=logs.txt")
    c.Header("Content-Type", "application/octet-stream")


    // 將日志內(nèi)容寫入響應(yīng)體
    for _, log := range resp.Logs {
      c.Writer.Write([]byte(fmt.Sprintf("%v\n", log)))
    }


  })


  router.Run(":8080")
}

啟動(dòng)代碼程序:

圖片圖片

訪問Nginx服務(wù),然后查看阿里云SLS日志服務(wù):

圖片圖片

訪問接口:

http://127.0.0.1:8080/download-logs

可以看到接口返回內(nèi)容是Nginx的服務(wù)日志,其他功能自行探索。

圖片圖片

責(zé)任編輯:武曉燕 來(lái)源: 院長(zhǎng)技術(shù)
相關(guān)推薦

2023-11-01 07:28:31

MySQL日志維護(hù)

2024-05-29 09:20:41

2023-11-27 00:55:43

Eureka服務(wù)

2024-01-30 18:29:29

微服務(wù)架構(gòu)Ingress

2022-09-29 08:32:14

查詢語(yǔ)句OR

2024-01-29 00:41:14

2025-04-02 08:03:53

云效賬號(hào)阿里云

2023-06-15 08:00:23

2023-01-10 08:43:15

定義DDD架構(gòu)

2024-02-04 00:00:00

Effect數(shù)據(jù)組件

2023-07-26 13:11:21

ChatGPT平臺(tái)工具

2024-01-19 08:25:38

死鎖Java通信

2024-01-02 12:05:26

Java并發(fā)編程

2023-08-01 12:51:18

WebGPT機(jī)器學(xué)習(xí)模型

2024-03-05 18:10:47

事務(wù)SQL目錄

2024-02-28 07:35:32

SQL查詢數(shù)據(jù)庫(kù)

2024-10-08 08:11:39

2023-07-29 00:10:48

2024-05-06 00:00:00

InnoDBView隔離

2024-08-06 09:47:57

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)