Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vert.x: best way to log to file

What is the fastest async way to log to file in Vert.x?

The aim is to write logs from loggers from different classes (i.e. Class1, Class2 etc) to 1 file (something like 'console.log')

like image 641
xardas Avatar asked Sep 08 '15 10:09

xardas


2 Answers

Vert.x uses the JDK bundled JUL logging framework to avoid shipping additional dependencies. However it allows to append a custom logger implementation.

Assuming that you want to stick to the default logging facility, customizing the log handler would then be as easy as droping a logging file and referencing it through the java.util.logging.config.file system property:

  • For example you can drop the logging configuration file under a config directory under the root path of your (fat) jar which may look as follows:

    handlers = java.util.logging.MyFileHandler
    config   =
    #...
    
  • You should then refrence that file in a system property as follows when starting your Vert.x application:

    -Djava.util.logging.config.file=config/logging.properties
    
  • You can then access the Logger object in your classes as follows:

    Logger logger = LoggerFactory.getLogger("some.package.MyClass");
    
  • Use that logger to log messages that will be handled by the configured handler(s):

    logger.info("some informative message");
    

Note the use of a custom log handler in the properties file to emphasis the possibily of appending your own handler (which may extend the default FileHandler).

Check the Vert.x documentation for more informations on how to use explore the logging feature.

like image 97
tmarwen Avatar answered Oct 22 '22 11:10

tmarwen


Most of loggers are async from the beginning , i.e. they are not write information immediately. Logs are stored into buffer, which is flushed by timeout or when it is full. So slf4j + log4 is good enough for most cases.

like image 2
Igor Azarny Avatar answered Oct 22 '22 11:10

Igor Azarny