Skip to content
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

Open
jaronnie opened this issue Apr 19, 2024 · 6 comments
Open

Comments

@jaronnie
Copy link
Contributor

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

@jaronnie
Copy link
Contributor Author

image For example. I want to use docker logs -f . I can not see logs

@fynxiu
Copy link
Contributor

fynxiu commented Apr 20, 2024

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 tee command can be used to direct output to multiple destinations.

@jaronnie
Copy link
Contributor Author

My solution:
When Log.Mode is file or volume. Tail -f logs/*.log

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
		}
	}()
}

@jaronnie
Copy link
Contributor Author

基于这样的解决方案带来的好处是:

  • 日志输出到文件(非常重要)
  • 能在控制台上看到日志(在排查问题或者开发阶段也是很重要的)

在目前 go-zero 只支持一种输出模式下的一个比较好的临时方案。可以参考 https://github.com/jaronnie/jzero/blob/main/daemon/middlewares/logs.go

另外 go-zero 是否会考虑将 Log.Mode 进行扩展,我认为同时输入到文件和控制台是很有必要的。如 Log.Mode = ["console", "file"]

@kevwan

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The benefits of a solution based on this are:

  • Log output to file (very important)
  • Can see logs on the console (also important during troubleshooting or development phase)

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"]

@kevwan

@kevwan
Copy link
Contributor

kevwan commented Apr 24, 2024

Not necessary, you can use tail -f to check the file content. Let's just keep it simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants