You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Niko Schweitzer <Sc...@nue.et-inf.uni-siegen.de> on 2001/10/12 09:26:46 UTC
Log different categories to different logfile
Hi,
I would like to log diefferent log categories to different logfiles but I
dont see
a way how to do this. I have included a short test-class that should
describe my problem.
I want to use three Log-categories LOG, LOGTime and LOGMem.
The output of each categoty should go to a separate logfile, eg.
deflog.log, timelog.log and memlog.log.
Any idea how to do this?
Kind regards,
Niko Schweitzer
-------------------------------------------------------
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
public class TempTestClass {
// <log4j> _________________________________________________________//
private static String sDirWork =
System.getProperty("user.dir");
private static String sDirLog4jConf = "log4jconfig";
private static String sDirLogfiles = "log4jlogfiles";
private static String sFS = File.separator;
private final static String sClassName =
TempTestClass.class.getName();
private static String sLog4jConfFile = sDirWork + sFS +
sDirLog4jConf + sFS
+
"log4jTimingConfig.lcf";
private static String sLog4jLogFile = sDirWork + sFS +
sDirLogfiles + sFS
+ "defaultLogFile.log";
private final static Category LOG =
Category.getInstance(sClassName);
private final static Category LOGTime =
Category.getInstance(sClassName + ".time");
private final static Category LOGMem =
Category.getInstance(sClassName + ".mem");
// </log4j> _________________________________________________________//
/** **********************************************************
* Method main
*
*
* @param args
*/
public static void main(String[] args) {
System.out.println(" hallo");
LOG.debug(" -> to default logfile");
LOGTime.debug(" -> to timing logfile");
LOGMem.debug(" -> to memory logfile");
}
/** **********************************************************
* Method initLog4j
*
*/
private static void initLog4j() {
//
/--------------------------------------------------------------------------
-//
// modify categories Priorities to change output level
// for instance if Priority is set to INFO no debug-logs will be output
LOG.setPriority(org.apache.log4j.Priority.DEBUG);
// test if log4j was already initializes
// Skip default initialization if the system property
// log4j.defaultInitOverride is set to any value other than "false" or
"null".
// equivalent to
// the DEFAULT_INIT_OVERRIDE_KEY indicates if Log4j has been initialized
// if this proerty is not set
if (System.getProperty(Category.DEFAULT_INIT_OVERRIDE_KEY) == null) {
System.setProperty(Category.DEFAULT_INIT_OVERRIDE_KEY, "false");
}
if (System.getProperty(Category.DEFAULT_CONFIGURATION_KEY) == null) {
System.setProperty(Category.DEFAULT_CONFIGURATION_KEY,
sLog4jConfFile);
}
//
---------------------------------------------------------------------------
//
// do initialization if DEFAULT_INIT_OVERRIDE_KEY == "false"
boolean bLogFileDeleted = false;
if
(System.getProperty(Category.DEFAULT_INIT_OVERRIDE_KEY).equalsIgnoreCase("f
alse")) {
// delete old logfile
try {
File file = new File(sLog4jLogFile);
if (file.exists()) {
// System.out.println( sLog4jLogFile +"-file.exists() = true");
bLogFileDeleted = file.delete();
}
} catch (Exception ex) {}
// Configure Log4j
PropertyConfigurator.configure(System.getProperty(Category.DEFAULT_CONFIGUR
ATION_KEY));
// set System.property to initialized
System.setProperty(Category.DEFAULT_INIT_OVERRIDE_KEY, "true");
LOG.debug("<METHOD name=initLog4j()> with "
+ System.getProperty(Category.DEFAULT_CONFIGURATION_KEY)
+ ", old logfile deleted = " + bLogFileDeleted +
"</METHOD>");
}
//
---------------------------------------------------------------------------
//
else // already initialized, skip initialization
{
LOG.debug("<METHOD name=initLog4j()> log4j already inititalized
</METHOD>");
}
}
static{
initLog4j();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org