Hi all,
It is very easy to create a custom log4j level.
All u need is the basic understanding of how the different levels are
organized.
here is the sample code for that.
create a custom level of your own.
package com.custom.log;
import org.apache.log4j.Level;
// Referenced classes of package org.apache.log4j:
// Priority
public class MyLevel extends Level
{
public MyLevel(int level, String levelStr, int syslogEquivalent)
{
super(level, levelStr, syslogEquivalent);
}
public static MyLevel toLevel(int val, Level defaultLevel)
{
return DISASTER;
}
public static MyLevel toLevel(String sArg, Level defaultLevel)
{
return DISASTER;
}
public static final MyLevel DISASTER = new MyLevel(60000, "DISASTER",
0);
}
end of class....
I created a custom class called Mylevel and it has a DISASTER level inside.
This is similar to the other levels like WARN,DEBUG,FATAL ..etc.
I want my custom level to be of more rank than any other.So i used a value
60000 to create it.
FATAL has got 50000.
please refer to any log4j docs to get the priority values for different
levels.
once the above class is created,all you need to do is change your
log4j.properties file accordingly.
sample log4j.properties.
log4j.rootLogger=DEBUG, R5
log4j.appender.R5=org.apache.log4j.RollingFileAppender
log4j.appender.R5.File=./nameOfLogFile
log4j.appender.R5.threshold=DISASTER#com.custom.log.MyLevel
log4j.appender.R5.layout=org.apache.log4j.PatternLayout
log4j.appender.R5.layout.ConversionPattern=%p %t %c - %m%n
Please observe the threshold value.It should be your LEVEL#CLASS format.
My class name is com.custom.log.MyLevel
and my defined level inside that class is DISASTER.
Using these two modifications ,you should be able to see your log statements
coming in the log file.
The modificaion while logging different statements is as follows.
testCustomLog=Logger.getLogger(testLog.class.getName());
testCustomLog.log(MyLevel.DISASTER, "i am new disaster");
the above 2 lines are the lines which you will use in your java files.
firstLine is to create a logger for u.
And then the second line will log your statements in the DISASTER log
file,Which is your custom log file with only your custom statements.
sample java class code
public class testLog {
public static Logger testCustomLog=null;
public static void main(String[] args) {
testCustomLog=Logger.getLogger(testLog.class.getName());
testCustomLog.info("i am working info info");
testCustomLog.fatal("i am working info");
testCustomLog.log(MyLevel.DISASTER, "i am new disaster");
}
}
the info and fatal wont be logged as the priority value we choosed is 60000
which is more than the FATAL in order.
please let me know if you have any more questions.
I think the above information should itself be enough.I never tried using
log4j.xml.
I only tried with log4j.properties.
Regards,
Chiran
Post by Praveen Kumar HasthalapuramHi All,
I need to log specified particular type of messages to one log file and rest
to another log file.
For example say, if message contains "SNMP" or "CDP" and so on, need to
log in to log1.log
and rest of the messages need to log in to log2.log
How to configure the log4j.properties to log custom levels?
Is there any provision to create custom levels and to configure it?
I heard that by Filters we can do this, It will be great if anyone throw
some light on this.
Thanks In Advacne,
Praveen
--
View this message in context: http://www.nabble.com/Logging-custom-level-using-log4j-in-java-tf822220.html#a14203484
Sent from the Log4j - Users mailing list archive at Nabble.com.