cihub / seelog

Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.
BSD 3-Clause "New" or "Revised" License
1.64k stars 244 forks source link

change log level at runtime #147

Closed qudongfang closed 1 year ago

qudongfang commented 7 years ago

i'd like to switch on some detailed log sometimes. do you have the plan to implement this feature?

pkorotkov commented 7 years ago

@qudongfang Does this wiki section work for you? Otherwise, please provide a snippet of your use case.

qudongfang commented 7 years ago

actually, i have already done some work on it today.

my use case:

first, I have configed seelog using a config file.


const (
    SEELOG_PATH = "../conf/seelog.xml"
)

func initLog() (err error) {
    logger, err := log.LoggerFromConfigAsFile(SEELOG_PATH)

    if err == nil {
        log.ReplaceLogger(logger)
        defer log.Flush()
    }

    return
}

then,i just want to change min or max log level for a while with a simple http api without restart my server or change the config file.

http.HandleFunc("/log/reset", func(out http.ResponseWriter, in *http.Request) {
        if !checkToken(out, in) {
            return
        }

        min := in.Form.Get("min")
        max := in.Form.Get("max")

        if 0 == len(min) || 0 == len(max) {
            proc.RenderJSON(out, "min & max are required")
        }

        low, ok := log.LogLevelFromString(min)
        if !ok {
            proc.RenderJSON(out, "min is wrong")
            return
        }

        high, ok := log.LogLevelFromString(max)
        if !ok {
            proc.RenderJSON(out, "max is wrong")
            return
        }

        if low > high {
            proc.RenderJSON(out, "wrong: min > max")
            return
        }

        c, err := log.NewMinMaxConstraints(low, high)
        if nil == err {
            last := log.SetConstraints(c)

                        str, ok := last.(fmt.Stringer)
            if ok {
                proc.RenderJSON(out, str.String())
            } else {
                proc.RenderJSON(out, last)
            }       
                } else {
            proc.RenderJSON(out, err)
        }
    })

my seelog config file:

<seelog type="asynctimer" asyncinterval="5000000" minlevel="trace" maxlevel="critical">

    <!--maxsize unit bytes-->
    <!--100M = 104857600 bytes-->
    <outputs formatid="main">
        <buffered size="10000" flushperiod="1000">
            <rollingfile type="size" filename="../log/trace.log" maxsize="104857600" maxrolls="3"/>
        </buffered>

        <filter levels="debug, info, warn, error, critical">
            <buffered size="10000" flushperiod="1000">
                <rollingfile type="size" filename="../log/debug.log" maxsize="104857600" maxrolls="3"/>
            </buffered>

            <filter levels="info, warn, error, critical">
                <buffered size="10000" flushperiod="1000">
                    <rollingfile type="size" filename="../log/info.log" maxsize="104857600" maxrolls="3"/>
                </buffered>

                <filter levels="warn, error, critical">
                    <buffered size="10000" flushperiod="1000">
                        <rollingfile type="size" filename="../log/warn.log" maxsize="104857600"
                                     maxrolls="3"/>
                    </buffered>

                    <filter levels="error, critical">
                        <buffered size="10000" flushperiod="1000">
                            <rollingfile type="size" filename="../log/error.log" maxsize="104857600"
                                         maxrolls="3"/>
                        </buffered>

                        <filter levels="critical">
                            <buffered size="10000" flushperiod="1000">
                                <rollingfile type="size" filename="../log/critical.log" maxsize="104857600"
                                             maxrolls="3"/>
                            </buffered>
                        </filter>
                    </filter>
                </filter>
            </filter>
        </filter>
    </outputs>

    <formats>
        <format id="main" format="%Date(2006 Jan 02/3:04:05.000000000 PM MST) [%Level] [%File:%Line] %Msg%n"/>
    </formats>
</seelog>
qudongfang commented 7 years ago

@pkorotkov @goodsign Would you guys consider accepting this pull request?

pkorotkov commented 7 years ago

@qudongfang, thanks for contribution! We will go through it soon.