func (a agent) runMbusHandler(errChan chan error) { handlerFunc := func(req boshmbus.Request) (resp boshmbus.Response) { switch req.Method { case "get_task", "ping", "get_state", "ssh", "start": action := a.actionFactory.Create(req.Method) value, err := action.Run(req.GetPayload()) if err != nil { err = bosherr.WrapError(err, "Action Failed %s", req.Method) resp = boshmbus.NewExceptionResponse(err.Error()) boshlog.Error("Agent", err.Error()) return } resp = boshmbus.NewValueResponse(value) case "apply", "fetch_logs", "stop", "drain": task := a.taskService.StartTask(func() (value interface{}, err error) { action := a.actionFactory.Create(req.Method) value, err = action.Run(req.GetPayload()) return }) resp = boshmbus.NewValueResponse(TaskValue{ AgentTaskId: task.Id, State: string(task.State), }) default: resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method) boshlog.Error("Agent", "Unknown action %s", req.Method) } return } errChan <- a.mbusHandler.Run(handlerFunc) }
func TestError(t *testing.T) { _, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_ERROR) logger.Error("TAG", "some %s info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "ERROR - some awesome info to log")) assert.True(t, matcher.Match(stderr)) }
func main() { app := boshapp.New() err := app.Run(os.Args) if err != nil { logger.Error("Main", err.Error()) os.Exit(1) } }
func TestLogLevelError(t *testing.T) { stdout, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_ERROR) logger.Debug("DEBUG", "some debug log") logger.Info("INFO", "some info log") logger.Error("ERROR", "some error log") }) assert.NotContains(t, string(stdout), "DEBUG") assert.NotContains(t, string(stdout), "INFO") assert.Contains(t, string(stderr), "ERROR") }
func (service asyncTaskService) processTasks() { for { task := <-service.taskChan value, err := task.taskFunc() if err != nil { task.Error = err.Error() task.State = TaskStateFailed boshlog.Error("Task Service", "Failed processing task #%s got: %s", task.Id, err.Error()) } else { task.Value = value task.State = TaskStateDone } service.taskSem <- func() { service.currentTasks[task.Id] = task } } }
It("debug with details", func() { stdout, _ := captureOutputs(func() { logger := NewLogger(LevelDebug) logger.DebugWithDetails("TAG", "some info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "DEBUG - some info to log")) Expect(matcher.Match(stdout)).To(BeTrue()) assert.Contains(GinkgoT(), string(stdout), "\n********************\nawesome\n********************") }) It("error", func() { _, stderr := captureOutputs(func() { logger := NewLogger(LevelError) logger.Error("TAG", "some %s info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "ERROR - some awesome info to log")) Expect(matcher.Match(stderr)).To(BeTrue()) }) It("error with details", func() { _, stderr := captureOutputs(func() { logger := NewLogger(LevelError) logger.ErrorWithDetails("TAG", "some error to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "ERROR - some error to log")) Expect(matcher.Match(stderr)).To(BeTrue())
func init() { Describe("Testing with Ginkgo", func() { It("info", func() { stdout, _ := captureOutputs(func() { logger := NewLogger(LEVEL_INFO) logger.Info("TAG", "some %s info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "INFO - some awesome info to log")) assert.True(GinkgoT(), matcher.Match(stdout)) }) It("debug", func() { stdout, _ := captureOutputs(func() { logger := NewLogger(LEVEL_DEBUG) logger.Debug("TAG", "some %s info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "DEBUG - some awesome info to log")) assert.True(GinkgoT(), matcher.Match(stdout)) }) It("debug with details", func() { stdout, _ := captureOutputs(func() { logger := NewLogger(LEVEL_DEBUG) logger.DebugWithDetails("TAG", "some info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "DEBUG - some info to log")) assert.True(GinkgoT(), matcher.Match(stdout)) assert.Contains(GinkgoT(), string(stdout), "\n********************\nawesome\n********************") }) It("error", func() { _, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_ERROR) logger.Error("TAG", "some %s info to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "ERROR - some awesome info to log")) assert.True(GinkgoT(), matcher.Match(stderr)) }) It("error with details", func() { _, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_ERROR) logger.ErrorWithDetails("TAG", "some error to log", "awesome") }) matcher, _ := regexp.Compile(expectedLogFormat("TAG", "ERROR - some error to log")) assert.True(GinkgoT(), matcher.Match(stderr)) assert.Contains(GinkgoT(), string(stderr), "\n********************\nawesome\n********************") }) It("log level debug", func() { stdout, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_DEBUG) logger.Debug("DEBUG", "some debug log") logger.Info("INFO", "some info log") logger.Error("ERROR", "some error log") }) assert.Contains(GinkgoT(), string(stdout), "DEBUG") assert.Contains(GinkgoT(), string(stdout), "INFO") assert.Contains(GinkgoT(), string(stderr), "ERROR") }) It("log level info", func() { stdout, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_INFO) logger.Debug("DEBUG", "some debug log") logger.Info("INFO", "some info log") logger.Error("ERROR", "some error log") }) assert.NotContains(GinkgoT(), string(stdout), "DEBUG") assert.Contains(GinkgoT(), string(stdout), "INFO") assert.Contains(GinkgoT(), string(stderr), "ERROR") }) It("log level error", func() { stdout, stderr := captureOutputs(func() { logger := NewLogger(LEVEL_ERROR) logger.Debug("DEBUG", "some debug log") logger.Info("INFO", "some info log") logger.Error("ERROR", "some error log") }) assert.NotContains(GinkgoT(), string(stdout), "DEBUG") assert.NotContains(GinkgoT(), string(stdout), "INFO") assert.Contains(GinkgoT(), string(stderr), "ERROR") }) }) }