コード例 #1
0
ファイル: log.go プロジェクト: weisd/api
func Get(name string) loggerMap {
	l, ok := LogsMap[name]
	if !ok {
		baseLog.Fatal(1, "Unknown log %s", name)
		return nil
	}

	return l
}
コード例 #2
0
ファイル: setting.go プロジェクト: hzmnet/api
func newLogService() {
	f := "logs.toml"
	configPath := getUserConfigFile(f)

	m := multiconfig.NewWithPath(configPath)
	err := m.Load(Cfg)
	if err != nil {
		panic(err)
	}

	for _, conf := range Cfg.Logs {
		if !conf.ENABLE {
			continue
		}
		level, ok := logLevels[conf.LEVEL]
		if !ok {
			log.Fatal(4, "Unknown log level: %s", conf.LEVEL)
		}

		str := ""
		switch conf.MODE {
		case "console":
			str = fmt.Sprintf(`{"level":%s}`, level)
		case "file":
			str = fmt.Sprintf(
				`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`,
				level,
				conf.FILE_NAME,
				conf.LOG_ROTATE,
				conf.MAX_LINES,
				1<<uint(conf.MAX_SIZE_SHIFT),
				conf.DAILY_ROTATE, conf.MAX_DAYS,
			)
		case "conn":
			str = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level,
				conf.RECONNECT_ON_MSG,
				conf.RECONNECT,
				conf.PROTOCOL,
				conf.ADDR)
		case "smtp":

			tos, err := json.Marshal(conf.RECEIVERS)
			if err != nil {
				log.Error(4, "json.Marshal(conf.RECEIVERS) err %v", err)
				continue
			}

			str = fmt.Sprintf(`{"level":%s,"username":"******","password":"******","host":"%s","sendTos":%s,"subject":"%s"}`, level,
				conf.USER,
				conf.PASSWD,
				conf.HOST,
				tos,
				conf.SUBJECT)
		case "database":
			str = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level,
				conf.DRIVER,
				conf.CONN)
		default:
			continue
		}

		log.Info(str)
		log.NewLogger(conf.BUFFER_LEN, conf.MODE, str)
		log.Info("Log Mode: %s(%s)", conf.MODE, conf.LEVEL)
	}

}
コード例 #3
0
ファイル: log.go プロジェクト: weisd/api
func InitLogs() {

	LogsMap = make(map[string]loggerMap)

	for name, v := range setting.Cfg.Logs {
		loggerMaper := make(map[string]*baseLog.Logger)
		for _, conf := range v {

			if !conf.ENABLE {
				continue
			}
			level, ok := logLevels[conf.LEVEL]
			if !ok {
				baseLog.Fatal(4, "Unknown log level: %s", conf.LEVEL)
			}

			str := ""
			switch conf.MODE {
			case "console":
				str = fmt.Sprintf(`{"level":%s}`, level)
			case "file":
				str = fmt.Sprintf(
					`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`,
					level,
					conf.FILE_NAME,
					conf.LOG_ROTATE,
					conf.MAX_LINES,
					1<<uint(conf.MAX_SIZE_SHIFT),
					conf.DAILY_ROTATE, conf.MAX_DAYS,
				)
			case "conn":
				str = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level,
					conf.RECONNECT_ON_MSG,
					conf.RECONNECT,
					conf.PROTOCOL,
					conf.ADDR)
			case "smtp":

				tos, err := json.Marshal(conf.RECEIVERS)
				if err != nil {
					baseLog.Error(4, "json.Marshal(conf.RECEIVERS) err %v", err)
					continue
				}

				str = fmt.Sprintf(`{"level":%s,"username":"******","password":"******","host":"%s","sendTos":%s,"subject":"%s"}`, level,
					conf.USER,
					conf.PASSWD,
					conf.HOST,
					tos,
					conf.SUBJECT)
			case "database":
				str = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level,
					conf.DRIVER,
					conf.CONN)
			default:
				continue
			}

			baseLog.Info(str)

			loggerMaper[conf.MODE] = baseLog.NewCustomLogger(conf.BUFFER_LEN, conf.MODE, str)
			baseLog.Info("Log Mode: %s(%s)", conf.MODE, conf.LEVEL)

		}

		LogsMap[name] = loggerMaper
	}

	Info("logs 初始化完成 map %v", LogsMap)

}