Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

libcompose library ignoring set log levels #487

Open
stepanstipl opened this issue Aug 10, 2017 · 3 comments
Open

libcompose library ignoring set log levels #487

stepanstipl opened this issue Aug 10, 2017 · 3 comments

Comments

@stepanstipl
Copy link

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
Copy link

any update on this?

@andscoop
Copy link

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
Copy link

HarikrishnanBalagopal commented Nov 26, 2020

Seems it is possible to control the logging level in [email protected]
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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants