예제 #1
0
func logWriterInitializer(level int, args map[string]string) (io.Writer, error) {
	flagStr, found := args["flag"]
	if !found || (flagStr == "") {
		flagStr = "log.lstdflags"
	}

	flag, found := flagMap[strings.ToLower(flagStr)]
	if !found {
		return nil, fmt.Errorf("未知的Flag参数:[%v]", flagStr)
	}

	prefix, _ := args["prefix"]

	return &logWriter{
		level:  level,
		flag:   flag,
		prefix: prefix,
		c:      writer.NewContainer(),
	}, nil
}
예제 #2
0
func TestLogWriter(t *testing.T) {
	a := assert.New(t)

	lw := &logWriter{
		level:  LevelInfo,
		flag:   log.LstdFlags,
		prefix: "",
		c:      writer.NewContainer(),
	}
	a.NotNil(lw)

	err := lw.Add(&logWriterTestWriter1{})
	a.NotError(err)
	err = lw.Add(&logWriterTestWriter2{})
	a.NotError(err)

	lw.initLogger()
	a.NotNil(lw.log)
	lw.log.Print("hello world")

	a.True(len(logWriterTestWriter1Content) > 0)
	a.Equal(logWriterTestWriter1Content, logWriterTestWriter2Content)
}
예제 #3
0
func TestconfigToWriter(t *testing.T) {
	a := assert.New(t)
	clearInitializer()

	logs := func(args map[string]string) (io.Writer, error) {
		return writer.NewContainer(), nil
	}
	a.True(Register("logs", logs))

	debug := func(args map[string]string) (io.Writer, error) {
		return &configTestWriter{}, nil
	}
	a.True(Register("debug", debug))

	// 加载xml
	r := bytes.NewReader([]byte(xmlCfg))
	a.NotNil(r)

	cfg, err := loadFromXml(r)
	a.NotError(err).NotNil(cfg)

	// 转换成writer
	w, err := cfg.toWriter()
	a.NotError(err).NotNil(w)

	// 转换成writer.Container
	c, ok := w.(*writer.Container)
	a.True(ok).NotNil(c)

	// 写入c,应该有内容输出到configTestWriterContent
	c.Write([]byte("hello"))
	a.Equal(configTestWriterContent, []byte("hello"))

	c.Write([]byte(" world"))
	a.Equal(configTestWriterContent, []byte("hello world"))
}