You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Dennis Lundberg <de...@apache.org> on 2006/06/24 18:25:49 UTC

How do I enable logging in a non-Mojo plugin class?

Hi

I want to be able to use the log instance returned from Mojo.getLog() in 
a class that is used by Mojo. The class itself is not a Mojo, but it is 
part of a plugin. Is there a best practice for this?

A couple of alternatives that came to mind:

1. If the class has a constructor pass the log instance to the 
constructor and store a reference in the class

2. If the class has a constructor pass the Mojo instance to the 
constructor and store a reference in the class

3. Pass the log instance to the methods that needs logging

4. Pass the Mojo instance to the methods that needs logging

None of these look nice design-wise. What I'm looking for is something 
like what is available in commons-logging:
   Log log = LogFactory.getLog(<context>);

Is there something like that available in Maven?

-- 
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Jason van Zyl <ja...@maven.org>.
On 25 Jun 06, at 12:09 AM 25 Jun 06, Dennis Lundberg wrote:

> Trygve Laugstøl wrote:
>> Dennis Lundberg wrote:
>>> Jason van Zyl wrote:
>>>>
>>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>>
>>>>> Hi
>>>>>
>>>>> I want to be able to use the log instance returned from  
>>>>> Mojo.getLog() in a class that is used by Mojo. The class itself  
>>>>> is not a Mojo, but it is part of a plugin. Is there a best  
>>>>> practice for this?
>>>>>
>>>>
>>>> What is the class?
>>>
>>> It's a part of the maven1 plugin in the sandbox that I'm working  
>>> on. To separate matters into reasonable chunks of code there is  
>>> currently:
>>>  1  interface
>>>  1  abstract implementation
>>> 10+ implementations
>> Sounds to me like you should make this a set of proper Plexus  
>> compomnents where you would get the logging  and probably a lot of  
>> the instatiation/discoverry for free.
>> Take a look at for example the compiler plugin on how to write a  
>> Plexus component and how to use that in a Mojo.
>
> Thanks Trygve. It looks like it would fit nicely with what I'm  
> trying to do.
>
> The compiler plugin doesn't have much in that respect though, at  
> least what I could find. It does use plexus-compiler which contains  
> plexus components for different compilers and then it uses  
> CompilerManager to retrieve compilers.
>
> What I'm not getting, and not finding at the plexus site either, is  
> how to connect the mojo and the plexus component. I'm missing the  
> lookup plumbing. Do I have to write my own manager for that, and if  
> so how do I make my components known to the manager?

If you make a proper plexus component then all you need is something  
like the following:

/**
   * @component
   */
private MyComponent component;

And Maven/Plexus will lookup the component and stuff it into your  
mojo. Standard Plexus dependency injection.

Jason.

>
> -- 
> Dennis Lundberg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

Jason van Zyl
jason@maven.org




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Dennis Lundberg <de...@apache.org>.
Trygve Laugstøl wrote:
> Dennis Lundberg wrote:
>> Trygve Laugstøl wrote:
>>> Dennis Lundberg wrote:
>>>> Trygve Laugstøl wrote:
>>>>> Dennis Lundberg wrote:
>>>>>> Jason van Zyl wrote:
>>>>>>>
>>>>>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>>>>>
>>>>>>>> Hi
>>>>>>>>
>>>>>>>> I want to be able to use the log instance returned from 
>>>>>>>> Mojo.getLog() in a class that is used by Mojo. The class itself 
>>>>>>>> is not a Mojo, but it is part of a plugin. Is there a best 
>>>>>>>> practice for this?
>>>>>>>>
>>>>>>>
>>>>>>> What is the class?
>>>>>>
>>>>>> It's a part of the maven1 plugin in the sandbox that I'm working 
>>>>>> on. To separate matters into reasonable chunks of code there is 
>>>>>> currently:
>>>>>>  1  interface
>>>>>>  1  abstract implementation
>>>>>> 10+ implementations
>>>>>
>>>>> Sounds to me like you should make this a set of proper Plexus 
>>>>> compomnents where you would get the logging  and probably a lot of 
>>>>> the instatiation/discoverry for free.
>>>>>
>>>>> Take a look at for example the compiler plugin on how to write a 
>>>>> Plexus component and how to use that in a Mojo.
>>>>
>>>> Thanks Trygve. It looks like it would fit nicely with what I'm 
>>>> trying to do.
>>>>
>>>> The compiler plugin doesn't have much in that respect though, at 
>>>> least what I could find. It does use plexus-compiler which contains 
>>>> plexus components for different compilers and then it uses 
>>>> CompilerManager to retrieve compilers.
>>>>
>>>> What I'm not getting, and not finding at the plexus site either, is 
>>>> how to connect the mojo and the plexus component. I'm missing the 
>>>> lookup plumbing. Do I have to write my own manager for that, and if 
>>>> so how do I make my components known to the manager?
>>>
>>> In addition to what Jason said, yes you will need a manager that will 
>>> have the map of components like the CompilerManager.
>>
>> Thanks,
>>
>> I think that I've got the hang of things now. I looked at the plexus 
>> site and used plexus-compiler as a model for the work I'm doing.
>>
>> There is one thing that I still can't quite understand though. In 
>> plexus-compiler there are no components.xml files. It seems to be 
>> using annotations instead. This is easier to read, but I can't get it 
>> to work. Do I need to add or upgrade some dependency to get this to work?
> 
> The components.xml files are generated with the plexus:descriptor mojo. 

Ah, that explains it. I'll have a look at that as well.

> I've just done some work on the compilers to be all annotations-based, 
> but I just might have broken something there. At least make sure you 
> have the latest plexus-maven-plugin and I will check up on it after the 
> tutorial at ApacheCon tomorrow.

The thing that I couldn't get to work was the stuff *I'm* working on, 
not plexus-compiler. I haven't actually tried building plexus-compiler 
yet, but I think I'll have go at that tomorrow.

Have a nice time in Dublin! I'll have some patches for the plexus site 
ready when you get back. :)

-- 
Dennis Lundberg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Trygve Laugstøl <tr...@apache.org>.
Dennis Lundberg wrote:
> Trygve Laugstøl wrote:
>> Dennis Lundberg wrote:
>>> Trygve Laugstøl wrote:
>>>> Dennis Lundberg wrote:
>>>>> Jason van Zyl wrote:
>>>>>>
>>>>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>>>>
>>>>>>> Hi
>>>>>>>
>>>>>>> I want to be able to use the log instance returned from 
>>>>>>> Mojo.getLog() in a class that is used by Mojo. The class itself 
>>>>>>> is not a Mojo, but it is part of a plugin. Is there a best 
>>>>>>> practice for this?
>>>>>>>
>>>>>>
>>>>>> What is the class?
>>>>>
>>>>> It's a part of the maven1 plugin in the sandbox that I'm working 
>>>>> on. To separate matters into reasonable chunks of code there is 
>>>>> currently:
>>>>>  1  interface
>>>>>  1  abstract implementation
>>>>> 10+ implementations
>>>>
>>>> Sounds to me like you should make this a set of proper Plexus 
>>>> compomnents where you would get the logging  and probably a lot of 
>>>> the instatiation/discoverry for free.
>>>>
>>>> Take a look at for example the compiler plugin on how to write a 
>>>> Plexus component and how to use that in a Mojo.
>>>
>>> Thanks Trygve. It looks like it would fit nicely with what I'm trying 
>>> to do.
>>>
>>> The compiler plugin doesn't have much in that respect though, at 
>>> least what I could find. It does use plexus-compiler which contains 
>>> plexus components for different compilers and then it uses 
>>> CompilerManager to retrieve compilers.
>>>
>>> What I'm not getting, and not finding at the plexus site either, is 
>>> how to connect the mojo and the plexus component. I'm missing the 
>>> lookup plumbing. Do I have to write my own manager for that, and if 
>>> so how do I make my components known to the manager?
>>
>> In addition to what Jason said, yes you will need a manager that will 
>> have the map of components like the CompilerManager.
> 
> Thanks,
> 
> I think that I've got the hang of things now. I looked at the plexus 
> site and used plexus-compiler as a model for the work I'm doing.
> 
> There is one thing that I still can't quite understand though. In 
> plexus-compiler there are no components.xml files. It seems to be using 
> annotations instead. This is easier to read, but I can't get it to work. 
> Do I need to add or upgrade some dependency to get this to work?

The components.xml files are generated with the plexus:descriptor mojo. 
I've just done some work on the compilers to be all annotations-based, 
but I just might have broken something there. At least make sure you 
have the latest plexus-maven-plugin and I will check up on it after the 
tutorial at ApacheCon tomorrow.

--
Trygve

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Dennis Lundberg <de...@apache.org>.
Trygve Laugstøl wrote:
> Dennis Lundberg wrote:
>> Trygve Laugstøl wrote:
>>> Dennis Lundberg wrote:
>>>> Jason van Zyl wrote:
>>>>>
>>>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>>>
>>>>>> Hi
>>>>>>
>>>>>> I want to be able to use the log instance returned from 
>>>>>> Mojo.getLog() in a class that is used by Mojo. The class itself is 
>>>>>> not a Mojo, but it is part of a plugin. Is there a best practice 
>>>>>> for this?
>>>>>>
>>>>>
>>>>> What is the class?
>>>>
>>>> It's a part of the maven1 plugin in the sandbox that I'm working on. 
>>>> To separate matters into reasonable chunks of code there is currently:
>>>>  1  interface
>>>>  1  abstract implementation
>>>> 10+ implementations
>>>
>>> Sounds to me like you should make this a set of proper Plexus 
>>> compomnents where you would get the logging  and probably a lot of 
>>> the instatiation/discoverry for free.
>>>
>>> Take a look at for example the compiler plugin on how to write a 
>>> Plexus component and how to use that in a Mojo.
>>
>> Thanks Trygve. It looks like it would fit nicely with what I'm trying 
>> to do.
>>
>> The compiler plugin doesn't have much in that respect though, at least 
>> what I could find. It does use plexus-compiler which contains plexus 
>> components for different compilers and then it uses CompilerManager to 
>> retrieve compilers.
>>
>> What I'm not getting, and not finding at the plexus site either, is 
>> how to connect the mojo and the plexus component. I'm missing the 
>> lookup plumbing. Do I have to write my own manager for that, and if so 
>> how do I make my components known to the manager?
> 
> In addition to what Jason said, yes you will need a manager that will 
> have the map of components like the CompilerManager.

Thanks,

I think that I've got the hang of things now. I looked at the plexus 
site and used plexus-compiler as a model for the work I'm doing.

There is one thing that I still can't quite understand though. In 
plexus-compiler there are no components.xml files. It seems to be using 
annotations instead. This is easier to read, but I can't get it to work. 
Do I need to add or upgrade some dependency to get this to work?

-- 
Dennis Lundberg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Trygve Laugstøl <tr...@apache.org>.
Dennis Lundberg wrote:
> Trygve Laugstøl wrote:
>> Dennis Lundberg wrote:
>>> Jason van Zyl wrote:
>>>>
>>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>>
>>>>> Hi
>>>>>
>>>>> I want to be able to use the log instance returned from 
>>>>> Mojo.getLog() in a class that is used by Mojo. The class itself is 
>>>>> not a Mojo, but it is part of a plugin. Is there a best practice 
>>>>> for this?
>>>>>
>>>>
>>>> What is the class?
>>>
>>> It's a part of the maven1 plugin in the sandbox that I'm working on. 
>>> To separate matters into reasonable chunks of code there is currently:
>>>  1  interface
>>>  1  abstract implementation
>>> 10+ implementations
>>
>> Sounds to me like you should make this a set of proper Plexus 
>> compomnents where you would get the logging  and probably a lot of the 
>> instatiation/discoverry for free.
>>
>> Take a look at for example the compiler plugin on how to write a 
>> Plexus component and how to use that in a Mojo.
> 
> Thanks Trygve. It looks like it would fit nicely with what I'm trying to 
> do.
> 
> The compiler plugin doesn't have much in that respect though, at least 
> what I could find. It does use plexus-compiler which contains plexus 
> components for different compilers and then it uses CompilerManager to 
> retrieve compilers.
> 
> What I'm not getting, and not finding at the plexus site either, is how 
> to connect the mojo and the plexus component. I'm missing the lookup 
> plumbing. Do I have to write my own manager for that, and if so how do I 
> make my components known to the manager?

In addition to what Jason said, yes you will need a manager that will 
have the map of components like the CompilerManager.

--
Trygve

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Dennis Lundberg <de...@apache.org>.
Trygve Laugstøl wrote:
> Dennis Lundberg wrote:
>> Jason van Zyl wrote:
>>>
>>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>>
>>>> Hi
>>>>
>>>> I want to be able to use the log instance returned from 
>>>> Mojo.getLog() in a class that is used by Mojo. The class itself is 
>>>> not a Mojo, but it is part of a plugin. Is there a best practice for 
>>>> this?
>>>>
>>>
>>> What is the class?
>>
>> It's a part of the maven1 plugin in the sandbox that I'm working on. 
>> To separate matters into reasonable chunks of code there is currently:
>>  1  interface
>>  1  abstract implementation
>> 10+ implementations
> 
> Sounds to me like you should make this a set of proper Plexus 
> compomnents where you would get the logging  and probably a lot of the 
> instatiation/discoverry for free.
> 
> Take a look at for example the compiler plugin on how to write a Plexus 
> component and how to use that in a Mojo.

Thanks Trygve. It looks like it would fit nicely with what I'm trying to do.

The compiler plugin doesn't have much in that respect though, at least 
what I could find. It does use plexus-compiler which contains plexus 
components for different compilers and then it uses CompilerManager to 
retrieve compilers.

What I'm not getting, and not finding at the plexus site either, is how 
to connect the mojo and the plexus component. I'm missing the lookup 
plumbing. Do I have to write my own manager for that, and if so how do I 
make my components known to the manager?

-- 
Dennis Lundberg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Trygve Laugstøl <tr...@apache.org>.
Dennis Lundberg wrote:
> Jason van Zyl wrote:
>>
>> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
>>
>>> Hi
>>>
>>> I want to be able to use the log instance returned from Mojo.getLog() 
>>> in a class that is used by Mojo. The class itself is not a Mojo, but 
>>> it is part of a plugin. Is there a best practice for this?
>>>
>>
>> What is the class?
> 
> It's a part of the maven1 plugin in the sandbox that I'm working on. To 
> separate matters into reasonable chunks of code there is currently:
>  1  interface
>  1  abstract implementation
> 10+ implementations

Sounds to me like you should make this a set of proper Plexus 
compomnents where you would get the logging  and probably a lot of the 
instatiation/discoverry for free.

Take a look at for example the compiler plugin on how to write a Plexus 
component and how to use that in a Mojo.

--
Trygve

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Dennis Lundberg <de...@apache.org>.
Jason van Zyl wrote:
> 
> On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:
> 
>> Hi
>>
>> I want to be able to use the log instance returned from Mojo.getLog() 
>> in a class that is used by Mojo. The class itself is not a Mojo, but 
>> it is part of a plugin. Is there a best practice for this?
>>
> 
> What is the class?

It's a part of the maven1 plugin in the sandbox that I'm working on. To 
separate matters into reasonable chunks of code there is currently:
  1  interface
  1  abstract implementation
10+ implementations

They handle relocation (changed groupId/artifactId) of plugins in a pom.


Having not done much IoC work, let me see if I understand how this would 
work. Since this is a really small code base I will make do with just 
one type of event: informUser.

interface RelocatorMonitor
+ informUser(String)

class MavenMojoRelocatorMonitor implements RelocatorMonitor
+ MavenMojoRelocatorMonitor(Mojo)
+ informUser(String)
   which calls  mojo.getLog().info(String)

class RelocatorOne extends AbstractRelocator
+ RelocatorOne(RelocatorMonitor)
+ relocate()

class MyMojo
Relocator[] relocators = new Relocator[]
{
     new RelocatorOne( new MavenMojoRelocatorMonitor(this) ),
     new RelocatoTwo( ),       // This one doesn't need logging
     ...
}

Now in RelocatorOne.relocate() when I want to log something I would 
instead call:
   relocatorMonitor.informUser(String)


If, later on, I wanted to use these classes in a non Maven environment 
that uses Log4j I would just create a monitor implementation for that 
environment:

class Log4jRelocatorMonitor implements RelocatorMonitor
+ Log4jRelocatorMonitor(Logger)
+ informUser(String)
   which calls  logger.getLogger().info(String)


> 
> I always use a monitor or an event model for systems so that anything 
> log system can be adapted. Paul Hammant has a good blog entry about it 
> and this is what Maven does as does Plexus:
> 
> http://paulhammant.com/blog/000241.html
> 
> I stand by the assertion that static logging is dumb. A logging system 
> is just a sink, you systems should not be coupled to them. Monitors 
> should be domain specific and you can then adapt a logging system to use 
> them.
> 
> 
>> A couple of alternatives that came to mind:
>>
>> 1. If the class has a constructor pass the log instance to the 
>> constructor and store a reference in the class
>>
>> 2. If the class has a constructor pass the Mojo instance to the 
>> constructor and store a reference in the class
>>
>> 3. Pass the log instance to the methods that needs logging
>>
>> 4. Pass the Mojo instance to the methods that needs logging
>>
>> None of these look nice design-wise. What I'm looking for is something 
>> like what is available in commons-logging:
>>   Log log = LogFactory.getLog(<context>);
>>
>> Is there something like that available in Maven?
>>
>> --Dennis Lundberg
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
>> For additional commands, e-mail: dev-help@maven.apache.org
>>
>>
> 
> Jason van Zyl
> jason@maven.org
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
> 


-- 
Dennis Lundberg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: How do I enable logging in a non-Mojo plugin class?

Posted by Jason van Zyl <ja...@maven.org>.
On 24 Jun 06, at 6:25 PM 24 Jun 06, Dennis Lundberg wrote:

> Hi
>
> I want to be able to use the log instance returned from Mojo.getLog 
> () in a class that is used by Mojo. The class itself is not a Mojo,  
> but it is part of a plugin. Is there a best practice for this?
>

What is the class?

I always use a monitor or an event model for systems so that anything  
log system can be adapted. Paul Hammant has a good blog entry about  
it and this is what Maven does as does Plexus:

http://paulhammant.com/blog/000241.html

I stand by the assertion that static logging is dumb. A logging  
system is just a sink, you systems should not be coupled to them.  
Monitors should be domain specific and you can then adapt a logging  
system to use them.


> A couple of alternatives that came to mind:
>
> 1. If the class has a constructor pass the log instance to the  
> constructor and store a reference in the class
>
> 2. If the class has a constructor pass the Mojo instance to the  
> constructor and store a reference in the class
>
> 3. Pass the log instance to the methods that needs logging
>
> 4. Pass the Mojo instance to the methods that needs logging
>
> None of these look nice design-wise. What I'm looking for is  
> something like what is available in commons-logging:
>   Log log = LogFactory.getLog(<context>);
>
> Is there something like that available in Maven?
>
> -- 
> Dennis Lundberg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

Jason van Zyl
jason@maven.org




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org