views:

283

answers:

2

I have a basic facility for allowing users to remotely apply changes to the logging files in my application. Some logs are configured using java.util.logging properties files, and some are configured using log4j/log4cplus-style properties files. I'd like to do some basic validation of the properties that users try to apply. Namely, I want to assure the following:

  • Every logging.properties file must always contain at least a root logger/logging level
  • The logger/level must be set to a valid value. That is, they should not be able to set .level = GIBBERISH or anything like that.
  • I'll probably allow them to set MaxFileSize and MaxBackupIndex (log4j), and .limit and .count properties (java.util.logging), too.

What's the best way to accomplish this? I can obviously just loop over the keys and values in a Properties object and look for their values in a hard-coded Map or some other data structure that tells what valid properties are, but I'm trying to come up with a solution that's a little more elegant than that.

+1  A: 

The problem with running any set of partial syntax checks against the properties files is that they'll always be inadequate by definition unless you capture every partial variation acceptable by the logging system, in which case you'll have recreated a portion of the logging system. No matter what properties you choose to validate theres bound to be additional ways to submit broken files.

Rather than testing for individual properties, why not create an additional (temporary, for the scope of the check only) logger object based on the input file and detect if it throws an error?

Steve B.
I like this idea, but maybe I'm not familiar enough with the APIs to implement it. How do I create a temporary Logger using a set of uploaded Properties? I don't see any obvious methods for doing this in the API docs.
Jeff
@Steve B - +1 on your reasoning; unfortunately the problem is that neither java.util.logging.LogManager nor Log4j's PropertyConfigurator throw an actual exception. They both ignore anything they don't understand, at best printing errors to System.err in some cases.
ChssPly76
A: 

The "elegant" solution would be to write a rule-based engine for checking sets of name-value pairs. But IMO that is totally over the top for this use-case ... unless the checks are far more complex than I imagine.

I'd say that the simple (inelegant) solution is best in this case.

Stephen C