Esempio n. 1
0
func Prettify(logMessage *events.LogMessage) string {
	entry := chug.ChugLogMessage(logMessage)

	// TODO: Or, do we use GetSourceType() for raw and Json source for pretty?
	color, ok := colorLookup[strings.Split(entry.LogMessage.GetSourceType(), ":")[0]]
	if !ok {
		color = colors.ColorDefault
	}

	sourcePrefix := fmt.Sprintf("[%s%s%s|%s%s%s]", color, entry.LogMessage.GetSourceType(), colors.ColorDefault, color, entry.LogMessage.GetSourceInstance(), colors.ColorDefault)
	colorWidth := len(color)*2 + len(colors.ColorDefault)*2
	sourcePrefixWidth := strconv.Itoa(22 + colorWidth)

	var components []string
	components = append(components, fmt.Sprintf("%-"+sourcePrefixWidth+"s", sourcePrefix))
	var whichFunc func(chug.Entry) []string

	if entry.IsLager {
		whichFunc = prettyPrintLog
	} else {
		whichFunc = prettyPrintRaw
	}

	components = append(components, whichFunc(entry)...)
	return strings.Join(components, " ")
}
			SourceInstance: &sourceInstance,
		}
	}

	verifyEntryIsRaw := func() {
		Expect(entry.IsLager).To(BeFalse())
		Expect(entry.Log).To(BeZero())
		Expect(entry.Raw).To(Equal(input))
	}

	Context("when the message has lager to chug", func() {
		It("chugs a lager message", func() {
			input = []byte(`{"timestamp":"1429296198.620077372","source":"rep","message":"rep.event-consumer.operation-stream.executing-container-operation.succeeded-fetch-container","log_level":1,"data":{"container-guid":"app-9eb203ad-72f3-4f26-6424-48f20dc12298","session":"7.1.10","trace":"trace-me-now"}}`)
			logMessage := buildLogMessage("", "", time.Time{}, input)

			entry := chug.ChugLogMessage(logMessage)

			Expect(entry).ToNot(BeNil())
			Expect(entry.LogMessage).To(Equal(logMessage))
			Expect(entry.Raw).To(Equal(input))
			Expect(entry.IsLager).To(BeTrue())
		})

		It("chugs a lager message prepended by junk", func() {
			input = []byte(`JUNK JUNK JUNK {"timestamp":"1429296198.620077372","source":"rep","message":"rep.event-consumer.operation-stream.executing-container-operation.succeeded-fetch-container","log_level":1,"data":{"container-guid":"app-9eb203ad-72f3-4f26-6424-48f20dc12298","session":"7.1.10","trace":"trace-me-now"}}`)
			logMessage := buildLogMessage("", "", time.Time{}, input)

			entry := chug.ChugLogMessage(logMessage)

			Expect(entry).ToNot(BeNil())
			Expect(entry.LogMessage).To(Equal(logMessage))