Esempio n. 1
0
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)
	}
}
Esempio n. 2
0
func getLogger(name string, logConfig LogConfig) *logger.Logger {
	mainLogger.Info("Make logger of %s at file %s", name, logConfig.Filename)
	log1 := logger.MustGetLogger(name)

	var leveledBackend logger.LeveledBackend
	f, err := logger.NewStringFormatter("%{shortfile} %{time:2006-01-02T15:04:05} %{level:.1s} %{id:04d} %{module} %{message}")
	if err != nil {
		mainLogger.Info("failed to set format: %s", err)
	}
	logger.SetFormatter(f)

	if logConfig.Filename != "" {
		logFileName := logConfig.Filename

		logFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
		backend := logger.NewLogBackend(logFile, "", 0)
		if err != nil {
			mainLogger.Error("Failed to open log file - " + logFileName)
			panic(err)
		} else {
			leveledBackend = logger.AddModuleLevel(backend)
		}
	} else {
		backend := logger.NewLogBackend(os.Stdout, "", 0)
		leveledBackend = logger.AddModuleLevel(backend)

	}

	switch logConfig.Level {
	case "debug":
		leveledBackend.SetLevel(logger.NOTICE, name)
	case "error":
		logger.SetLevel(logger.ERROR, name)
	case "info":
		logger.SetLevel(logger.INFO, name)
	case "warn":
		logger.SetLevel(logger.WARNING, name)
	}

	log1.SetBackend(leveledBackend)
	return log1
}