// TestExtensions runs extension tests when invoked from Gohan CLI func TestExtensions(c *cli.Context) { buflog.SetUpDefaultLogging() var config *util.Config configFilePath := c.String("config-file") if configFilePath != "" && !c.Bool("verbose") { config = util.GetConfig() err := config.ReadConfig(configFilePath) if err != nil { log.Error(fmt.Sprintf("Failed to read config from path %s: %v", configFilePath, err)) os.Exit(1) } err = gohan_log.SetUpLogging(config) if err != nil { log.Error(fmt.Sprintf("Failed to set up logging: %v", err)) os.Exit(1) } } testFiles := getTestFiles(c.Args()) //logging from config is a limited printAllLogs option returnCode := RunTests(testFiles, c.Bool("verbose") || config != nil, c.String("run-test")) os.Exit(returnCode) }
func addLevelsToBackend(config *util.Config, prefix string, backend logging.LeveledBackend) { level, _ := logging.LogLevel(config.GetString(prefix+"level", defaultLogLevel)) backend.SetLevel(level, "") modules := config.GetList(prefix+"modules", []interface{}{}) for _, rawModule := range modules { module, _ := rawModule.(map[string]interface{}) moduleName, _ := module["name"].(string) moduleLevel, _ := module["level"].(string) level, err := logging.LogLevel(moduleLevel) if moduleName == "" || err != nil { continue } backend.SetLevel(level, moduleName) } }
//SetUpLogging configures logging based on configuration func SetUpLogging(config *util.Config) error { var backends = []logging.Backend{} if prefix := "logging/file/"; config.GetBool(prefix+"enabled", false) { logFile, err := os.OpenFile(config.GetString(prefix+"filename", "gohan.log"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) if err != nil { return err } fileBackendLeveled := getLeveledBackend(logFile, mustGohanJSONFormatter("gohan")) addLevelsToBackend(config, prefix, fileBackendLeveled) backends = append(backends, fileBackendLeveled) } if prefix := "logging/stderr/"; config.GetBool(prefix+"enabled", true) { stringFormatter := logging.MustStringFormatter( "%{color}%{time:15:04:05.000} %{module} %{level} %{color:reset} %{message}", ) stderrBackendLeveled := getLeveledBackend(os.Stderr, stringFormatter) addLevelsToBackend(config, prefix, stderrBackendLeveled) backends = append(backends, stderrBackendLeveled) } logging.SetBackend(backends...) return nil }