Skip to content

Quick start guide

Aleksander edited this page Aug 27, 2019 · 20 revisions

Setup steps a bit differ for plain java application and for Spring Boot environment:

1. Plain java app

1.1. Add dependency

Add a dependency to you pom.xml file:

<dependency>
    <groupId>com.github.sidssids</groupId>
    <artifactId>block-logger</artifactId>
    <version>1.2.1</version>
</dependency>

1.2. Setup encoder

Use com.github.sidssids.blocklogger.encoder.BlockloggerPatternLayoutEncoder encoder in your logback configuration.

This is how your logback.xml file looks like, if you want to print output to console (for details see logback manual):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="com.github.sidssids.blocklogger.encoder.BlockloggerPatternLayoutEncoder">
            <pattern>%d [%-10thread] %-5level %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Root Logger -->
    <root level="TRACE">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

1.3. Enjoy

Now you can use LogBlock in you code in couple with standard org.slf4j.Logger:

public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(Main.class);        // slf4 logger

    logger.info("outside block");

    try (LogBlock log = LogBlockFactory.info(Main.class, "block")) {   // start block
        logger.info("inside block");                                   // use slf4 logger inside the block
    }                                                                  // LogBlock is autoclosable
}

Will give you the output:

2018-04-25 13:04:54,631 [main      ] INFO  outside block
2018-04-25 13:04:54,682 [main      ] INFO  [+] block
2018-04-25 13:04:54,682 [main      ] INFO      inside block
2018-04-25 13:04:54,686 [main      ] INFO  [-] block (PT0.004S)

To get more information, see Usage

2. Spring Boot

2.1. Add dependency

Add a dependencies to you pom.xml file:

<dependency>
    <groupId>com.github.sidssids</groupId>
    <artifactId>block-logger</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Spring Boot AOP dependency is required to use @BlockLoggable annotation

2.2. Setup encoder

Use com.github.sidssids.blocklogger.encoder.BlockloggerPatternLayoutEncoder encoder in your logback configuration. You need use your own logback.xml (or logback-spring.xml) configuration file instead of the default Spring one

The simplest way

  • create logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Include useful things from Spring Boot Logging -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!-- Define console appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="com.github.sidssids.blocklogger.encoder.BlockloggerPatternLayoutEncoder">
            <!-- This property is set up through Spring environment  -->
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>
  • add your logback.xml file to root of a classpath.

For details about Spring Boot Logging configuration see this how-to guide

2.3. Add annotation

Add annotation @EnableLogBlock to your configuration, for example:

@SpringBootApplication
@EnableLogBlock
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

2.4. Enjoy

Now you can use LogBlock and/or @BlockLoggable annotation in you code in couple with standard org.slf4j.Logger:

@RestController
public class TestController {

    @Autowired
    Logger logger;

    @BlockLoggable       // just one annotation
    @GetMapping("/test")
    public void test() {
        logger.info("Hello, world");
    }

    @BlockLoggable
    @GetMapping("/test2")
    public void test2() {
        try (LogBlock log = LogBlockFactory.info(TestController.class, "inner block")) {
            logger.info("Hello, world");
        }
    }
}

Invoking GET localhost:8080/test will give you the output:

2018-04-25 13:04:54,682 [main      ] INFO  [+] test
2018-04-25 13:04:54,682 [main      ] INFO      Hello, world
2018-04-25 13:04:54,686 [main      ] INFO  [-] test (PT0.004S)

Invoking GET localhost:8080/test2 will give you the output:

2018-04-25 13:04:54,682 [main      ] INFO  [+] test2
2018-04-25 13:04:54,682 [main      ] INFO      [+] inner block
2018-04-25 13:04:54,682 [main      ] INFO          Hello, world
2018-04-25 13:04:54,686 [main      ] INFO      [-] inner block (PT0.004S)
2018-04-25 13:04:54,686 [main      ] INFO  [-] test2 (PT0.004S)

To get more information, see Usage

Clone this wiki locally