Logger + Factory ?
Hi all,
I'm working on an application that just turned big enough to need a real logging solution instead of pointless trace() expressions, that's why I'll go with Flex4 's Log API which is pretty cool. As I'm not a big fan of using the static Log.getLogger('...') syntax but a big fan of dependency injection do you think it would be possible to have some kind of "custom injection rule" ?
I'd love to be able to do something like this :
package foo
{
public class bar
{
[Inject]
public var logger : ILogger; <- inject the result of Log.getLogger('foo.bar')
}
}
Is this something doable in RL ?
Thanks,
Comments are currently closed for this discussion. You can start a new one.
2 Posted by Mike Cann on 29 Sep, 2011 11:12 AM
I dont use the Flex logger, I prefer to roll my own then just map my Ilogger interface as a singleton, then every time I want to log something I write:
logger.debug(this, "This is a debug line");
However if you want to use Flex's system I can think of a few ways you could go about this..
One way would be to make a base class some some sort, perhaps "MyCustomActor" and have a [PostConstruct] method in there that provides you with the logger such as:
Another way may be to write your own injector (or extend SwiftSuspenders) so that whenever a an injection point of ILogger is found it returns the Log.getLogger() value rather than the default mapping.
Those would be my guesses unless anyone else can thing of a cleverer solution?
Mike
Support Staff 3 Posted by Stray on 29 Sep, 2011 11:25 AM
I was going to say something similar, except that I suggest you implement ILogger yourself into a delegate that can wrap the static reference rather than have to have it in your classes, which is where the inflexibility comes in. And I suggest that any 'work' should be in the wrapper implementation, not the class that uses it. (Even in a base class - sorry Mike ;P )
This approach relies on setter injection:
Then you need an implementation of ILoggerWrapper that simply implements ILogger and also does this:
You'll want each instance to receive a unique instance of ILogger, so map it like this (assuming your class that does the logging activity delegating is called FlexLogWrapper):
hth,
Stray
4 Posted by Palleas on 29 Sep, 2011 12:41 PM
I was about to use a custom injector but it seemed a too heavy solution for such a simple aim.
Thanks, I use Stray's solution and it works great :)
Cheers !
5 Posted by krasimir on 29 Sep, 2011 12:54 PM
Just a quick note about the subject of debugging:
in one of my latest projects I used http://demonsterdebugger.com/ it is actually really powerful and provides nice features.
6 Posted by Mike Cann on 29 Sep, 2011 01:00 PM
Ye demonster is okay on smaller projects, try using it on anything of size and it soon chuggs however. Writing your own using sockets over localhost aint hard tho ;)
Support Staff 7 Posted by Till Schneidereit on 29 Sep, 2011 01:24 PM
Just a quick update to give a narrow answer to the original question:
The way the Swiftsuspenders injector works, it's unfortunately not
possible to customize the injected value based on the injection
target. Right now, that's also not possible in Swiftsuspenders 2, but
seeing as I think that this is a very valid use-case, I will see if
it's possible to change the injection process to enable it.
8 Posted by Mike Cann on 29 Sep, 2011 01:36 PM
Oh nice! That would be a pretty cool feature Till :)
Support Staff 9 Posted by Till Schneidereit on 29 Sep, 2011 01:52 PM
I'll see what I can do :)
Support Staff 10 Posted by Till Schneidereit on 29 Sep, 2011 04:10 PM
Good news: I just prototyped (or, in fact implemented without much
testing) the required changes and it looks like the overhead is
negligible. That means that in Swiftsuspenders 2, all dependency
providers will have access to the target object's class. They can't
have access to the actual instance though, because in case of
constructor injection, the instance won't be created at the time of
dependency collection.
On Thu, Sep 29, 2011 at 15:51, Till Schneidereit
<***@gmail.com> wrote:
> I'll see what I can do :)
>
> On Thu, Sep 29, 2011 at 15:36, Mike Cann
> <***@tenderapp.com>
> wrote:
>>
11 Posted by Stray on 29 Sep, 2011 04:17 PM
Nice work sir! That's very good news - an extra flexible feature based on a real use case :)
12 Posted by Mike Cann on 29 Sep, 2011 04:25 PM
Agreed! Nice work Till :)
13 Posted by Palleas on 30 Sep, 2011 12:58 PM
I think I'm in love with Till.
Thank you :)
14 Posted by Stray on 30 Sep, 2011 01:25 PM
It's impossible not to be at least a little bit in love with Till.
xx
Support Staff 15 Posted by Till Schneidereit on 30 Sep, 2011 02:16 PM
Now let's not get ahead of ourselves: It's logging we're talking about, here ;)
In other news, I've just pushed the required changes to the branch that will become Swiftsuspenders 2:
https://github.com/tschneidereit/SwiftSuspenders/tree/the-future
Once that's released, writing a provider encapsulating Log.getLogger will boil down to this:
Using it will look like this:
(Don't you just love the brevity of AS3? A LOC/ lines-that-actually-do-something ratio of 10:1, yay!)
16 Posted by Palleas on 30 Sep, 2011 02:30 PM
The syntax is really cool.
Support Staff 17 Posted by Ondina D.F. on 01 Nov, 2011 02:13 PM
If you need more help with this, please feel free to re-open this discussion. Please open new threads for new issues.
Thanks!
Ondina
Ondina D.F. closed this discussion on 01 Nov, 2011 02:13 PM.