package com.ankurm.structuredlogging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * A StructuredLogFormatter is a plain function from a real logging
 * event to a String, so it's unit-testable without a Spring context or
 * capturing console output at all: build a real Logback LoggingEvent
 * (the same class Logback hands every appender/encoder) and call
 * format() on it directly.
 */
class PipeDelimitedLogFormatterTest {

    @Test
    void formatProducesAPipeDelimitedLineInsteadOfJson() {
        Logger logger = (Logger) LoggerFactory.getLogger("com.ankurm.structuredlogging.SomeClass");
        LoggingEvent event = new LoggingEvent();
        event.setLoggerName(logger.getName());
        event.setLevel(Level.WARN);
        event.setMessage("disk usage above threshold");

        String formatted = new PipeDelimitedLogFormatter().format(event);

        assertThat(formatted).isEqualTo("WARN|com.ankurm.structuredlogging.SomeClass|disk usage above threshold\n");
        assertThat(formatted).doesNotContain("{");

        Transcript t = new Transcript("31-pipe-delimited-format.txt");
        t.line("StructuredLogFormatter#format() called directly on a real Logback LoggingEvent, no Spring context involved:");
        t.line(formatted.strip());
        t.write();
    }
}
