Lorenzo De Francesco
Lorenzo De Francesco

Reputation: 641

VertX and Log4j cannot get configuration

I am working with VertX, I want to use Log4J as logger for my code and for the exthernal libraries.

So I wrote down this piece of code:

import org.apache.log4j.Logger;

public class Main extends AbstractVerticle {

   final static Logger logger = Logger.getLogger("Gimli");

   @Override
   public void start(Future<Void> fut) throws Exception {
      System.setProperty("vertx.logger-delegate-factory-class-name", " io.vertx.core.logging.Log4j2LogDelegateFactory"); // Default logger

    logger.debug("Debug log");
    logger.error("Error log");
    logger.warn("Warning log");
  }
}

and I put into src/main/resources the file log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
  <Appenders>
    <RollingFile name="app_file" append="true" fileName="/var/log/seachlog.log" filePattern="/var/log/vertx/$${date:yyyy-MM}/seachlog-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
        <Policies>
            <OnStartupTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="5MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>
    <RollingFile name="vertx_file" append="true" fileName="/var/log/vertx.log" filePattern="/var/log/vertx/$${date:yyyy-MM}/vertx-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
        <Policies>
            <OnStartupTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="5MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <!-- <LogStashJSONLayout/> -->
        <PatternLayout pattern="%d{ISO8601} %-5p %c:%L - sfsdfsdfsdfsdf %m%n" />
    </Console>
</Appenders>

<Loggers>
    <Logger name="Gimli" level="DEBUG">
        <!-- <AppenderRef ref="vertx_rollingFile" /> -->
        <!-- <AppenderRef ref="vertx_socket" /> -->
        <AppenderRef ref="STDOUT"/>
        <AppenderRef ref="vertx_file" />
    </Logger>
    <Root level="DEBUG">
        <!-- <AppenderRef ref="vertx_socket" /> -->
        <AppenderRef ref="STDOUT"/>
        <AppenderRef ref="app_file" />
    </Root>
</Loggers>

But the output seems not changing:

2017-12-09 09:21:34,503 DEBUG [vert.x-eventloop-thread-0] [Gimli] Debug log (Main.java:31)
2017-12-09 09:21:34,505 ERROR [vert.x-eventloop-thread-0] [Gimli] Error log (Main.java:32)
2017-12-09 09:21:34,507 WARN  [vert.x-eventloop-thread-0] [Gimli] Warning log (Main.java:33)

How can I set the configuration file correctly?

Upvotes: 2

Views: 3051

Answers (2)

Lorenzo De Francesco
Lorenzo De Francesco

Reputation: 641

Finally I resolved, the main problem was in the maven configuration. I added the following lines to my build script

 <build>
   <resources>
      <resource>
        <directory>/</directory>
        <includes>
          <include>config.json</include>
        </includes>
      </resource>
    </resources>
 </build>

this way maven would consider / the resources folder and not /src/main/resources so log4j cannot find the log4j.properties file

Upvotes: 0

kubacech
kubacech

Reputation: 117

First of all it looks like you are combining log4j with log4j2 config file. That could be reason why your config is not loaded.

But I recommend to use vert.x logging feature - you can use log4j or log4j2 with it too. You will define your loggers as this:

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

Logger logger = LoggerFactory.getLogger("Gimli");

then use this normally as you did. so:

logger.debug("Debug log");
logger.error("Error log");
logger.warn("Warning log");

next you will keep your config in src/main/resources. Just be careful because this is log4j2 config. So it should be named log4j2.xml.
And now you just need to specify logger delegate factory for vertx. For log4j or log4j2 its:

io.vertx.core.logging.Log4jLogDelegateFactory 

or

io.vertx.core.logging.Log4j2LogDelegateFactory

you can setup system property vertx.logger-delegate-factory-class-name or you can specify VM argument as this:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory

(for log4j2)

More info in documentation

Upvotes: 6

Related Questions