javaloggingfilehandler

Java Logger FileHandler naming convention


I have a servlet deployed. I am a little confused about the log files being generated. particularly i am not sure which one to look at.

There are several generated with the same data.

I am using the following code to initialize my Filehandler.

Logger logger = Logger.getLogger(Global.TAG);
String path = realPath + "myappname" + ".%g.log";
FileHandler fh = new FileHandler(path, 10240000, 1000, true);
logger.addHandler(fh);
logger.setLevel(Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logInfo("Logger Configured!");

Notice that i have %g in there for when files are rotated.

The files generated are something like this.

myappname.0.log
myappname.0.log.1
myappname.0.log.2
myappname.0.log.3
myappname.0.log.4
myappname.0.log.5
myappname.0.log.6
myappname.1.log
myappname.1.log.2
myappname.1.log.4
myappname.1.log.5
myappname.2.log
myappname.2.log.1
myappname.2.log.2
myappname.2.log.3
myappname.2.log.4
myappname.2.log.5
myappname.2.log.6
myappname.3.log
myappname.3.log.1
myappname.3.log.3
myappname.3.log.6

The servlet was undeployed and redeployed several times in the last 2-3 weeks in which the logs were generated.

What i have noticed is that logs with the same name (except the last bit after the ".log" part) have the same logging statements. Some files have more and some less.

What gives? How should i be reading this?


Solution

  • You are programmatically opening a FileHandler on deploy so you have close and remove the FileHandler from the logger on undeploy.

    Per the FileHandler docs:

    Normally the "%u" unique field is set to 0. However, if the FileHandler tries to open the filename and finds the file is currently in use by another process it will increment the unique number field and try again. This will be repeated until FileHandler finds a file name that is not currently in use. If there is a conflict and no "%u" field has been specified, it will be added at the end of the filename after a dot. (This will be after any automatically added generation number.)

    You have to either setup your FileHandler via the logging.properties so it is only installed once or you have move your setup and teardown code to a ServletContextListener