16

I’ve an application which should use log in state of debug, i.e. all the logs that I want to provide is like log.debug

I’ve read about it and find the following

My question is how should I “tell” to the program that now run at debug mode an then all the logs will be printed since this I believe should come from outside …

example will be very helpful since I'm new to golfing.

3
  • 1
    Check codebases that are out there. In go, the common way to do that is to load configuration via environment variables (eg LOG_LEVEL, and use os.Getenv or a config package to load the value). That's why logrus for example allows you to set log levels via ints or strings. Commented Nov 27, 2017 at 16:04
  • @EliasVanOotegem - Can you please provide example how to init and use it via logrus ? Commented Nov 27, 2017 at 16:07
  • Posted a minimal example. TBH: just read the readme of the logrus repository, It contains an example where the log level is actually being set. Commented Nov 27, 2017 at 16:29

2 Answers 2

26

Ok, a really simple example of the approach I suggested in the comment:

package main

import (
    "os"

    "github.com/sirupsen/logrus"
)

func init() {
    lvl, ok := os.LookupEnv("LOG_LEVEL")
    // LOG_LEVEL not set, let's default to debug
    if !ok {
        lvl = "debug"
    }
    // parse string, this is built-in feature of logrus
    ll, err := logrus.ParseLevel(lvl)
    if err != nil {
        ll = logrus.DebugLevel
    }
    // set global log level
    logrus.SetLevel(ll)
}

func main() {
    logrus.Debug("Will only be visible if the loglevel permits it")
}

The original comment:

Check codebases that are out there. In go, the common way to do that is to load configuration via environment variables (eg LOG_LEVEL, and use os.Getenv or a config package to load the value). That's why logrus for example allows you to set log levels via ints or strings.

Please, please: before you ask a question, read the basic info about the packages you use. Even the github repo for logrus' main README contains an example setting the log level to a specific level:

https://github.com/sirupsen/logrus#example

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks 1+ I've tried your suggestion which is good idea but not working :( in both case the log is printed if !ok and if ok , why ?
in both case it get 5 for debug level
and btw, does it good practice to put it on init function ?
In this example, unless you set the log level to something other than debug (eg info), you'll always see debug log output. Check the implementation of the ParseLevel function to see what string values you can assign to LOG_LEVEL. Having this in the main.init func is acceptable, but in most cases you'll want to configure some other things, and you'll end up setting up the logger in the main func somewhere. It all depends. In this small example, this is a valid use-case of the init func though
@rfportilla No need to apologise. All in all, though, using go run is not something that is recommended, especially not when you're running something that requires bootstrapping (which your example of go run -set loglevel=debug implies). I'd recommend you use go build and execute the binary, and have a look at fsnotify to use file-based config that can be reloaded at runtime
|
2

If the only thing you need is check level before printing you can create you own thin wrapper for standard logger.

This will help to better understand how they work. Feel free to ask any questions.

package main

import (
    "log"
)

type MyLog struct {
    PrintDebug bool
}

func (m *MyLog) Debug(args ...interface{}) {
    if m.PrintDebug {
        m.Print(args...)
    }
}

func (m *MyLog) Print(args ...interface{}) {
    log.Print(args...)
}

func main() {
    ml := MyLog{}
    ml.Debug("PrintDebig = false, so no oitput")
    ml.Print("this will be printed anyway")

    ml.PrintDebug = true
    ml.Debug("Hello, playground")
}

https://play.golang.org/p/gKxQtC9NqX

4 Comments

Thanks, assume I want to influence from outside (my application should be deployed to the cloud) the program on the log i.e. flag/env to print debug.log or not , how should I do it ?
This is a poor implementation: it's not thread safe (no assignment is, unless you use the atomic package). You're not really implementing different levels. OP mentions logrus, which does have differen log levels. Why wouldn't you simply use that instead? Lastly: the question was mainly focused on how you switch to a different log level: hard-coding a boolean reassignment wouldn't be the way to go I don't think
@EliasVanOotegem that's a start point to understand better how to start. If you understand logics than bool can be switched to any else and analogue methods like Debug, Warn and Critical implemented. Usually you do not switch DebugLevels on fly, so bool var is really written only once. Original Logger is thread safe. Logrus is nice, but it has some drawbacks - for example bad handling of line numbers, what standard logger can do well. Solution
Logrus' issues aside, the OP was mainly asking how to set the loglevel, to which the answer would be: this is commonly done through environment variables. Your answer is a simplistic implementation using boolean flags. The OP already mentioned logrus, so I'd assume he's using that, and is mainly trying to find out how to turn debug logging on/off

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.