Return-Path: Delivered-To: apmail-logging-log4j-user-archive@www.apache.org Received: (qmail 16178 invoked from network); 21 Mar 2008 16:51:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Mar 2008 16:51:19 -0000 Received: (qmail 90408 invoked by uid 500); 21 Mar 2008 16:51:16 -0000 Delivered-To: apmail-logging-log4j-user-archive@logging.apache.org Received: (qmail 90376 invoked by uid 500); 21 Mar 2008 16:51:16 -0000 Mailing-List: contact log4j-user-help@logging.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Log4J Users List" Reply-To: "Log4J Users List" Delivered-To: mailing list log4j-user@logging.apache.org Received: (qmail 90365 invoked by uid 99); 21 Mar 2008 16:51:16 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Mar 2008 09:51:16 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of stauffer.james@gmail.com designates 64.233.170.189 as permitted sender) Received: from [64.233.170.189] (HELO rn-out-0910.google.com) (64.233.170.189) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Mar 2008 16:50:34 +0000 Received: by rn-out-0910.google.com with SMTP id s42so1335208rnb.18 for ; Fri, 21 Mar 2008 09:50:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=/lwcNAvbFVE4DH8015OPhyoFY0gin3M4vrlbnEJPcKo=; b=YejBuuewBE+tHs6clSw7OxrzcGY9YDjN0f5cmnJ8ymvgvyTLGT6iPbOC3RtqsKZ9JyPGhvzmSdSYJTLYLNL879rz0ik0M2IvhuzJvAKl4O3//8/D9wtACzWHrnHUZCSwwqYiQ2J/Ceo0CA5AWPMgAljsQgpX67UWWQuZx8YblbU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=pi8sgJRG44xnZAkVYsTc617x8eClgibrFxieB1U7R3M4LMEfZpoNI71dHThc/dRk7/ejyR4ZbEFhC5C6uynWUXlOhiS/5fA2MDSGGW6HBww4Qm+h6MyAws0UM+tRJHGYcrGVUv7JRINgISHo59Hng7iPAIgXKhRPBzVIF7ddCIA= Received: by 10.150.154.6 with SMTP id b6mr1689129ybe.64.1206118245207; Fri, 21 Mar 2008 09:50:45 -0700 (PDT) Received: by 10.150.92.16 with HTTP; Fri, 21 Mar 2008 09:50:45 -0700 (PDT) Message-ID: Date: Fri, 21 Mar 2008 10:50:45 -0600 From: "James A. N. Stauffer" To: "Log4J Users List" , pico@mindspring.com Subject: Re: conditional logging of exception In-Reply-To: <21694223.1206040011060.JavaMail.root@mswamui-bichon.atl.sa.earthlink.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <21694223.1206040011060.JavaMail.root@mswamui-bichon.atl.sa.earthlink.net> X-Virus-Checked: Checked by ClamAV on apache.org Two options that come to mind: 1. Use 2 appenders with only one logging the exception. Then when you would normally change the level you would change the appender. 2. If it is code that you control you could log the exception at a lower level (i.e. info) so you don't see them when the threshhold is at error. On Thu, Mar 20, 2008 at 1:06 PM, wrote: > Hello, > > For a webapp, it would be nice to be able to conditionally log stack trace along > with exception. > > Scenario: > > (1) Production application ships with Level set to Level.ERROR. > (2) An app admin notices some errors in the log [Logger.error(Object)]. > (3) A means is available [Logger.setLevel()] to dynamically set Level to Level.DEBUG > for a particular class. > (4) When the error occurs again, the stack trace is logged along with the error > [Logger.error(Object, Throwable)]. > > A solution would be to have a wrapper class with a static method like: > > public static void logVerboseOnLevel(Logger logger, Level thresholdLevel, Exception > e, Level logAtLevel) { > if (logger.isEnabledFor(thresholdLevel)) { > logger.log(logAtLevel, e, e); > } else { > logger.log(logAtLevel, e); > } > } > > and called like: > > Logger logger = Logger.getLogger(getClass); > Wrapper.logVerboseOnLevel(logger, Level.DEBUG, new Exception(), Level.ERROR); > > If logger level is ERROR, just the one liner is logged. If logger level is DEBUG, > the stack trace is logged. > > The benefit is to minimize the amount of information in the log file, unless the > cause of the exception is being sought. Since it is possible to set log level per > class, the signal to noise ratio is much more conducive to finding the problem. > > In log4j.properties, the appender is defined like: > > log4j.appender.MY_APPENDER.layout.ConversionPattern=%d [%t] %-5p %c:%l %C:%-4L > - %m%n > > The portion "%C:%-4L" means I want the class and line number where the > problem occurred. This conflicts with the wrapper, as its class and line number > are the ones used to invoke log4j. Also, it would be nice to only use the wrapper > class in specialized situations. So any solution would need to handle both the > straightforward Logger use case, as well as the wrapper. > > There is an old post on this forum describing a similar problem, but I couldn't > really figure out how to utilize the responses. > > http://mail-archives.apache.org/mod_mbox/logging-log4j-user/200710.mbox/%3cd54c36420710251039nae475e1p3e59fc7f00bd141f@mail.gmail.com%3e > > Originally I had the chunk of code above copied in each specialized place where > I needed it, then I refactored the copies into a utility class, and ran into the > logging output issue. > > Any suggestions for a solution to this usage case? > > Thanks... > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org > For additional commands, e-mail: log4j-user-help@logging.apache.org > > -- James A. N. Stauffer http://www.geocities.com/stauffer_james/ Are you good? Take the test at http://www.livingwaters.com/good/ --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org For additional commands, e-mail: log4j-user-help@logging.apache.org