Example #1
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
}
Example #2
0
func initLog(logConfig map[string]interface{}) {

	mainLogger.Info("LogConfig: %v", logConfig)
	_, ok := logConfig["level"]
	if ok {
		switch logConfig["level"].(string) {
		case "debug":
			logger.SetLevel(logger.NOTICE, moduleName)
		case "error":
			logger.SetLevel(logger.ERROR, moduleName)
		case "info":
			logger.SetLevel(logger.INFO, moduleName)
		case "warn":
			logger.SetLevel(logger.WARNING, moduleName)
		}
	}

	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)

	_, ok = logConfig["file"]
	if ok {
		logFileName := logConfig["file"].(string)

		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 {
			logger.SetBackend(backend)
		}
	} else {
		backend := logger.NewLogBackend(os.Stdout, "", 0)
		logger.SetBackend(backend)
	}
}