开发者

Setting properties for logging in java

Before you read this, you should know that I am a python developer in uncharted Java territory.

I am trying to set up logging for Java. I was able to get it to work when I set the logging.properties. Like this:

$ /usr/bin/java -server -Xmx512m -Djava.util.logging.config.file= /path/to/logging/file/logging.properties -jar start.jar

The logging.properties file is checked into revision control and in my current set up is to be used in all environments, so I don't want to hard code the logging.FileHandler.pattern in the logging.properties file. So, I tried to move that to the command line, by doing this:

$ /usr/bin/java -server -Xmx512m -Djava.util.logging.config.file= /path/to/logging/file/logging.properties -Djava.util.logging.FileHandler.pattern=/var/log/project_name/solr/solr.log -jar start.jar

But it seems that the FileHandler.pattern wasn't recognized, so the the file never wrote. So, then I thought, well, let's just ditch the logging.properties file and try to throw everything on the command line. So, then I tried this:

$ /usr/bin/java -server -Xmx512m -Djava.util.logging开发者_如何学C.Handler=java.util.logging.FileHandler -D.level=WARNING -Djava.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter -Djava.util.logging.FileHandler.pattern=/var/log/project_name/solr/solr.log -jar start.jar

But that didn't work either. So, I am asking for help.

Three questions:

  1. Why didn't the second example I provided work?
  2. Why didn't the third example I provided work?
  3. Since the logging.properties file is checked into version control and is a shared file, what is the right approach to this? What is the best practice?

When responding, please used small words for us Java newbies. :-)


The -Dxyz.blahbla.bla=foobar is a System property, and not everything could be used as a System property, because there will be some code that will ask for that property, and even though you can put whatever you want, nobody will read it.

For instance, try this code:

class HelloSystemProperty {
     public static void main( String ... args ) {
         System.out.println( System.getProperty("getIt"));
      }
 }

And then run it like:

java -DgetIt=Yeah HelloSystemProperty

So, as you may guess by now you can't just go and put anything that goes into that file as a system property UNLESS you read it your self and process it accordingly.

So if you definitely NEED to modify the logging level by hand as a system property you may want to add code to read that property and set the values, but probably that's too much and I'm not sure if you want to go there.

So using the file should be enough

I hope this helps.


"Hardcoding the FileHandler pattern" might not be such a bad thing, since the pattern can contain tokens that are replaced at runtime. I know %t is replaced by whatever the VM believes is the system temp directory (like /var/tmp or C:\\TEMP), %h is the user's home directory (or rather the "user.home" system property), and there are some more but I don't know all of them.

If you use some of those, the pattern might be sufficiently generic that you don't have to worry about trying to super-generalize it for portability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜