Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I filter certain lines from a log4j stack trace?

I want to filter out certain lines from stack traces using log4j (like net.sf.cglib.* and org.springframework.*). Is it possible to configure this with log4j?

In eclipse the junit plugin allows you to add filters which modify the stack traces displayed in the junit tab, but this doesn't change the stack traces output to the console.

like image 555
HappyEngineer Avatar asked Jul 06 '11 21:07

HappyEngineer


People also ask

How do you analyze a stack trace?

Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.

What is filter in log4j?

Filters allow Log Events to be evaluated to determine if or how they should be published. A Filter will be called on one of its filter methods and will return a Result, which is an Enum that has one of 3 values - ACCEPT, DENY or NEUTRAL.

What does the stack trace contain?

The stack trace, also called a backtrace, consists of a collection of stack records, which store an application's movement during its execution. The stack trace includes information about program subroutines and can be used to debug or troubleshoot and is often used to create log files.

Do you read a stack trace from top to bottom?

The first line in the call stack represents the last executed function call, so remember to always read a stack trace top-down.


2 Answers

Use this filtering layout log4j add-on: http://www.openmindlab.com/lab/tools/openutilslog4j/layout.html

They have an example with log4j.xml on that page. Just use the it.openutils.log4j.FilteredPatternLayout instead of the log4j PatternLayout, and then put some entries like

    <param name="Filter" value="net.sf.cglib"/>
    <param name="Filter" value="org.springframework"/>

below.

like image 134
pdxleif Avatar answered Oct 16 '22 04:10

pdxleif


I just published an Open Source library that filters the stacktrace very nicely. Here is the link to a short article about the library: MgntUtils Open Source Java Library Also here is a github link to the sources: https://github.com/michaelgantman/Mgnt and here is Maven link to the library

like image 35
Michael Gantman Avatar answered Oct 16 '22 02:10

Michael Gantman