コード例 #1
0
ファイル: log.go プロジェクト: webdeskltd/log
// Initialize default configuration
func (log *LogEssence) Initialize() *LogEssence {
	var err error
	var cnf *Configuration

	log.SetApplicationName(``)
	log.HostName, err = os.Hostname()
	if testing_mode_one {
		err = errors.New("Hostname not defined")
	}
	if err != nil {
		log.HostName = `undefined`
	}
	// Create default configuration and apply
	cnf = log.defaultConfiguration()
	err = log.Configure(cnf)
	if err != nil {
		Error("Error Configure(): %v\n", err)
	} else {
		log.ready = true
	}

	// Default level writer
	log.defaultLevelLogWriter = w.NewWriter(l.DEFAULT_LEVEL).Resolver(log.ResolveNames).AttachBackends(log.backend)
	if log.interceptStandardLog {
		stdLogConnect(log.defaultLevelLogWriter)
	}

	return log
}
コード例 #2
0
ファイル: log_test.go プロジェクト: webdeskltd/log
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
	}
}
コード例 #3
0
ファイル: configuration.go プロジェクト: webdeskltd/log
// 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
}