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 Curt Arnold <ca...@apache.org> on 2005/12/21 06:33:13 UTC

Re: How to add a new logging level that is independent of the defualt ones.

On Dec 20, 2005, at 5:45 AM, Joseph, Shinoy wrote:

>
> Hi,
>     I am in need of adding a new level logging in to the log4j.  
> Requirement is that the new level should be independent of the  
> existing levels(debug,warn,info error,fatal etc).
>
> I have added the new level by extending Level, Logger and  
> LoggingFactory classes.
> Is it possible to do that by customizing log4j.xml ? It will be  
> great if some could guide me in this regard.
>
> Thanks & Regards
> Shinoy

As you've noticed, it isn't easy to do and it makes you maintain a  
branched version of log4j.

In many cases, people add levels for reasons that are much better  
addressed by using the logger hierarchy more effectively.  For  
example, they'd like to add a level SECURITY or AUDIT when that is  
more a description of the intended audience instead of the  
significance of the message to the audience.  In that case, having a  
branch of the logger hierarchy for security related messages  
("security.myapp.foo") is better than logging a message of  
Level.SECURITY to "myapp.foo".

If you explain your requirements, maybe we can find a decent solution  
that doesn't require you maintaining your own branched copy of log4j.


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


RE: How to add a new logging level that is independent of the defualt ones.

Posted by "Joseph, Shinoy" <Sh...@orbitz.com>.
Thank you very much Arnold, actually it serves the purpose for us.
Regards
Shinoy


-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org]
Sent: Wed 12/21/2005 10:48 AM
To: Log4J Users List
Subject: Re: How to add a new  logging level that is  independent of the defualt ones.
 

On Dec 21, 2005, at 2:50 AM, Joseph, Shinoy wrote:

> Hi Arnold,
> Thank you very much for your reply.
> Let me explain our requirement. Our system communicates to the  
> external systems for storing and rerieving data. This data has got  
> its own format and all. During development and Testing or even  
> production our developers need to control the data logging out. So  
> we decided to have a seperate level of log, without affecting the  
> defualt levels of log4j. Essencially we need to have a log level  
> which is to be turned off and on, doesn't matter whether the  
> defualt levels are enabled or not.
>
> Also we don't want to create new branch out of log4j for this. If  
> we can accomplish it with adding some new tag/attributes to the  
> log4j.xml or some thing like that and use that attribute in the new  
> Logger class (thats what I am trying to implement)
>

That seems exactly where you would want to use a branch in the  
hierarchy of loggers.  It seems you have two audiences for the log  
requests, those log requests intended to diagnosticians who are  
trying to understand the behavior of the system to figure out what is  
going on so they may fix a problem and this external system.  To  
address this, split your hierarchy into two branches, for example,  
all loggers that start with "external" are directed to the external  
system and everything else is a diagnostic log message.  So in your  
classes, you'd could have something like:

class Foo {
     //    diagnostic logger
     private static Logger logger = Logger.getLogger(Foo.class);
     //    external system logger
     private static Logger externalLogger = Logger.getLogger 
("external." + Foo.class);
     ..

}

If you configuration file, you could use:

log4j.logger.external=OFF
log4j.rootLogger=DEBUG, CON

to disable logging on the external system and have everything else  
DEBUG and higher sent to the CON appender.  Or:

log4j.logger.external=INFO, EXTERNAL_SYSTEM
log4j.additivity.external=false
log4j.rootLogger=INFO,CON

to have everything INFO and higher from the external branch of the  
logger hierarchy to be directed the EXTERNAL_SYSTEM appender (and not  
fall through to the CON appender) and everything else INFO and higher  
go to the console.

Obviously, you could pick a better name than "external" and there may  
not be a need to subdivide external like I did by appending the class  
name.



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




Re: How to add a new logging level that is independent of the defualt ones.

Posted by Curt Arnold <ca...@apache.org>.
On Dec 21, 2005, at 2:50 AM, Joseph, Shinoy wrote:

> Hi Arnold,
> Thank you very much for your reply.
> Let me explain our requirement. Our system communicates to the  
> external systems for storing and rerieving data. This data has got  
> its own format and all. During development and Testing or even  
> production our developers need to control the data logging out. So  
> we decided to have a seperate level of log, without affecting the  
> defualt levels of log4j. Essencially we need to have a log level  
> which is to be turned off and on, doesn't matter whether the  
> defualt levels are enabled or not.
>
> Also we don't want to create new branch out of log4j for this. If  
> we can accomplish it with adding some new tag/attributes to the  
> log4j.xml or some thing like that and use that attribute in the new  
> Logger class (thats what I am trying to implement)
>

That seems exactly where you would want to use a branch in the  
hierarchy of loggers.  It seems you have two audiences for the log  
requests, those log requests intended to diagnosticians who are  
trying to understand the behavior of the system to figure out what is  
going on so they may fix a problem and this external system.  To  
address this, split your hierarchy into two branches, for example,  
all loggers that start with "external" are directed to the external  
system and everything else is a diagnostic log message.  So in your  
classes, you'd could have something like:

class Foo {
     //    diagnostic logger
     private static Logger logger = Logger.getLogger(Foo.class);
     //    external system logger
     private static Logger externalLogger = Logger.getLogger 
("external." + Foo.class);
     ..

}

If you configuration file, you could use:

log4j.logger.external=OFF
log4j.rootLogger=DEBUG, CON

to disable logging on the external system and have everything else  
DEBUG and higher sent to the CON appender.  Or:

log4j.logger.external=INFO, EXTERNAL_SYSTEM
log4j.additivity.external=false
log4j.rootLogger=INFO,CON

to have everything INFO and higher from the external branch of the  
logger hierarchy to be directed the EXTERNAL_SYSTEM appender (and not  
fall through to the CON appender) and everything else INFO and higher  
go to the console.

Obviously, you could pick a better name than "external" and there may  
not be a need to subdivide external like I did by appending the class  
name.



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


RE: How to add a new logging level that is independent of the defualt ones.

Posted by "Joseph, Shinoy" <Sh...@orbitz.com>.
Hi Arnold,
Thank you very much for your reply.
Let me explain our requirement. Our system communicates to the external systems for storing and rerieving data. This data has got its own format and all. During development and Testing or even production our developers need to control the data logging out. So we decided to have a seperate level of log, without affecting the defualt levels of log4j. Essencially we need to have a log level which is to be turned off and on, doesn't matter whether the defualt levels are enabled or not.

Also we don't want to create new branch out of log4j for this. If we can accomplish it with adding some new tag/attributes to the log4j.xml or some thing like that and use that attribute in the new Logger class (thats what I am trying to implement)



-----Original Message-----
From: Curt Arnold [mailto:carnold@apache.org]
Sent: Tue 12/20/2005 11:33 PM
To: Log4J Users List
Subject: Re: How to add a new  logging level that is  independent of the defualt ones.
 

On Dec 20, 2005, at 5:45 AM, Joseph, Shinoy wrote:

>
> Hi,
>     I am in need of adding a new level logging in to the log4j.  
> Requirement is that the new level should be independent of the  
> existing levels(debug,warn,info error,fatal etc).
>
> I have added the new level by extending Level, Logger and  
> LoggingFactory classes.
> Is it possible to do that by customizing log4j.xml ? It will be  
> great if some could guide me in this regard.
>
> Thanks & Regards
> Shinoy

As you've noticed, it isn't easy to do and it makes you maintain a  
branched version of log4j.

In many cases, people add levels for reasons that are much better  
addressed by using the logger hierarchy more effectively.  For  
example, they'd like to add a level SECURITY or AUDIT when that is  
more a description of the intended audience instead of the  
significance of the message to the audience.  In that case, having a  
branch of the logger hierarchy for security related messages  
("security.myapp.foo") is better than logging a message of  
Level.SECURITY to "myapp.foo".

If you explain your requirements, maybe we can find a decent solution  
that doesn't require you maintaining your own branched copy of log4j.


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




Re: How to add a new logging level that is independent of the defualt ones.

Posted by lo...@xemaps.com.
Perhaps this is a situation that would warrant creating a new logging  
level:

There are some messages that we want to always appear in our logs,  
but we don't want to identify them as FATAL errors.
These messages indicate the beginning and ending of certain important  
processes.

To facilitate this requirement, we:
1.  Created a Log4JHelper Class that does things like setting up our  
ConfigureAndWatch

2.  Created a MyLevel class in the Helper class as:

     private static class MyLevel extends Level {
         private MyLevel(int level, String name, int sysLogLevel) {
             super(level, name, sysLogLevel);
         }
     }

3.  Created a STATUS Level in the Helper Class as:
     /**
      * Provides a Log4J {@link org.apache.log4j.Level Level} that  
always logs unless logging is completely turned off.
      */
     public static final Level STATUS_LEVEL = new MyLevel 
(Priority.FATAL_INT + 10, "STATUS", SyslogAppender.LOG_LOCAL0);


Now, when we want to log something at the STATUS level, we use the  
following Logger instantiation, and log call:
     private static final Logger LOG = Logger.getLogger 
(SomeClass.class);
         .
         .
         .
     LOG.log(Log4JHelper.STATUS_LEVEL,"Some Message");



On Dec 21, 2005, at 12:33 AM, Curt Arnold wrote:

> On Dec 20, 2005, at 5:45 AM, Joseph, Shinoy wrote:
>
>>
>> Hi,
>>     I am in need of adding a new level logging in to the log4j.  
>> Requirement is that the new level should be independent of the  
>> existing levels(debug,warn,info error,fatal etc).
>>
>> I have added the new level by extending Level, Logger and  
>> LoggingFactory classes.
>> Is it possible to do that by customizing log4j.xml ? It will be  
>> great if some could guide me in this regard.
>>
>> Thanks & Regards
>> Shinoy
>
> As you've noticed, it isn't easy to do and it makes you maintain a  
> branched version of log4j.
>
> In many cases, people add levels for reasons that are much better  
> addressed by using the logger hierarchy more effectively.  For  
> example, they'd like to add a level SECURITY or AUDIT when that is  
> more a description of the intended audience instead of the  
> significance of the message to the audience.  In that case, having  
> a branch of the logger hierarchy for security related messages  
> ("security.myapp.foo") is better than logging a message of  
> Level.SECURITY to "myapp.foo".
>
> If you explain your requirements, maybe we can find a decent  
> solution that doesn't require you maintaining your own branched  
> copy of log4j.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org