Exemplo n.º 1
0
/*
Creates and returns a new Context object. Because some
items are absolutely required by this object and the processes
that use it, this method will panic if it gets an invalid
config param from the command line, or if it cannot set up some
essential services, such as logging.

This object is meant to used as a singleton with any of the
stand-along processing services (bag_processor, bag_restorer,
cleanup, etc.).
*/
func NewContext(config *models.Config) (context *Context) {
	context = &Context{
		succeeded: int64(0),
		failed:    int64(0),
	}
	context.Config = config
	context.MessageLog, context.pathToLogFile = logger.InitLogger(config)
	context.JsonLog, context.pathToJsonLog = logger.InitJsonLogger(config)
	context.VolumeClient = network.NewVolumeClient(context.Config.VolumeServicePort)
	context.NSQClient = network.NewNSQClient(context.Config.NsqdHttpAddress)
	context.initPharosClient()
	return context
}
Exemplo n.º 2
0
func TestInitLogger(t *testing.T) {
	config := getLoggingTestConfig(t)
	defer teardownLoggerTest(config)
	log, filename := logger.InitLogger(config)
	log.Error("Test Message")
	logFile := filepath.Join(config.AbsLogDirectory(), path.Base(os.Args[0])+".log")
	if !fileutil.FileExists(logFile) {
		t.Errorf("Log file does not exist at %s", logFile)
	}
	if filename != logFile {
		t.Errorf("Expected log file path '%s', got '%s'", logFile, filename)
	}
	data, err := ioutil.ReadFile(logFile)
	if err != nil {
		t.Error(err)
	}
	if false == strings.HasSuffix(string(data), "Test Message\n") {
		t.Error("Expected message was not in the message log.")
	}
}