Пример #1
0
func TestGetDefaultLog(t *testing.T) {
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	lg := GetDefaultLog()
	if lg == nil {
		t.Errorf("Error GetDefaultLog()")
	}
}
Пример #2
0
func TestConfigure(t *testing.T) {
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	err := Configure(nil)
	if err != ERROR_CONFIGURATION_IS_NULL {
		t.Errorf("Error Configure()")
		return
	}
}
Пример #3
0
func TestWriterResolverAttachBackends(t *testing.T) {
	var bck = b.NewBackends()
	var obj = NewWriter(l.INFO)

	obj.AttachBackends(bck).Write([]byte{})
	obj.AttachBackends(bck).WriteString("")
	if obj.backends == nil {
		t.Errorf("Error AttachBackends() not work")
		return
	}
}
Пример #4
0
func TestCustomMessage(tst *testing.T) {
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	CustomMessage(m.NewMessage(
		t.NewTrace().
			Trace(t.STEP_BACK + 2).
			GetRecord().
			Resolver(singleton[default_LOGUUID].getEssence().ResolveNames),
	).Level(l.NOTICE).
		Write(""),
	)
}
Пример #5
0
func TestFatal(t *testing.T) {
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	var code int = 0
	exit_func = func(c int) {
		code = c
	}
	Fatal()
	if code == 0 {
		t.Errorf("Error Fatal()")
		return
	}
}
Пример #6
0
func TestLogResolveNames(t *testing.T) {
	testing_mode_two = true
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()

	lg := NewLog().getEssence()
	lg.backend = b.NewBackends()
	lg.moduleNames["testing"] = "TestLogResolveNames"
	txt, _ := m.NewMessage(
		tr.NewTrace().
			Trace(tr.STEP_BACK).
			GetRecord().
			Resolver(lg.ResolveNames),
	).Level(l.NOTICE).
		Write("Test SetModuleName()").
		Prepare().
		Record.
		Format(`%{module} | %{package}`)
	if txt != "testing | TestLogResolveNames" {
		t.Errorf("Error in TestLogResolveNames()")
		return
	}
	testing_mode_two = false
}
Пример #7
0
func TestLogSetModuleName(t *testing.T) {
	testing_mode_two = true
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()

	lg := NewLog().getEssence()
	lg.backend = b.NewBackends()
	if len(lg.moduleNames) != 0 {
		t.Errorf("Error in moduleNames (map[string]string)")
		return
	}
	lg.SetModuleName("TestLogSetModuleName")
	if len(lg.moduleNames) != 1 {
		t.Errorf("Error in SetModuleName()")
		return
	}
	lg.Notice("Test SetModuleName()")
	if nm, ok := lg.moduleNames["testing"]; !ok || nm != "TestLogSetModuleName" {
		t.Errorf("Error in SetModuleName()")
		return
	}
	lg.Close()
	testing_mode_two = false
}
Пример #8
0
func TestDestructor(t *testing.T) {
	var bck = b.NewBackends()
	var obj = NewWriter(l.INFO)
	obj.Resolver(func(rec *r.Record) {})
	obj.AttachBackends(bck)
	if obj.backends == nil {
		t.Errorf("Error AttachBackends() not work")
		return
	}
	if obj.resolver == nil {
		t.Errorf("Error Resolver() not work")
		return
	}
	destructor(obj)
	if obj.backends != nil || obj.resolver != nil {
		t.Errorf("Error destructor() not work")
		return
	}
}
Пример #9
0
// Close logging
func (log *LogEssence) Close() (err error) {
	// Reset standard logging to default settings
	log.InterceptStandardLog(false)
	log.defaultLevelLogWriter = nil

	// Block programm while goroutine exit
	log.backend.Close()

	// Create new backend object, old object automatic call Stop all backend and destroy
	log.backend = b.NewBackends()

	// Reinitialisation
	var uuid, _ = u.ParseUUID(default_LOGUUID)
	singleton[default_LOGUUID] = newLogEssence(uuid)

	runtime.GC()
	runtime.Gosched()
	return
}
Пример #10
0
func TestLogInitialize(t *testing.T) {
	testing_mode_two = true
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	testing_mode_two = false

	testing_mode_one = true
	hn, err := os.Hostname()
	if err != nil {
		t.Errorf("Error os.Hostname()")
		return
	}

	lg := NewLog().getEssence()
	if lg.HostName != `undefined` {
		t.Errorf("Error Initialize() os.Hostname() doesn't work")
		return
	}
	if lg.HostName == hn {
		t.Errorf("Error testing_mode_one flag")
		return
	}
	testing_mode_one = false

	lg = NewLog().getEssence()
	lg.defaultLevelLogWriter = w.NewWriter(l.NOTICE)
	lg.InterceptStandardLog(true)
	lg.Initialize()
	if lg.ready == false {
		t.Errorf("Error Initialize()")
		return
	}
	lg.InterceptStandardLog(false)
	err = lg.Close()
	if err != nil {
		t.Errorf("Error Close() logging: %v", err)
		return
	}
}
Пример #11
0
// Apply new configuration
func (self *LogEssence) Configure(cnf *Configuration) (err error) {
	var bname b.BackendName
	var ok bool

	// Паникаловка :)
	defer func() {
		if r := recover(); r != nil {
			if e, ok := r.(error); ok {
				if err != nil {
					err = errors.New("failed to configure: " + e.Error() + ", " + err.Error())
				} else {
					err = errors.New("failed to configure: " + e.Error())
				}
			}
			self.backend = nil
			self.cnf = nil
		}
	}()

	// Проверка и подготовка конфигурации
	err = self.prepareConfigure(cnf)
	if err != nil {
		return
	}

	// (Ре)Инициализация пула
	self.backend = b.NewBackends()
	for bname = range cnf.Mode {
		var backend *b.Backend
		var bmode b.Mode
		var levels []l.Level
		var level l.Level

		switch bname {
		case b.NAME_CONSOLE:
			backend = b.NewBackendConsole(nil).SetFormat(cnf.Formats[bname])
		case b.NAME_SYSLOG:
			backend = b.NewBackendSyslog(self.HostName).SetFormat(cnf.Formats[bname])
		case b.NAME_FILE:
			var fh *os.File
			if cnf.File == "" {
				panic(ERROR_LOG_FILENAME_IS_EMPTY)
			}
			fh, err = os.OpenFile(cnf.File, syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
			if err != nil {
				panic(ERROR_INIT_FILE_BACKEND)
			}
			backend = b.NewBackendFile(fh).SetFormat(cnf.Formats[bname])
		case b.NAME_GRAYLOG2:
			var hgpc g.GelfProtocolClient
			var hG *g.GelfClient
			if cnf.Graylog2.Protocol == g.UDP_NETWORK {
				hgpc = g.MustUdpClient(cnf.Graylog2.Host, cnf.Graylog2.Port, cnf.Graylog2.ChunkSize)
			}
			if cnf.Graylog2.Protocol == g.TCP_NETWORK {
				hgpc = g.MustTcpClient(cnf.Graylog2.Host, cnf.Graylog2.Port)
			}
			hG = g.NewGelfClient(hgpc, self.cnf.Graylog2.Compression)
			backend = b.NewBackendGraylog2(hG).SetFormat(cnf.Formats[bname])
		case b.NAME_MEMORYPIPE:
			backend = b.NewBackendMemorypipe().SetFormat(cnf.Formats[bname])
		case b.NAME_TELEGRAM:
			backend = b.NewBackendTelegram().SetFormat(cnf.Formats[bname])
		default:
			panic(errors.New(fmt.Sprintf("%s %v", ERROR_UNKNOWN_MODE.Error(), bname)))
		}

		// Устанавливаем уровень или уровни логирования: SetLevel() для NORMAL or SetSelectLevels() для SELECT
		if cnf.Mode[bname] == nil {
			bmode = b.MODE_NORMAL
		} else if len(cnf.Mode[bname]) == 0 {
			bmode = b.MODE_NORMAL
		} else {
			bmode = b.MODE_SELECT
			for n := range cnf.Mode[bname] {
				if _, ok = l.Map2Level[cnf.Mode[bname][n]]; ok {
					levels = append(levels, l.Map2Level[cnf.Mode[bname][n]])
				}
			}
		}
		if bmode == b.MODE_NORMAL {
			if _, ok = cnf.Levels[bname]; ok {
				level = l.Map2Level[cnf.Levels[bname]]
			} else {
				level = l.DEFAULT_LEVEL
			}
		}

		// Если backend создан, добавляем его в pool
		if backend != nil {
			self.backend.AddBackend(
				backend.SetLevel(level).
					SetSelectLevels(levels...).
					SetMode(bmode),
			)
		}

	}
	// Инициализация writer
	self.defaultLevelLogWriter = w.NewWriter(l.DEFAULT_LEVEL).Resolver(self.ResolveNames).AttachBackends(self.backend)
	if self.interceptStandardLog {
		stdLogConnect(self.defaultLevelLogWriter)
	}

	self.cnf = cnf
	return
}
Пример #12
0
func TestDebug(t *testing.T) {
	singleton[default_LOGUUID].getEssence().InterceptStandardLog(false)
	singleton[default_LOGUUID].getEssence().backend = b.NewBackends()
	Debug()
}