New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I want to print log to file. But I already want to see it in console. #4093
Comments
If you want your application to log information both to the console and to a file simultaneously, you can achieve this using Unix shell commands. Particularly, the |
My solution: package middlewares
import (
"fmt"
"io"
"path/filepath"
"github.com/nxadm/tail"
"github.com/zeromicro/go-zero/core/mr"
"github.com/jaronnie/jzero/daemon/internal/config"
)
func PrintLogToConsole(c config.Config) {
if c.Log.Mode == "console" {
return
}
logs := []string{"access.log", "error.log", "severe.log", "slow.log"}
if c.Log.Stat {
logs = append(logs, "stat.log")
}
var logPaths []string
for _, v := range logs {
logPaths = append(logPaths, filepath.Join(c.Log.Path, v))
}
go func() {
err := mr.MapReduceVoid(func(source chan<- string) {
for _, v := range logPaths {
source <- v
}
}, func(item string, writer mr.Writer[*tail.Tail], cancel func(error)) {
t, err := tail.TailFile(
filepath.Join(item), tail.Config{
Follow: true,
ReOpen: true,
Poll: true,
Location: &tail.SeekInfo{
Offset: 0,
Whence: io.SeekEnd,
},
Logger: tail.DiscardingLogger,
})
if err != nil {
cancel(err)
}
// Print the text of each received line
for line := range t.Lines {
fmt.Println(line.Text)
}
}, func(pipe <-chan *tail.Tail, cancel func(error)) {}, mr.WithWorkers(len(logs)))
if err != nil {
return
}
}()
} |
基于这样的解决方案带来的好处是:
在目前 go-zero 只支持一种输出模式下的一个比较好的临时方案。可以参考 https://github.com/jaronnie/jzero/blob/main/daemon/middlewares/logs.go 另外 go-zero 是否会考虑将 Log.Mode 进行扩展,我认为同时输入到文件和控制台是很有必要的。如 Log.Mode = ["console", "file"] |
The benefits of a solution based on this are:
At present, go-zero only supports one output mode, which is a better temporary solution. You can refer to https://github.com/jaronnie/jzero/blob/main/daemon/middlewares/logs.go In addition, will go-zero consider extending Log.Mode? I think it is necessary to input it to the file and console at the same time. Such as Log.Mode = ["console", "file"] |
Not necessary, you can use |
Is your feature request related to a problem? Please describe.
I want to print log to file. But I already want to see it in console.
But now I set mode is file, I can not see it in console
Describe the solution you'd like
Log.Mode "console,file"
support many
The text was updated successfully, but these errors were encountered: