// Create dafault configuration func (log *LogEssence) defaultConfiguration() (cnf *Configuration) { if testing_mode_one { return } cnf = &Configuration{ BufferFlushImmediately: true, BufferSize: 0, Mode: make(map[b.BackendName][]l.LevelName), Levels: make(map[b.BackendName]l.LevelName), Formats: make(map[b.BackendName]string), Format: default_FORMAT, Graylog2: ConfigurationGraylog2{ Compression: g.COMPRESSION_NONE, Source: log.HostName, Protocol: g.UDP_NETWORK, BufferSize: 1000, }, Telegram: ConfigurationTelegram{}, } cnf.Mode[b.NAME_CONSOLE] = nil cnf.Levels[b.NAME_CONSOLE] = l.LevelName(l.Map[l.DEFAULT_LEVEL]) return }
func TestLogPrepareConfigure(t *testing.T) { var cnf *Configuration var err error var log *LogEssence = NewLog().getEssence() cnf = &Configuration{ BufferFlushImmediately: true, BufferSize: 0, Mode: make(map[b.BackendName][]l.LevelName), Levels: make(map[b.BackendName]l.LevelName), Formats: make(map[b.BackendName]string), Format: "", Graylog2: ConfigurationGraylog2{ Compression: g.COMPRESSION_NONE, Source: "test.local", Protocol: g.UDP_NETWORK, BufferSize: 1000, }, Telegram: ConfigurationTelegram{}, } err = log.prepareConfigure(cnf) if err != nil { t.Errorf("Error in prepareConfigure()") } else if cnf.Format != default_FORMAT { t.Errorf("Error Default_FORMAT in prepareConfigure()") } cnf.Format = "%{ERROR_FOR_TEST}" err = log.prepareConfigure(cnf) if err == nil { t.Errorf("Error in prepareConfigure()") } cnf.Format = "" cnf.Mode = map[b.BackendName][]l.LevelName{ b.NAME_CONSOLE: []l.LevelName{"fatal"}, b.NAME_FILE: []l.LevelName{"fatal"}, b.NAME_GRAYLOG2: []l.LevelName{"fatal"}, b.NAME_MEMORYPIPE: []l.LevelName{"fatal"}, b.NAME_SYSLOG: []l.LevelName{"fatal"}, b.NAME_TELEGRAM: []l.LevelName{"fatal"}, } err = log.prepareConfigure(cnf) if err != nil { t.Errorf("Error in prepareConfigure()") } cnf.Mode = map[b.BackendName][]l.LevelName{ b.BackendName("FOR_TEST"): []l.LevelName{"FOR_TEST"}, } cnf.Levels = map[b.BackendName]l.LevelName{ b.BackendName("FOR_TEST"): l.LevelName("FOR_TEST"), } cnf.Formats = map[b.BackendName]string{ b.BackendName("coNsSOLE"): default_FORMAT, } err = log.prepareConfigure(cnf) if err != nil { t.Errorf("Error in prepareConfigure(): %v", err) } cnf.Formats["file"] = "" err = log.prepareConfigure(cnf) if err == nil { t.Errorf("Error in prepareConfigure()") } delete(cnf.Formats, "file") cnf.Graylog2.Protocol = "TCP" err = log.prepareConfigure(cnf) if err != nil { t.Errorf("Error in prepareConfigure(): %v", err) } else if cnf.Graylog2.Protocol != "tcp" { t.Errorf("Error in prepareConfigure()") } cnf.Graylog2.Protocol = "" err = log.prepareConfigure(cnf) if err != nil { t.Errorf("Error in prepareConfigure(): %v", err) } else if cnf.Graylog2.Protocol != "udp" { t.Errorf("Error in prepareConfigure()") } }
// Prepare configuration func (self *LogEssence) prepareConfigure(cnf *Configuration) (err error) { var i b.BackendName var mode b.BackendName var n int if cnf == nil { err = ERROR_CONFIGURATION_IS_NULL return } // Если формат по умолчанию для всех режимов не установлен, используем стандартный формат по умолчанию if cnf.Format == "" { cnf.Format = default_FORMAT } b.DefaultFormat = cnf.Format // Проверка формата по умолчанию _, err = r.CheckFormat(cnf.Format) if err != nil { return } for i = range cnf.Mode { mode = b.CheckMode(i) for n = range cnf.Mode[i] { cnf.Mode[i][n] = l.LevelName(strings.ToUpper(string(cnf.Mode[i][n]))) } if mode != i { cnf.Mode[mode] = cnf.Mode[i] delete(cnf.Mode, i) } } for i = range cnf.Levels { cnf.Levels[i] = l.LevelName(strings.ToUpper(string(cnf.Levels[i]))) mode = b.CheckMode(i) if mode != i { cnf.Levels[mode] = cnf.Levels[i] delete(cnf.Levels, i) } } for i = range cnf.Formats { mode = b.CheckMode(i) if mode != i { cnf.Formats[mode] = cnf.Formats[i] delete(cnf.Formats, i) } // Проверка формата _, err = r.CheckFormat(cnf.Formats[mode]) if err != nil { return } } // Graylog2 protocol switch strings.ToLower(cnf.Graylog2.Protocol) { case "tcp": cnf.Graylog2.Protocol = `tcp` case "udp": cnf.Graylog2.Protocol = `udp` default: cnf.Graylog2.Protocol = `udp` } return }