tags:

views:

10

answers:

2

The applications I work on do a hell of a lot of things and I find it quite difficult to parse through the logs when I'm going over them. Especially when I'm debugging/testing a particular bit of code. and have to wade through pages and pages of irrelevant log data.

Is there a sensible way to manage logging (Primarilly in Java, But also interested in .Net) which would allow me to commit only a sub set of things while I'm working on a particular piece of functionality but allow me to log everything at other times.

I've considdered Labling the log statements I'm interested in and grepping for them, or wrapping the logger library (log4j/log4net) in a class which allows me to specify a context that I'm interested in, and only commit that.

Is there a standard way to do this?

For Example at the moment I'm not interested in the output of several scheduled tasks that run once a second, and I'm not interested in the output of the two layers that surround my middle tier classes. but I am interested in it's output.

A: 
Dima
A: 

The key to controlling verbosity in different parts of your application when using log4j/log4net is to name your loggers appropriately. Normally the convention is to name them the same as the fully qualified class name they relate to, but you can use any arbitrary names you want. The logger name is just a hierarchical dotted namespace that you, as the developer, can define as you see fit.

Once the areas in your application have been defined (each node in the namespace hierarchy is an "area") then you can configure the verbosity for each area, and get very detailed DEBUG-level messages for an area you're focusing on while only letting through say ERROR-level messages from other areas.

You haven't said much about the context you are interested in, but in both log4j and log4net you can define properties which are added to LoggingEvent instances. You can use existing Filter classes or create bespoke Filter classes which let the events through or not based on their properties. You then configure your Filters in the log4j/log4net configuration, and then you should be able to be very specific about what logging information gets output from a specific area in your application.

Note: to use Filters with log4j you have to use XML rather than a .properties file for configuration.

Vinay Sajip