Java: Log4J

First we have a bin/res/

	# Set root category priority to INFO and its only appender to CONSOLE.
	log4j.rootCategory=INFO, CONSOLE

	# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
	log4j.appender.CONSOLE.layout.ConversionPattern=%-4r %-5p %c{1} %x - %m%n

We're saying in the first uncommented line the default message is an INFO message and it is appended to a console as defined below it.

We're defining the console to be the usual log4j console, then giving it various properties such as the fact we'll be outputting via a pattern as defined in ConversionPattern. The pattern's code is explained here


	Reader isr = new InputStreamReader(Main.class.getResourceAsStream("/res/"));
	Properties p = new Properties();
	Logger l = Logger.getLogger(Main.class);"a message");

First we're loading the properties file, and load it into a Properties object and pass that to the PropertyConfiguration to configure log4j. Then you create a Logger with the name of your current class. Then the info output will output via the configuration parameters set above.

java java-log4j

Edit on github
comments powered by Disqus
Click me