コード例 #1
0
ファイル: log.go プロジェクト: masaki-saeki/gohan
//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
}
コード例 #2
0
ファイル: log.go プロジェクト: masaki-saeki/gohan
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)
	}
}