[slf4j-user] logging progress through loggers...

luvar at plaintext.sk luvar at plaintext.sk
Tue Sep 17 20:15:26 CEST 2013


Hello,
I have another one question. Why is Log4jLoggerAdapter from slf4j-log4j12 (version 1.7.5) final? I want to have access to its private field (logger instance from log4j). I want to be able to add "my own appender" to one of logger (with all underlying hierarchy of appenders). Is it possible somehow, or I have to make my own backing appender and use it instead of slf4j-log4j12? I just want to hook to log messages flow in one logger (with hierarchy).

Now I have done it this way:

In constructor of my GUI class where I need to catch all log messages I have done this code:

		if(log instanceof Log4jLoggerAdapter) {
			Logger l = Logger.getLogger("sk.plaintext.gui.package.example");
			l.addAppender(new ProgressBarAppender(this.progressBar));
		}

So if my project uses slf4j-log4j12, I will make my own log4j logger (variable l) and add my custom appender to it. It will receive all messages from slf4j loggers, which are directed through log4j. This workaround is working well, while I am using log4j as backend. Is it possible to make something like this in universal way? Only dependant on slf4j-api?

PS: Just for clarification. Class ProgressBarAppender is listening for ProgressMadeMessage messages (serialized ones because of slf4j limitation described in first post) and set progress to given JProgressBar instance. Code which is doing any computation can be anywhere in given package hierarchy ("sk.plaintext.gui.package.example") and it is free of any gui related imports or any weird interfaces for progress reporting.

Thanks,
--
LuVar


----- "LuVar" <luvar at plaintext.sk> wrote:

> Hello,
> I am using log4j now on one of my older app. I have there a small
> library which allows me to log progress of anything happening in my
> applications. I just do something like this:
> 
> this.log.info(ProgressMadeMessage.newFirstMsg("import user
> dictionary));
> //do some work
> this.log.info(ProgressMadeMessage.newFirstMsg("import user
> dictionary), 8);
> //do some work
> this.log.info(ProgressMadeMessage.newFirstMsg("import user
> dictionary), 19);
> //do some work
> this.log.info(ProgressMadeMessage.newFirstMsg("import user
> dictionary), 55);
> ....
> 
> Than I have my own appender defined and I visualize progress made by
> asynchronous task. My appender is able also to predict (few old run
> times are saved) trend of progress.
> 
> I have now migrated to slf4j. It allows only to log strings. This
> makes logging of my ProgressMadeMessage instances impossible. Is there
> some performance non problematic solution to my needs? (serializing
> ProgressMadeMessage instances to String and than backwards is
> possible, but performance will be hurt a lot)
> 
> Thanks for advice,
> --
> LuVar
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user


More information about the slf4j-user mailing list