go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

I want to print log to file. But I already want to see it in console.

Open jaronnie opened this issue 1 year ago • 6 comments
trafficstars

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 avatar Apr 19 '24 02:04 jaronnie

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

jaronnie avatar Apr 19 '24 03:04 jaronnie

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.

fynxiu avatar Apr 20 '24 07:04 fynxiu

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 avatar Apr 22 '24 06:04 jaronnie

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

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

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

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

@kevwan

jaronnie avatar Apr 22 '24 06:04 jaronnie

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

Issues-translate-bot avatar Apr 22 '24 06:04 Issues-translate-bot

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

kevwan avatar Apr 24 '24 15:04 kevwan