---
title: "日志组件-链式操作"
url: "https://goframe.org/docs/core/glog-chaining"
markdown_url: "https://goframe.org/docs/core/glog-chaining.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "核心组件"
category: "核心组件 / 日志组件"
published: "2024-10-21"
updated: "2026-02-11"
description: "详细介绍`GoFrame`框架日志组件的链式操作功能，支持通过一系列链式方法灵活设置日志输出参数。包括设置输出目录、文件分类、日志级别、开启堆栈打印、终端输出、异步输出等功能。通过多个实际代码示例展示了如何使用`Path`、`Cat`、`Line`、`Skip`等链式方法组合实现灵活的日志管理，包括文件回溯设置、调用行号打印等高级功能。"
keywords: ["GoFrame", "GoFrame框架", "日志组件", "链式操作", "glog", "日志输出", "日志级别", "文件回溯", "终端输出", "异步日志"]
---

# 日志组件-链式操作

完整的方法列表可参考接口文档：[https://pkg.go.dev/github.com/gogf/gf/v2/os/glog](https://pkg.go.dev/github.com/gogf/gf/v2/os/glog)

`glog`模块支持非常简便的`链式操作`方式，主要的链式操作方法如下：

```go
// 重定向日志输出接口
func To(writer io.Writer) *Logger
// 日志内容输出到目录
func Path(path string) *Logger
// 设置日志文件分类
func Cat(category string) *Logger
// 设置日志文件格式
func File(file string) *Logger
// 设置日志打印级别
func Level(level int) *Logger
// 设置日志打印级别(字符串)
func LevelStr(levelStr string) *Logger
// 设置文件回溯值
func Skip(skip int) *Logger
// 是否开启trace打印
func Stack(enabled bool) *Logger
// 开启trace打印并设定过滤trace的字符串
func StackWithFilter(filter string) *Logger
// 是否开启终端输出
func Stdout(enabled...bool) *Logger
// 是否输出日志头信息
func Header(enabled...bool) *Logger
// 输出日志调用行号信息
func Line(long...bool) *Logger
// 异步输出日志
func Async(enabled...bool) *Logger
```

## 示例1, 基本使用

```go
package main

import (
    "context"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gfile"
)

func main() {
    ctx := context.TODO()
    path := "/tmp/glog-cat"
    g.Log().SetPath(path)
    g.Log().Stdout(false).Cat("cat1").Cat("cat2").Print(ctx, "test")
    list, err := gfile.ScanDir(path, "*", true)
    g.Dump(err)
    g.Dump(list)
}
```

执行后，输出结果为：

```javascriprt
[
    "/tmp/glog-cat/cat1",
    "/tmp/glog-cat/cat1/cat2",
    "/tmp/glog-cat/cat1/cat2/2018-10-10.log",
]
```

## 示例2, 打印调用行号

```go
package main

import (
    "context"

    "github.com/gogf/gf/v2/frame/g"
)

func main() {
    ctx := context.TODO()
    g.Log().Line().Print(ctx, "this is the short file name with its line number")
    g.Log().Line(true).Print(ctx, "lone file name with line number")
}
```

执行后，终端输出结果为：

```html
2019-05-23 09:22:58.141 glog_line.go:8: this is the short file name with its line number
2019-05-23 09:22:58.142 /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/os/glog/glog_line.go:9: lone file name with line number
```

## 示例3, 文件回溯 `Skip`

有时我们通过一些模块封装了 `glog` 模块来打印日志，例如封装了一个 `logger` 包通过 `logger.Print` 来打印日志，这个时候打印出来的调用文件行号总是同一个位置，因为对于 `glog` 来讲，它的调用方即总是 `logger.Print` 方法。这个时候，我们可以通过设置回溯值来跳过回溯的文件数，使用 `SetStackSkip` 或者链式方法 `Skip` 实现。

> 文件回溯值的设置同样也会影响 `Stack` 调用回溯打印结果。

```go
package main

import (
    "context"

    "github.com/gogf/gf/v2/frame/g"
)

func PrintLog(ctx context.Context, content string) {
    g.Log().Skip(1).Line().Print(ctx, "line number with skip:", content)
    g.Log().Line().Print(ctx, "line number without skip:", content)
}

func main() {
    ctx := context.TODO()
    PrintLog(ctx, "just test")
}
```

执行后，终端输出结果为：

```html
2019-05-23 19:30:10.984 glog_line2.go:13: line number with skip: just test
2019-05-23 19:30:10.984 glog_line2.go:9: line number without skip: just test
```
