Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use of the vertx-default-jul-logging.properties file to control the format and number of files?

Tags:

java

vert.x

I'm struggling with this Vertx package that I am told to use. All logging is to be done with Vertx (io.vertx.core.logging.Logger)

I am able to get logging out to a file today with the use of the vertx-default-jul-logging.properties file. What I have been stuck on are these 3 things. (And yes i've been reading lots of documents online. I just need to see a solid working properties file with the stuff written out) 1. How to have multiple output files specified so some go to mmddyy class.method.log and a second file to mmddyy audit.log

  1. How do I control the output of the content so the logs have the format of {"yy-MM-dd"} [%-5level] %class{15}.%M:%L - %msg%n

  2. Any solid resource that you can point me to that has some tutorial on writing the proper stuff in this vertx-default-jul-logging.properties file as I've spent my day on this and don't think it should take this long to get.

Appreciate your help.

like image 548
Elijah Avatar asked Jan 19 '26 16:01

Elijah


2 Answers

You should start by reading the small chapter http://vertx.io/docs/vertx-core/java/#_logging from the official documentation. It is important to see that it explains that even though JUL is the default you can use any other logging framework if you so desire:

  • log4j
  • slf4j
  • log4j2

What the vert.x logger class gives you is just an abstraction, that no matter what is beneath is hidden from your code and if you desire to change it you don't need to change your code.

If you still want to use JUL the formatting of your logs are configurable on the config:

# here you specify who's going to handle the logs
# for this example I'll use only the console
handlers=java.util.logging.ConsoleHandler
# now you specify the format
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
# and now you tell the console to use that format
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

These are the basic of the configuration, in order to control the format the syntax is specified here http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

like image 99
Paulo Lopes Avatar answered Jan 22 '26 07:01

Paulo Lopes


First of all, if you still want to use JUL discard this answer; otherwise, keep reading.

I'm using Vert.x 3.3.3 and redirecting all the logs through/to Log4J 2.x — a similar approach applies for Logback/SLF4J.

You first need to add/include these dependencies (I'm using Gradle; update the versions if available):

compile 'com.lmax:disruptor:3.3.5'
compile 'org.apache.logging.log4j:log4j-1.2-api:2.7'
compile 'org.apache.logging.log4j:log4j-jcl:2.7'
compile 'org.apache.logging.log4j:log4j-jul:2.7'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.7'

Then make a file named ${projectRoot}/src/main/resources/log4j2.xml, and add the following "content" into it:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Properties>
    <Property name="log-pattern">%d{MM-dd-yyyy HH:mm:ss.SSS} |- %highlight{%5p}{TRACE=blue, DEBUG=green, INFO=green, WARN=yellow, ERROR=red, FATAL=red} in %style{%C{1}:%L}{cyan} [%style{%t}{magenta}] - %m%n</Property>
  </Properties>

  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="${log-pattern}" />
    </Console>
  </Appenders>

  <!-- Logger levels: trace, debug, info, warn, error, fatal -->
  <Loggers>
    <AsyncLogger name="your.package.name" level="DEBUG" additivity="false" includeLocation="true">
      <AppenderRef ref="Console" />
    </AsyncLogger>

    <Root level="WARN">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

...finally, define a LOGGER in your class(es) in order to actually "do the logging".

...
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class Application extends AbstractVerticle {
  final Logger LOGGER = LogManager.getLogger();

  ...
}

That will do the trick — but using Log4J 2.x. Keep in mind, Vert.x is asynchronous by nature (or sort of) and this configuration also uses asynchronous logger(s), hence the need for the (awesome IMO) LMAX Disruptor.

Also notice that you need to change the package name(s) for your application (see the AsyncLogger definition; I used your.package.name) and eventually, the log-pattern for the format you are looking for.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!