Giter Club home page Giter Club logo

slf4j-log4j2-demo's Introduction

Build Status License

本示例提供slf4j+log4j2的使用示例和规范示范配置

Requirements

  • IntelliJ IDEA 2017.2 or later
  • SpringBoot 2.1.4.RELEASE

Backwards compatibility

  • For SpringBoot 1.5.19.RELEASE, import manually:
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.11.2</version>
</dependency>

Getting Started

Who Uses It

Communication

Build Project

At this point your project should build without error. If you are having problem, contact me by Communication

Author

Licenses

All source code is licensed under the MIT License.

How To Use

  • run or debug : Slf4jLog4j2DemoApplication

  • import dependency manually, dependencies about slf4j and log4j2 has been integrated with an overall log4j-slf4j-impl

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.11.2</version>
</dependency>

don't forget to exclude dependency:spring-boot-starter-logging from spring-boot-starter when using spring-boot-starter or spring-boot-starter-web

<!--spring boot starter-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <!-- 去除logback依赖 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • instanciate log: use lombok or use slf4j
import lombok.*;
import lombok.extern.slf4j.Slf4j;
...
@Slf4j
public class Slf4jLog4j2DemoApplication {

}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...    
private static final Logger logger = LoggerFactory.getLogger(Slf4jLog4j2DemoApplication.class);
  • see Slf4jLog4j2DemoApplication
...
    ExampleEntity entity = new ExampleEntity("0", "alpha");

    // 使用lombok->log打印日志
    // debug
    log.debug("{}", entity.toString());

    // warn
    try {
        throw new Exception("程序异常");
    } catch (Exception e) {
        log.warn("{}", e.toString());
        log.warn("{}", e.getMessage());
        log.warn("", e);
    }

    // info
    // 需要自行实现toString
    log.info("{}", entity.toString());
    log.info("{}", entity);

    // error
    try {
        throw new RuntimeException("运行时异常");
    } catch (RuntimeException e) {
        log.error("{}", e.toString());
        log.error("{}", e.getMessage());
        log.error("", e);
    }
  • setup log configure path at application.yml / application.properties
logging:
  config: classpath:log4j2.xml
  • to modify log4j2 specific configuration properties, refer to log4j2.xml

  • notice: if you are not using embedded tomcat or jetty container, modify log file location:

...
<Property name="filePath">${sys:catalina.home}/logs/slf4j-log4j2-demo/</Property>
...
  • look up log from generated log file, such as logs/all.log、info.log、error.log in root directory

  • make sure log4j2.xml below root directory or config directory when deployed. Usually we extracted this config file from jar.

  • AsyncLogger, todo

<!--log4j2的AsyncLogger 需要添加-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.3.7</version>
</dependency>

slf4j-log4j2-demo's People

Contributors

r2ys avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.