Skip to content

How to set up independent logging #4968

@hide-in-code

Description

@hide-in-code

How to set up independent logging

This is a help request (maybe also a framework system issue).

The situation is as follows: I use go-zero to develop business logic. As the business grows and expands, the daily log volume is also constantly increasing. However, for me personally, I may be more concerned with the related requests of one or two core business data tables. Therefore, I hope to separate the corresponding logs from access.log for easier analysis. For example: for a payment business, I care more about the amount flow-related content. I hope to have an independent cash.log that allows me to write independently, and this log's retention period is also different from other logs' retention periods.

I reviewed the logx-related code and found that logx settings are global, and logx's setup is forcibly set to 1 time, and cannot be modified elsewhere. The specific code should be here:

setupOnce.Do(func() {

I don't know how everyone solves this problem.

By the way, I know that the following method can be used to redirect logs:

writer := logx.NewWriter(file)
logx.SetWriter(writer)

But this approach is not elegant, and the impact it brings is global. I have experimented with it, and in extreme cases, logs will appear chaotic and mixed up.


如何设置独立日志

这是一个求助问题(也许算是框架系统问题)
情况是这样的,我使用go zero开发业务,随着业务的增加和扩张,每天的日志量也在不断变大,然而,对于我个人而言更多的可能更加关注核心业务本身的一两个数据表的相关请求,因此我希望将对应的日志从access.log中独立出来,以方便分析,举个例子:比如一个支付业务,我更关心金额流水相关的内容,我希望能有一个独立的cash.log能允许我独立写入,这个日志的留存周期与其他日志的留存周期也不一样。

我翻阅了logx相关的代码,我发现logx的设置是全局的,并且logx的setup被强制设置为1次,其他地方无法修改,具体代码应该是这里:

setupOnce.Do(func() {

不知道大家是如何解决这个问题的。

顺带说明一下,我知道可以使用如下的这种办法来重定向日志

writer := logx.NewWriter(file)
logx.SetWriter(writer)

但是这种做法并不优雅,且带来的影响是全局性的,我实验过,极限情况下会出现日志相互混乱出现的情况

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions