Opening Java messages with too many log files

This is a bit strange, but I'm new to the registration frame and its use of properties. All the questions I can find on googling are "how do I make a record open with multiple files?" but today my question is how to make it stop the execution of several files at the same time. Here we go...

First of all: this project is limited to using java.util.logging, no, I can not switch to log4j or any other third-party package, yes, I know that they should be more awesome. :-)

So, when this applet launches, it launches this bit of code:

import java.util.logging.Logger; import java.util.logging.LogManager; // in startup routine: LogManager.getLogManager().readConfiguration( this.getClass().getResourceAsStream("/logging.properties")); 

pull the properties file from the JAR and apply them, which works. It is assumed that readConfiguration() reset all existing settings when starting the virtual machine. The rest of the project has lines like

 private final static Logger LOGGER = Logger.getLogger(NameOfClass.class.getName()); 

which I think is pretty standard. All our classes are in one package (for example, this is the TheProject project), and the funky name names / property hierarchy follow the same convention, because that's what java.util.logging is like.

The logging.properties file started as a copy of the one that comes with the Java 6 SE JRE, and then was modified. Now it looks like this:

 handlers=java.util.logging.FileHandler,java.util.logging.ConsoleHandler # Default global logging level. .level=INFO # Loggers # ------------------------------------------ # Loggers are usually attached to packages. # Here, the level for each package is specified. # The global level is used by default, so levels # specified here simply act as an override. java.level = INFO javax.swing.level = INFO sun.awt.level = INFO theproject.level = ALL # Handlers # ----------------------------------------- theproject.handlers=java.util.logging.FileHandler # Override of global logging level java.util.logging.FileHandler.level=ALL # Naming style for the output file: java.util.logging.FileHandler.pattern=/path/to/logfiles/TheProject%u.%g.log 

All that "works" is that the log messages are displayed on the Java console, as well as displayed on files on disk. Here's the weird part: as soon as the applet starts, two files open simultaneously, both TheProject0.0.log and TheProject1.0.log. When log messages are started, they are displayed in both files at the same time. Both files are an exact copy of each other at any time, including when they reach the maximum size and rotate (both!).

While only one JRE VM is running, I checked.

At any given time, both files open or both close, I checked. It is not like one is open for a longer or shorter time than the other.

The% u icon, which differs between the two file names, is documented as a "unique conflict resolution number" if the log file is already opened by another process, but I think it is not. Since both logs receive the same data and nothing else opens the file. (proof: Windows will not allow me to delete a single file while the virtual machine is running, but after the VM finally quits.)

Am I doing something stupid in the properties file, or am I misunderstanding how to load properties correctly or ...?

+4
source share
2 answers

@Joop nailed it (in a comment that cannot be marked as an answer). It turns out that installing the handler is an additive process, it does not just overwrite previous settings. It seems really unintuitive to me, but it's java.util.logging for ya ... Leaving all the other properties in place, but removing the handler assignment, that was the way to go.

I also use the reset () call suggested by @jschoen because this is just a reasonable thing!

Many thanks to all of you. I'm not sure how to mark it all as “closed” ... time to play with the site.

+1
source

I don't think readConfiguration() works the way you think. Create JavaDocs:

Re-initialize registration properties and re-read configuration registration from this stream, which should be in java.util.Properties. The PropertyChangeEvent property will be launched after the properties are read.

Definitions of any log level in the new configuration file will be applied using Logger.setLevel () if the target Logger exists.

I'm not sure if this really resets the logging configuration or not, or just adds / updates the current configuration.

I think you might need to call reset() before calling readConfiguration() .

Reset logging configuration.

For all the mentioned loggers, the reset operation deletes and closes all Handlers and (except for the root registrar) set the level to zero. The root logger level is set to Level.INFO.

Edit: To show that this is not a problem with your properties file, you can create a class called test in theproject package, as shown below, and run it. Just remember to delete the existing log files so that you know how many they were created at startup.

 package theproject; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class Test { private final static Logger LOGGER = Logger.getLogger(Test.class.getName()); public static void main(String[] args) throws Exception { LogManager.getLogManager().readConfiguration( Test.class.getResourceAsStream("/logging.properties")); // The only way I could get it to create two log files was to uncomment the line below that // adds another FileHandler to the root logger. // LogManager.getLogManager().getLogger("").addHandler(new FileHandler("/path/to/logfiles/TheProject%u.%g.log")); Test test = new Test(); } public Test(){ LOGGER.log(Level.INFO, "Info"); } } 
+3
source

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


All Articles