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 chiran <ch...@gmail.com> on 2007/12/07 00:19:45 UTC

Re: Logging custom level using log4j in java

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



Praveen Kumar Hasthalapuram wrote:
> 
> Hi 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.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Logging custom level using log4j in java

Posted by orko <or...@yahoo.com>.
A good example will be found here:
http://jaikiran.wordpress.com/2006/07/12/create-your-own-logging-level-in-log4j/

Curt Arnold <ca...@apache.org> wrote: 
On Dec 6, 2007, at 5:19 PM, chiran wrote:

>
> 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.
>

Good example of extending levels for the right reason.

>

>
> Praveen Kumar Hasthalapuram wrote:
>>
>> Hi 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
>>
>>


Logger names are the best tool to separate log messages based on  
target audience or topics.  Levels are not intended for that purpose.   
Many people assume since most examples use class names as logger  
names, that that is the only pattern that is allowed and try to  
overload LEVEL to indicate topic or audience.  In your case, use a  
logger named "snmp.whatever" or "cdp.whatever" for messages that you  
may want to route to different locations.

This has been discussed many times previously, see http://permalink.gmane.org/gmane.comp.jakarta.log4j.user/15285 
  or search for "log4j-user custom level audience" .

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org



Re: Logging custom level using log4j in java

Posted by Curt Arnold <ca...@apache.org>.
On Dec 6, 2007, at 5:19 PM, chiran wrote:

>
> 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.
>

Good example of extending levels for the right reason.

>

>
> Praveen Kumar Hasthalapuram wrote:
>>
>> Hi 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
>>
>>


Logger names are the best tool to separate log messages based on  
target audience or topics.  Levels are not intended for that purpose.   
Many people assume since most examples use class names as logger  
names, that that is the only pattern that is allowed and try to  
overload LEVEL to indicate topic or audience.  In your case, use a  
logger named "snmp.whatever" or "cdp.whatever" for messages that you  
may want to route to different locations.

This has been discussed many times previously, see http://permalink.gmane.org/gmane.comp.jakarta.log4j.user/15285 
  or search for "log4j-user custom level audience" .

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org