Ejemplo n.º 1
0
func extractLogContent(logMsg *logmessage.LogMessage, logHeader string) (logContent string) {
	msgText := string(logMsg.GetMessage())
	reg, err := regexp.Compile("[\n\r]+$")
	if err == nil {
		msgText = reg.ReplaceAllString(msgText, "")
	}

	msgLines := strings.Split(msgText, "\n")
	padding := strings.Repeat(" ", len(logHeader))
	coloringFunc := terminal.LogStdoutColor
	logType := "OUT"

	if logMsg.GetMessageType() == logmessage.LogMessage_ERR {
		coloringFunc = terminal.LogStderrColor
		logType = "ERR"
	}

	logContent = fmt.Sprintf("%s %s", logType, msgLines[0])
	for _, msgLine := range msgLines[1:] {
		logContent = fmt.Sprintf("%s\n%s%s", logContent, padding, msgLine)
	}
	logContent = coloringFunc(logContent)

	return
}
Ejemplo n.º 2
0
func (e *LoggregatorEmitter) EmitLogMessage(logMessage *logmessage.LogMessage) {
	messages := splitMessage(string(logMessage.GetMessage()))

	for _, message := range messages {
		if isEmpty(message) {
			continue
		}

		if len(message) > MAX_MESSAGE_BYTE_SIZE {
			logMessage.Message = append([]byte(message)[0:TRUNCATED_OFFSET], TRUNCATED_BYTES...)
		} else {
			logMessage.Message = []byte(message)
		}
		if e.sharedSecret == "" {
			marshalledLogMessage, err := proto.Marshal(logMessage)
			if err != nil {
				e.logger.Errorf("Error marshalling message: %s", err)
				return
			}
			e.LoggregatorClient.Send(marshalledLogMessage)
		} else {
			logEnvelope, err := e.newLogEnvelope(*logMessage.AppId, logMessage)
			if err != nil {
				e.logger.Errorf("Error creating envelope: %s", err)
				return
			}
			marshalledLogEnvelope, err := proto.Marshal(logEnvelope)
			if err != nil {
				e.logger.Errorf("Error marshalling envelope: %s", err)
				return
			}
			e.LoggregatorClient.Send(marshalledLogEnvelope)
		}
	}
}
Ejemplo n.º 3
0
Archivo: start.go Proyecto: nota-ja/cli
func simpleLogMessageOutput(logMsg *logmessage.LogMessage) (msgText string) {
	msgText = string(logMsg.GetMessage())
	reg, err := regexp.Compile("[\n\r]+$")
	if err != nil {
		return
	}
	msgText = reg.ReplaceAllString(msgText, "")
	return
}
Ejemplo n.º 4
0
// ParseLogMessages is a modified version of httpRecent.
// https://github.com/cloudfoundry/loggregator_consumer/blob/89d7fe237afae1e8222554359ec03b72c8466d10/consumer.go#L145
// Also, when using their Recent function, we would get unauthorized errors. If we make the request ourselves, it works.
// TODO eventually figure out the cause of the unauthorized errors
func (c *LogContext) ParseLogMessages(body *io.ReadCloser, contentType string) (*bytes.Buffer, error) {
	_, params, err := mime.ParseMediaType(contentType)
	if err != nil {
		return nil, err
	}

	var msg logmessage.LogMessage
	reader := multipart.NewReader(*body, params["boundary"])
	var buffer bytes.Buffer
	var messages bytes.Buffer
	firstNotInserted := true
	messages.WriteRune('[')
	for part, loopErr := reader.NextPart(); loopErr == nil; part, loopErr = reader.NextPart() {
		// Skip putting a comma if we haven't put the first element in there yet.
		if !firstNotInserted {
			messages.WriteRune(',')
		} else {
			firstNotInserted = false
		}

		// Clear out temporary buffer.
		buffer.Reset()

		msg = logmessage.LogMessage{}
		// Read raw bytes.
		_, err := buffer.ReadFrom(part)
		if err != nil {
			break
		}
		// Try to unmarshal the bytes into a LogMessage struct.
		proto.Unmarshal(buffer.Bytes(), &msg)
		// Marshal the data into json with only the Message.
		json, err := json.Marshal(struct {
			Message string `json:"message"`
		}{
			Message: string(msg.GetMessage()),
		})
		// Write it to our buffer.
		messages.Write(json)
		part.Close()
	}
	messages.WriteRune(']')
	buffer.Reset()
	return &messages, nil
}
Ejemplo n.º 5
0
func ExtractLogContent(logMsg *logmessage.LogMessage, logHeader string) (logContent string) {
	msgText := string(logMsg.GetMessage())
	msgText = newLinesPattern.ReplaceAllString(msgText, "")

	msgLines := strings.Split(msgText, "\n")
	padding := strings.Repeat(" ", utf8.RuneCountInString(logHeader))
	coloringFunc := terminal.LogStdoutColor
	logType := "OUT"

	if logMsg.GetMessageType() == logmessage.LogMessage_ERR {
		coloringFunc = terminal.LogStderrColor
		logType = "ERR"
	}

	logContent = fmt.Sprintf("%s %s", logType, msgLines[0])
	for _, msgLine := range msgLines[1:] {
		logContent = fmt.Sprintf("%s\n%s%s", logContent, padding, msgLine)
	}
	logContent = coloringFunc(logContent)

	return
}
func getLoggregatorMsgString(message *logmessage.LogMessage) string {
	return string(message.GetMessage())
}
Ejemplo n.º 7
0
			legacy_consumer := loggregator_consumer.New(legacyEndpoint, &tls.Config{}, nil)
			messages, err := legacy_consumer.Tail(APP_ID, AUTH_TOKEN)
			Expect(err).NotTo(HaveOccurred())
			var request *http.Request
			Eventually(fakeDoppler.TrafficControllerConnected, 10).Should(Receive(&request))

			Expect(request.URL.Path).To(Equal("/apps/1234/stream"))

			currentTime := time.Now().UnixNano()
			dropsondeMessage := makeDropsondeMessage("Make me Legacy Format", APP_ID, currentTime)
			fakeDoppler.SendLogMessage(dropsondeMessage)

			var receivedMessage *logmessage.LogMessage
			Eventually(messages).Should(Receive(&receivedMessage))

			Expect(receivedMessage.GetMessage()).To(BeEquivalentTo("Make me Legacy Format"))
			Expect(receivedMessage.GetAppId()).To(Equal(APP_ID))
			Expect(receivedMessage.GetTimestamp()).To(Equal(currentTime))

			legacy_consumer.Close()
		})
	})

	Context("Recent", func() {
		It("returns a multi-part HTTP response with all recent messages", func() {
			const messageLength = 5
			expectedMessages := make([][]byte, messageLength)

			for i := 0; i < messageLength; i++ {
				message := makeDropsondeMessage(strconv.Itoa(i), "1234", 1234)
				expectedMessages[i] = message
				MessageType:    events.LogMessage_OUT.Enum(),
				Timestamp:      proto.Int64(1234),
				AppId:          proto.String("AppId"),
				SourceType:     proto.String("SRC"),
				SourceInstance: proto.String("0"),
			},
		}

		bytes, _ := proto.Marshal(envelope)
		msg, err := marshaller.TranslateDropsondeToLegacyLogMessage(bytes)

		Expect(err).To(BeNil())

		var legacyMessage logmessage.LogMessage
		proto.Unmarshal(msg, &legacyMessage)
		Expect(legacyMessage.GetMessage()).To(Equal(message))
		Expect(legacyMessage.GetAppId()).To(Equal("AppId"))
		Expect(legacyMessage.GetSourceName()).To(Equal("SRC"))
		Expect(legacyMessage.GetSourceId()).To(Equal("0"))
		Expect(legacyMessage.GetTimestamp()).To(BeNumerically("==", 1234))
	})

	It("returns an error if it fails to unmarshall the envelope", func() {
		junk := []byte{1, 2, 3}

		msg, err := marshaller.TranslateDropsondeToLegacyLogMessage(junk)

		Expect(msg).To(BeNil())
		Expect(err.Error()).To(ContainSubstring("Unable to unmarshal"))
	})