How to change log levels at runtime without restarting the go application

1,777 views
Skip to first unread message

vteja...@gmail.com

unread,
Jun 4, 2018, 2:39:24 PM6/4/18
to golang-nuts
Hi,

Is there a way in go-lang to change the log level at run time without application restart?

BR,
Teja

Ian Lance Taylor

unread,
Jun 4, 2018, 2:44:51 PM6/4/18
to vteja...@gmail.com, golang-nuts
On Mon, Jun 4, 2018 at 11:28 AM, <vteja...@gmail.com> wrote:
>
> Is there a way in go-lang to change the log level at run time without
> application restart?

This is going to depend on the log package that you are using, so you
need to tell us that.

Ian

vteja...@gmail.com

unread,
Jun 4, 2018, 2:49:11 PM6/4/18
to golang-nuts
Hi,

We are using package logger

import (
        "log"
        "os"
)

const (
        levelTrace   int    = 6
        levelInfo    int    = 5
        levelWarning int    = 4
        levelError   int    = 3
        levelFatal   int    = 2
        levelPanic   int    = 1
        prefix       string = ""
)

Not any specific module. In case if we such module available, it would be helpful.

Thanks,
Teja

vteja...@gmail.com

unread,
Jun 4, 2018, 2:51:35 PM6/4/18
to golang-nuts
Hi,

We are not using any special logger.

package logger

import (
        "log"
        "os"
)

const (
        levelTrace   int    = 6
        levelInfo    int    = 5
        levelWarning int    = 4
        levelError   int    = 3
        levelFatal   int    = 2
        levelPanic   int    = 1
        prefix       string = ""
)

Its the basic logger.

It would be great if you can let us know about new module.

Thanks,
Teja

On Monday, June 4, 2018 at 8:44:51 PM UTC+2, Ian Lance Taylor wrote:

Peter Mogensen

unread,
Jun 4, 2018, 2:54:25 PM6/4/18
to golan...@googlegroups.com


On 06/04/2018 08:48 PM, vteja...@gmail.com wrote:
>
> Not any specific module. In case if we such module available, it would
> be helpful.
>

It also depends a bit on what you are logging to. For instance, whether
you are just logging to stdout/stderr and prefixing lines with the level
as <#>. (as systemd would expect)

You can in principle do that with standard "log" package, but it's not
very efficient.
I wrote a log package for that purpose which is backward source
compatible with the standard library "log" package.

/Peter

vteja...@gmail.com

unread,
Jun 4, 2018, 2:59:08 PM6/4/18
to golang-nuts
Hi,

We are sending log messages to stdout as you say like : logger.Info("hello")

But we want to control the logs, at runtime If we set it to debug level, we expect the application to change the log level setting and start producing debug logs

BR,
Teja

Peter Mogensen

unread,
Jun 4, 2018, 3:12:07 PM6/4/18
to golan...@googlegroups.com


On 06/04/2018 08:58 PM, vteja...@gmail.com wrote:
> Hi,
>
> We are sending log messages to stdout as you say like
> : logger.Info("hello")
>
> But we want to control the logs, at runtime If we set it to debug
> level, we expect the application to change the log level setting and
> start producing debug logs
>

I guess a lot of log packages can do that.
Here's the one I wrote:
https://github.com/One-com/gone/tree/master/log

I normally hook it up via SIGTTIN/SIGTTOU for increase/decrease of
log-level or via a command interface on a UNIX socket to control
loglevels at runtime.

/Peter

b97...@gmail.com

unread,
Jun 5, 2018, 9:47:01 AM6/5/18
to golang-nuts
You could store app settings (log level etc.) in a file, any time this file is changed, you reload it and update the settings.

在 2018年6月5日星期二 UTC+7上午1:39:24,vteja...@gmail.com写道:
Reply all
Reply to author
Forward
0 new messages