initial structure and tests
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
|
||||
import com.baeldung.structuredlogging.User;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static net.logstash.logback.argument.StructuredArguments.kv;
|
||||
|
||||
public class StructuredLog4jExampleUnitTest {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger("logger_name_example");
|
||||
|
||||
@Test
|
||||
void whenInfoLoggingData_thenFormatItCorrectly() {
|
||||
User user = new User("1", "John Doe", "123456");
|
||||
|
||||
logger.atInfo().setMessage("Processed user succesfully")
|
||||
.addKeyValue("user_info", user)
|
||||
.log();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenStructuredLog_whenUseLog4j_thenExtractCorrectInformation() {
|
||||
User user = new User("1", "John Doe", "123456");
|
||||
|
||||
try {
|
||||
throwExceptionMethod();
|
||||
} catch (RuntimeException ex) {
|
||||
logger.atError().addKeyValue("user_info", user)
|
||||
.setMessage("Error processing given user")
|
||||
.addKeyValue("exception_class", ex.getClass().getSimpleName())
|
||||
.addKeyValue("error_message", ex.getMessage())
|
||||
.log();
|
||||
}
|
||||
}
|
||||
|
||||
private void throwExceptionMethod() {
|
||||
throw new RuntimeException("Error saving user data", new AssertionError());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="INFO">
|
||||
<Appenders>
|
||||
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||
<JsonLayout compact="true" eventEol="true" objectMessageAsJsonObject="true" properties="true"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="INFO">
|
||||
<AppenderRef ref="stdout"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user