Пример #1
0
// Start starts logging using the given Context.
func (l *Log) Start(ctx *Context) (err error) {
	var target io.Writer
	if l.Path != "" {
		path := ctx.AbsPath(l.Path)
		target, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
		if err != nil {
			return err
		}
	} else if l.Verbose || l.Debug {
		target = ctx.Stderr
	}

	if target != nil {
		writer := loggo.NewSimpleWriter(target, &loggo.DefaultFormatter{})
		_, err = loggo.ReplaceDefaultWriter(writer)
		if err != nil {
			return err
		}
	} else {
		loggo.RemoveWriter("default")
	}
	if l.Verbose || l.Debug {
		level := loggo.INFO
		if l.Debug {
			level = loggo.DEBUG
		}
		// Set the level on the root logger.
		loggo.GetLogger("").SetLogLevel(level)
	}
	loggo.ConfigureLoggers(l.Config)
	return nil
}
Пример #2
0
// SetupLog set application's modules log level
func SetupLog(appModules []string, verbose bool, logfile string) error {
	var hivyModules = []string{
		"hivy",
		"hivy.security",
	}
  for _, module := range appModules {
    hivyModules = append(hivyModules, module)
  }
	logLevel := defaultLogLevel
	if verbose {
		logLevel = superVerboseLogLevel
	}

	// If a file was specified, replace console output
	if logfile != "" {
		target, err := os.OpenFile(logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
		if err != nil {
			return err
		}
		fileWriter := loggo.NewSimpleWriter(target, &loggo.DefaultFormatter{})
		//loggo.RegisterWriter("logfile", file_writer, log_level)
		_, err = loggo.ReplaceDefaultWriter(fileWriter)
		if err != nil {
			return err
		}
	}

	// Central log level configuration
	for _, module := range hivyModules {
		if err := loggo.ConfigureLoggers(module + "=" + logLevel.String()); err != nil {
			return err
		}
	}

	//log.Debugf("logging level:", loggo.LoggerInfo())
	return nil
}