docker / libcompose

*Unmaintained/Deprecated* An experimental go library providing Compose-like functionality
https://godoc.org/github.com/docker/libcompose
Apache License 2.0
585 stars 191 forks source link

libcompose library ignoring set log levels #487

Open stepanstipl opened 6 years ago

stepanstipl commented 6 years ago

Hi, I can't seem to be able to get libcompose to respect my logger settings.

If I do smth like:

import (
    "github.com/sirupsen/logrus"
        ...
)

func main() {
    logger := logrus.New()
    logger.Formatter = &logrus.JSONFormatter{}
    logger.SetLevel(logrus.ErrorLevel)
    // use logrus for standard log output:
    log.SetOutput(logger.Writer())

    cd, err := data.Asset("docker-compose.yaml")
    if err != nil {
        log.Fatal(err)
    }

    dctx := &ctx.Context{
        Context: project.Context{
            ComposeBytes: [][]byte{cd},
            ProjectName:  "test",
        },
    }

    prj, err := docker.NewProject(dctx, nil)
    if err != nil {log.Fatal(err)
    }

    err = prj.Up(context.Background(), options.Up{})
    if err != nil {
        log.Fatal(err)
    }
}

typically libraries using logrus would respect my logger settings (level & format).

I can't get it working with libcompose, for some reason I'll still see all the logs from info level above with default formatting.

Any idea why is this not working/how I can suppress logs from libcompose? Any help appreciated, cheers.

mcuadros commented 6 years ago

any update on this?

andscoop commented 5 years ago

I dug in a little more and wanted to share my findings

Controlling Log Level as Best We Can

While not ideal, it appears that you gain some control over the logging level if you toggle the infoEvents map in listener.go. This will work if you vendor libcompose and either want the logging level at info or debug. I don't see a way to interface with this map at run time. Ideally libcompose respects logrus settings and a developer wouldn't need to modify this map. https://github.com/docker/libcompose/blob/master/project/listener.go

Isolating Issue

I wanted to isolate the log level to a single log so I created the linked gist below. Toggling the log-level on this script shows that the log call to logrus.Infof("Recreating %s", s.name) in /libcompose/docker/service/service.go is unable to be suppressed. Reason is still unclear.

https://gist.github.com/andscoop/3a30a42b8f977cc8f85cf5c6e0297afd

Potential Code Cleanup

One last note - there are a few places where contributors printed to standard out vs logging which will make it impossible to control the logging level via this method https://github.com/docker/libcompose/blob/master/docker/network/network.go#L49

HarikrishnanBalagopal commented 3 years ago

Seems it is possible to control the logging level in libcompose@v0.4.1-0.20171025083809-57bd716502dc Example:

        originalLevel := log.GetLevel()
    log.SetLevel(log.FatalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.
    err := proj.Parse()
    log.SetLevel(originalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.

The above code allows libcompose to be used to detect if a file is a docker-compose file or not. Usually it prints a bunch of errors if the file is not a valid docker-compose file, but with above code it does not.