For a complete method list, refer to the API documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/glog
The glog
module supports a very convenient chaining operation
method, with the main chaining methods as follows:
// Redirect log output interface
func To(writer io.Writer) *Logger
// Log content output to directory
func Path(path string) *Logger
// Set log file category
func Cat(category string) *Logger
// Set log file format
func File(file string) *Logger
// Set log print level
func Level(level int) *Logger
// Set log print level (string)
func LevelStr(levelStr string) *Logger
// Set file backtrack value
func Skip(skip int) *Logger
// Enable trace printing
func Stack(enabled bool) *Logger
// Enable trace printing with filter string
func StackWithFilter(filter string) *Logger
// Enable terminal output
func Stdout(enabled...bool) *Logger
// Enable log header information
func Header(enabled...bool) *Logger
// Output log line number information
func Line(long...bool) *Logger
// Asynchronous log output
func Async(enabled...bool) *Logger
Example 1, Basic Usage
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)
}
After execution, the output is:
[
"/tmp/glog-cat/cat1",
"/tmp/glog-cat/cat1/cat2",
"/tmp/glog-cat/cat1/cat2/2018-10-10.log",
]
Example 2, Print Call Line Number
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")
}
After execution, the terminal output is:
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
Example 3, File Backtrack Skip
Sometimes we encapsulate the glog
module using some modules to print logs, such as encapsulating a logger
package to print logs via logger.Print
. In this case, the printed call line number is always the same location because, for glog
, its caller is always the logger.Print
method. At this time, we can set the backtrack value to skip the backtracked file count, using SetStackSkip
or the chaining method Skip
.
The setting of the file backtrack value also affects the
Stack
call backtrack print result.
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")
}
After execution, the terminal output is:
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