Discussion:
Logging custom level using log4j in java
Praveen Kumar Hasthalapuram
2005-12-29 17:06:50 UTC
Permalink
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
Philip Denno
2005-12-29 17:22:18 UTC
Permalink
I think you misunderstand the idea of level. Levels in log4j are things
like "DEBUG", "INFO", "WARN", "ERROR", "FATAL". So if your level is set
to INFO and in your call you execute logger.debug(message) - then the
message will be thrown away, because DEBUG < INFO. On the other hand if
you execute logger.info(message) - this message will be written to the
file, because INFO <= INFO.

Several solutions come to mind:

1) Create two loggers and make sure every call that contains
"SNMP" go to logger 1 and everything else to logger 2.

2) Write everything to one file and use some log filtering tool
to view.

Finally, there may be a way to use Filters to re-direct, as I do not
have much experience with these, however I think this is just a finer -
grained mechanism to control what goes into the log file.

Cheers,
Philip.


-----Original Message-----
From: Praveen Kumar Hasthalapuram [mailto:***@gmail.com]
Sent: December 29, 2005 9:07 AM
To: log4j-***@logging.apache.org
Subject: Logging custom level using log4j in java


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
Praveen Kumar Hasthalapuram
2005-12-29 17:33:22 UTC
Permalink
Thanks Philip,

I will go with first solution, because with second soultion may lead
performance overhead and it is time consuming process.

Regards,
Praveen
Post by Philip Denno
I think you misunderstand the idea of level. Levels in log4j are things
like "DEBUG", "INFO", "WARN", "ERROR", "FATAL". So if your level is set
to INFO and in your call you execute logger.debug(message) - then the
message will be thrown away, because DEBUG < INFO. On the other hand if
you execute logger.info(message) - this message will be written to the
file, because INFO <= INFO.
1) Create two loggers and make sure every call that contains
"SNMP" go to logger 1 and everything else to logger 2.
2) Write everything to one file and use some log filtering tool
to view.
Finally, there may be a way to use Filters to re-direct, as I do not
have much experience with these, however I think this is just a finer -
grained mechanism to control what goes into the log file.
Cheers,
Philip.
-----Original Message-----
Sent: December 29, 2005 9:07 AM
Subject: Logging custom level using log4j in java
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
---------------------------------------------------------------------
chiran
2007-12-06 23:19:45 UTC
Permalink
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 Hasthalapuram
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.
Curt Arnold
2007-12-07 00:15:35 UTC
Permalink
Post by chiran
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.
Post by chiran
Post by Praveen Kumar Hasthalapuram
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" .
orko
2007-12-07 00:27:01 UTC
Permalink
A good example will be found here:
http://jaikiran.wordpress.com/2006/07/12/create-your-own-logging-level-in-log4j/
Post by chiran
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.
Post by chiran
Post by Praveen Kumar Hasthalapuram
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-***@logging.apache.org
For additional commands, e-mail: log4j-user-***@logging.apache.org
Loading...