Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--log-level option #301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Config struct {
}

// BuildFromDockerLabels builds a scheduler using the config from a docker labels
func BuildFromDockerLabels(filterFlags ...string) (*core.Scheduler, error) {
func BuildFromDockerLabels(logLevel string, filterFlags ...string) (*core.Scheduler, error) {
c := &Config{}

d, err := c.buildDockerClient()
Expand All @@ -53,38 +53,38 @@ func BuildFromDockerLabels(filterFlags ...string) (*core.Scheduler, error) {
return nil, err
}

return c.build()
return c.build(logLevel)
}

// BuildFromFile builds a scheduler using the config from a file
func BuildFromFile(filename string) (*core.Scheduler, error) {
func BuildFromFile(logLevel string, filename string) (*core.Scheduler, error) {
c := &Config{}
if err := gcfg.ReadFileInto(c, filename); err != nil {
return nil, err
}

return c.build()
return c.build(logLevel)
}

// BuildFromString builds a scheduler using the config from a string
func BuildFromString(config string) (*core.Scheduler, error) {
func BuildFromString(logLevel string, config string) (*core.Scheduler, error) {
c := &Config{}
if err := gcfg.ReadStringInto(c, config); err != nil {
return nil, err
}

return c.build()
return c.build(logLevel)
}

func (c *Config) build() (*core.Scheduler, error) {
func (c *Config) build(logLevel string) (*core.Scheduler, error) {
defaults.SetDefaults(c)

d, err := c.buildDockerClient()
if err != nil {
return nil, err
}

sh := core.NewScheduler(c.buildLogger())
sh := core.NewScheduler(c.buildLogger(logLevel))
c.buildSchedulerMiddlewares(sh)

for name, j := range c.ExecJobs {
Expand Down Expand Up @@ -133,10 +133,15 @@ func (c *Config) buildDockerClient() (*docker.Client, error) {
return d, nil
}

func (c *Config) buildLogger() core.Logger {
func (c *Config) buildLogger(logLevel string) core.Logger {
stdout := logging.NewLogBackend(os.Stdout, "", 0)
// Set the backends to be used.
logging.SetBackend(stdout)
if logLevel == "" {
logLevel = "DEBUG"
}
level, _ := logging.LogLevel(logLevel)
logging.SetLevel(level, "")
logging.SetFormatter(logging.MustStringFormatter(logFormat))

return logging.MustGetLogger("ofelia")
Expand Down
2 changes: 1 addition & 1 deletion cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SuiteConfig struct{}
var _ = Suite(&SuiteConfig{})

func (s *SuiteConfig) TestBuildFromString(c *C) {
sh, err := BuildFromString(`
sh, err := BuildFromString("", `
[job-exec "foo"]
schedule = @every 10s

Expand Down
5 changes: 3 additions & 2 deletions cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DaemonCommand struct {
ConfigFile string `long:"config" description:"configuration file" default:"/etc/ofelia.conf"`
DockerLabelsConfig bool `short:"d" long:"docker" description:"read configurations from docker labels"`
DockerFilters []string `short:"f" long:"docker-filter" description:"filter to select docker containers"`
LogLevel string `long:"log-level" description:"log level" default:"DEBUG" choice:"DEBUG" choice:"WARNING" choice:"ERROR"`

config *Config
scheduler *core.Scheduler
Expand Down Expand Up @@ -42,9 +43,9 @@ func (c *DaemonCommand) Execute(args []string) error {

func (c *DaemonCommand) boot() (err error) {
if c.DockerLabelsConfig {
c.scheduler, err = BuildFromDockerLabels(c.DockerFilters...)
c.scheduler, err = BuildFromDockerLabels(c.LogLevel, c.DockerFilters...)
} else {
c.scheduler, err = BuildFromFile(c.ConfigFile)
c.scheduler, err = BuildFromFile(c.LogLevel, c.ConfigFile)
}

return
Expand Down
10 changes: 5 additions & 5 deletions cli/docker-labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *TestDockerSuit) TestLabelsFilterJobsCount(c *check.C) {
_, err := s.startTestContainersWithLabels(containersToStartWithLabels)
c.Assert(err, check.IsNil)

scheduler, err := BuildFromDockerLabels("label=" + strings.Join(filterLabel, "="))
scheduler, err := BuildFromDockerLabels("DEBUG", "label="+strings.Join(filterLabel, "="))
c.Assert(err, check.IsNil)
c.Assert(scheduler, check.NotNil)

Expand All @@ -79,15 +79,15 @@ func (s *TestDockerSuit) TestFilterErrorsLabel(c *check.C) {
c.Assert(err, check.IsNil)

{
scheduler, err := BuildFromDockerLabels()
scheduler, err := BuildFromDockerLabels("DEBUG")
c.Assert(errors.Is(err, errNoContainersMatchingFilters), check.Equals, true)
c.Assert(strings.Contains(err.Error(), requiredLabelFilter), check.Equals, true)
c.Assert(scheduler, check.IsNil)
}

customLabelFilter := []string{"label", "test=123"}
{
scheduler, err := BuildFromDockerLabels(strings.Join(customLabelFilter, "="))
scheduler, err := BuildFromDockerLabels("DEBUG", strings.Join(customLabelFilter, "="))
c.Assert(errors.Is(err, errNoContainersMatchingFilters), check.Equals, true)
c.Assert(err, check.ErrorMatches, fmt.Sprintf(`.*%s:.*%s.*`, "label", requiredLabel))
c.Assert(err, check.ErrorMatches, fmt.Sprintf(`.*%s:.*%s.*`, customLabelFilter[0], customLabelFilter[1]))
Expand All @@ -96,7 +96,7 @@ func (s *TestDockerSuit) TestFilterErrorsLabel(c *check.C) {

{
customNameFilter := []string{"name", "test-name"}
scheduler, err := BuildFromDockerLabels(strings.Join(customLabelFilter, "="), strings.Join(customNameFilter, "="))
scheduler, err := BuildFromDockerLabels("DEBUG", strings.Join(customLabelFilter, "="), strings.Join(customNameFilter, "="))
c.Assert(errors.Is(err, errNoContainersMatchingFilters), check.Equals, true)
c.Assert(err, check.ErrorMatches, fmt.Sprintf(`.*%s:.*%s.*`, "label", requiredLabel))
c.Assert(err, check.ErrorMatches, fmt.Sprintf(`.*%s:.*%s.*`, customLabelFilter[0], customLabelFilter[1]))
Expand All @@ -106,7 +106,7 @@ func (s *TestDockerSuit) TestFilterErrorsLabel(c *check.C) {

{
customBadFilter := "label-test"
scheduler, err := BuildFromDockerLabels(customBadFilter)
scheduler, err := BuildFromDockerLabels("DEBUG", customBadFilter)
c.Assert(errors.Is(err, errInvalidDockerFilter), check.Equals, true)
c.Assert(scheduler, check.IsNil)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ValidateCommand struct {
// Execute runs the validation command
func (c *ValidateCommand) Execute(args []string) error {
fmt.Printf("Validating %q ... ", c.ConfigFile)
config, err := BuildFromFile(c.ConfigFile)
config, err := BuildFromFile("", c.ConfigFile)
if err != nil {
fmt.Println("ERROR")
return err
Expand Down