Can I configure two FileHandlers in the same logging.properties file?

Using the log classes in java.util.logging, is it possible to configure two different FileHandlers with different formats that will write different log data to two different files?

I am currently using the logging.properties file, and the line of handlers is not encouraging:

handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

I do not see how I could distinguish two java.util.logging.FileHandlerlater in the file.

Look at the related questions, it seems that switching to Log4J will give me the desired flexibility, but I would prefer to avoid depending on another library if the JSE log library could be turned into what I want somehow.

+3
source share
3 answers

API No.

, , Log4J .

+4

, , ,

public static void main(String args[])  {

    Logger logger=Logger.getLogger("");
    FileHandler fh=new FileHandler("<yourPath" +
            "%g.txt",20000,5);
    fh.setFormatter(new SimpleFormatter());
    FileHandler fd=new FileHandler("yourSecondPath" +
            "%g.txt",20000,5);
    fd.setFormatter(new SimpleFormatter());

    logger.addHandler(fd);
}

simpleFormaters , logging.properties,

0

You can create your own log level by expanding the Level class; just give it a unique identifier.

import java.util.logging.*;

public class CustomLogLevel extends Level
{
  public static void main(String[] args) 
  {
    Logger log = Logger.getLogger("robertgrant.org");
    Level templevel = Level.WARNING;
    Level level = new CustomLogLevel("Rob Level", templevel.intValue());
    Level customlevel = level.parse("Rob Level");
    log.log(customlevel, "This is from a custom level");
  }

  public CustomLogLevel(String name, int value){
    super(name, value);
  }
}
-1
source

Source: https://habr.com/ru/post/1724929/


All Articles