แอพพลิเคชันไฟล์เรคอร์เดอร์

func main() {
    app := iris.Default()
    // บันทึกลงไฟล์
    // ปิดโทนสีโดยอัตโนมัติเมื่อเขียนลงในไฟล์
    f, _ := os.Create("iris.log")
    app.Logger().SetOutput(f)

    // หากคุณต้องการเขียนบันทึกลงในไฟล์และคอนโซล
    // ให้ใช้โค้ดต่อไปนี้
    // app.Logger().AddOutput(os.Stdout)
    
    app.Get("/ping", func(ctx iris.Context) {
        ctx.WriteString("pong")
    })

   app.Listen(":8080")
}

ควบคุมการแสดงผลสีของบันทึก

โดยค่าเริ่มต้น การแสดงผลบันทึกบนคอนโซลจะถูกพูดให้สีตาม TTY ที่ตรวจจับได้.

ปรับแต่งชื่อระดับ ข้อความ สี และรูปแบบในกรณีธรรมดา

นำเข้า golog และ pio:

import (
    "github.com/kataras/golog"
    "github.com/kataras/pio"
    // [...]
)

ให้รับระดับที่ต้องการปรับแต่ง เช่น DebugLevel:

level := golog.Levels[golog.DebugLevel]

คุณมีการควบคุมทั้งหมดในข้อความระดับ ชื่อ และสไตล์:

// ชื่อระดับ
// ชื่อตัวพิมพ์เล็กจะถูกใช้
// เพื่อแปลงระดับสตริงให้อยู่ในรูปแบบระดับที่ถูกต้อง
Name string
// AlternativeNames คือชื่อทางเลือกที่ระดับบันทึกนี้สามารถอ้างอิงได้
// เช่น Name = "warn"
// AlternativeNames = []string{"warning"}, นี่คือฟิลด์ทางเลือก,
// ดังนั้นเราเก็บ Name เป็นสตริงที่เรียบง่าย และสร้างฟิลด์นี้ขึ้นมาใหม่
AlternativeNames []string
// ชื่อเริ่มต้นคือคำนำของระดับบันทึก
// นอกจากนี้ยังมี `ColorCode` และ `Style`
// `ColorCode` และ `Style` ควรเหมือนกันระหว่างตัวเขียน
Title string
// ColorCode คือสีสำหรับ `Title`
ColorCode int
// Style คือตัวเลือกที่มีคุณภาพสำหรับ `Title`
Style []pio.RichOption

โค้ดตัวอย่าง:

level := golog.Levels[golog.DebugLevel]
level.Name = "debug" // ค่าเริ่มต้น
level.Title = "[DBUG]" // ค่าเริ่มต้น
level.ColorCode = pio.Yellow // ค่าเริ่มต้น

เปลี่ยนรูปแบบการแสดงผล:

app.Logger().SetFormat("json", "    ")

ลงทะเบียนฟอร์แมตเตอร์ที่กำหนดเอง:

app.Logger().RegisterFormatter(new(myFormatter))

อินเทอร์เฟซ golog.Formatter มีดังนี้:

// Formatter รับผิดชอบในการพิมพ์บันทึกไปยังไล็อกเกอร์ของการเขียน
type Formatter interface {
    // ชื่อของฟอร์แมตเตอร์
    String() string
    // ตั้งค่าตัวเลือกใดๆ และส่งคืนอ็อบเจคที่ถูกโคลนไว้ หากเป็นไปได้ ดูที่ `Logger.SetFormat`.
    Options(opts ...interface{}) Formatter
    // พิมพ์ "บันทึก" ไปยัง "dest" ไล็อกเกอร์
    Format(dest io.Writer, log *Log) bool
}

เปลี่ยนการแสดงผลและรูปแบบตามระดับ:

app.Logger().SetLevelOutput("error", os.Stderr)
app.Logger().SetLevelFormat("json")

การบันทึกคำขอ

ตัวบันทึกแอปเปิลเคชันที่เห็นข้างต้นถูกใช้โดยส่วนใหญ่สำหรับการบันทึกข้อมูลและข้อผิดพลาดที่เกี่ยวข้องกับแอปพลิเคชัน ในทางกลับกัน ตัวบันทึกการเข้าถึงที่เราเห็นด้านล่างเสมอถูกใช้เพื่อบันทึกคำขอและการตอบรับ HTTP ขาเข้าโดยหลัก

package main

import (
    "os"

    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/middleware/accesslog"
)

// อ่านตัวอย่างและความคิดเห็นของมันอย่างรอบคอบ
func makeAccessLog() *accesslog.AccessLog {
    // ตั้งค่า middlewares บันทึกการเข้าถึงใหม่
    ac := accesslog.File("./access.log")
    // เอาบรรทัดนี้ออกเพื่อปิดการบันทึกไปที่คอนโซล:
    ac.AddOutput(os.Stdout)

    // ค่าเริ่มต้น:
    ac.Delim = '|'
    ac.TimeFormat = "2006-01-02 15:04:05"
    ac.Async = false
    ac.IP = true
    ac.BytesReceivedBody = true
    ac.BytesSentBody = true
    ac.BytesReceived = false
    ac.BytesSent = false
    ac.BodyMinify = true
    ac.RequestBody = true
    ac.ResponseBody = false
    ac.KeepMultiLineError = true
    ac.PanicLog = accesslog.LogHandler
    
    // ใช้รูปแบบบรรทัดเริ่มต้นถ้าไม่มีการจัดรูปแบบ:
    // Time|Delay|Status|Method|Path|IP|Path Query Parameters|Received Bytes|Sent Bytes|Request|Response|
    //
    // ตั้งค่าการจัดรูปแบบที่กำหนดเอง:
    ac.SetFormatter(&accesslog.JSON{
        Indent:    "  ",
        HumanTime: true,
    })
    // ac.SetFormatter(&accesslog.CSV{})
    // ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})
    
    return ac
}

func main() {
    ac := makeAccessLog()
    defer ac.Close() // ปิดไฟล์

    app := iris.New()
    // ลงทะเบียน middlewares (การใช้ UseRouter ยังสามารถจับข้อผิดพลาด HTTP ได้)
    app.UseRouter(ac.Handler)
    
    app.Get("/", indexHandler)
    
    app.Listen(":8080")
}

func indexHandler(ctx iris.Context) {
    ctx.WriteString("OK")
}

อ่านตัวอย่างเพิ่มเติมได้ที่นี่: _examples/logging/request-logger.