简体   繁体   中英

Logging in tomcat doesn't work

I have fullproject based on spring with using some Controllers, DAO and other web application elements. And now i want to configure log4j2. I do it:

<dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.1</version>
    </dependency>

create log4j2.xml in src/main/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <logger name="com.myproject" level="INFO" />
    </Loggers>
</Configuration>

and then i create class with main(String [] args) method which have to output some info logs:

public class MainClass{
    public static void main(String[] args){

        Logger logger = LogManager.getLogger(MainClass.class.getName());
        logger.info("Entering Log4j Example.");


    }
}

for run application i use:

<groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.3.2</version>
          <configuration>
              <mainClass>com.myproject.MainClass</mainClass>
          </configuration>

And all it i do in redactor, which has own console. And deploy it with tomcat7 maven plugin.

Please say me, do i make it all in correct way or wrong. And if in correct when i can see result of these logs, because in redactor console i don't see and in tomcat logs too

You forgot the destination appender:

<Loggers>
  <Logger name="com.myproject" level="INFO">
    <AppenderRef ref="CONSOLE"/>
  </Logger>
</Loggers>

By the way, you have confused a standalone application with a web application . In Tomcat, you need a web application with servlets, jsp's, etc. You may want to see How To Create A Web Application Project With Maven .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM