log4j2.xmllog4j-api-2.5.jarlog4j-core-2.5.jar

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE xml>

<Configuration status="WARN">

<Appenders>

    <Console name="Console" target="SYSTEM_OUT">

        <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>

    </Console>

    <File name="log" fileName="logs.log" append="false">

        <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>

    </File>

</Appenders>   

<Loggers>

    <Logger name="com.dbyl.libarary.utils.Log" level="all">

        <AppenderRef ref="Console"/>

        <AppenderRef ref="log"/>

    </Logger>

    <Root level="trace">

        <AppenderRef ref="Console"/>

        <AppenderRef ref="log"/>

    </Root>

  </Loggers>

</Configuration>

 

 

 

@WebServlet(urlPatterns = "/HomeServlet",asyncSupported=true)

public class HomeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

 

     private static final Logger LOGGER = LogManager.getLogger(HomeServlet.class);

 

     public static void main(String[] args) {

            LOGGER.info("--------------info------------------"+HomeServlet.class);

    }

 

     public void testLog() {

 

 

         LOGGER.trace("Trace Message!");

          LOGGER.debug("Debug Message!");

          LOGGER.info("Info Message!");

          LOGGER.warn("Warn Message!");

          LOGGER.error("Error Message!");

          LOGGER.fatal("Fatal Message!");

        LOGGER.info("--------------info------------------"+HomeServlet.class.getName());

        LOGGER.info("--------------info------------------"+HomeServlet.class);

        LOGGER.debug("------------debug--------------------");

        LOGGER.fatal("---------------fatal-----------------");

        LOGGER.info("--------------------------------");

 

 

 

getLogger(Bar.class.getName())

getLogger(MyAPP.class) //官方推荐的写法

getLogger("HomeServlet")

getLogger()

 

 

public class MyService {

    // instead of Logger logger = LogManager.getLogger(MyService.class):

    private static final ExtLogger logger = ExtLogger.create(MyService.class);

    public void demoExtendedLogger() {

        // ...

        logger.trace("the built-in TRACE level");

        logger.verbose("a custom level: a VERBOSE message");

        logger.debug("the built-in DEBUG level");

        logger.notice("a custom level: a NOTICE message");

        logger.info("the built-in INFO level");

        logger.diag("a custom level: a DIAG message");

        logger.warn("the built-in WARN level");

        logger.error("the built-in ERROR level");

        logger.fatal("the built-in FATAL level");

        logger.notice("java 8 lambda expression only executed if NOTICE is enabled: {}", () -> someMethod());

        // ...

    }

    ...

}