Customized log message

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Customized log message

ArchitP
Hi all,

I am writing my custom checks for J2EE . While performing the check I
want to log the error message such that it gives me the start and end of
message definition. Till here it's fine. But while logging the message I
cannot customize the log message because the check.java file has just
two log methods specified which do not suit my needs.

I tried to overload the logName method in MyCustomCheck.java class.

What I did was this

Public class MyCustomCheck extends AbstractInterfaceCheck
     protected void logName(DetailAST aAST, String aKey, Object[] aArgs)
     {
         final DetailAST nameAST =
aAST.findFirstToken(TokenTypes.IDENT);
         final DetailAST methodEndAST =
aAST.findFirstToken(TokenTypes.RPAREN);
         final String name = nameAST.getText();
         final Object[] fullArgs = new Object[aArgs.length + 1];
         System.arraycopy(aArgs, 0, fullArgs, 1, aArgs.length);
         fullArgs[0] = name;
         LocalizedMessages localMessages = new LocalizedMessages();
         localMessages.add(new LocalizedMessage(
                 nameAST.getLineNo(),
                 methodEndAST.getLineNo(),
                 getMessageBundle(),
                 aKey,
                 aArgs,
                 getSeverityLevel(),
                 this.getClass()));
         setMessages(localMessages);
     }
}

This method gets executed but it does not log error. I think I didn't
get the logging mechanism of checkstyle.
Please help me with that.

Regards,

Archit


MASTEK
"Making a valuable difference"
Mastek in NASSCOM's 'India Top 20' Software Service Exporters List.
In the US, we're called MAJESCO

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Opinions expressed in this e-mail are those of the individual and not that of Mastek Limited, unless specifically indicated to that effect. Mastek Limited does not accept any responsibility or liability for it. This e-mail and attachments (if any) transmitted with it are confidential and/or privileged and solely for the use of the intended person or entity to which it is addressed. Any review, re-transmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. This e-mail and its attachments have been scanned for the presence of computer viruses. It is the responsibility of the recipient to run the virus check on e-mails and attachments before opening them. If you have received this e-mail in error, kindly delete this e-mail from all computers.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. <a href="http://ads.osdn.com/?ad_idt77&alloc_id492&op=click">http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

Re: Customized log message

Oleg Sukhodolsky
Hi Archit,

in fact, Check has 11 log() methods some of them it inherits from AbstractViolationReporter.
I'd suggest to call on of these methods your own log method.
Something like:
protected void logName(DetailAST aAST, String aKey, Object[] aArgs)
{
    final DetailAST nameAST =aAST.findFirstToken(TokenTypes.IDENT);
    final DetailAST methodEndAST = aAST.findFirstToken(TokenTypes.RPAREN);
    final String name = nameAST.getText();
    final Object[] fullArgs = new Object[aArgs.length + 1];
    System.arraycopy(aArgs, 0, fullArgs, 1, aArgs.length);
    fullArgs[0] = name;
    super.log(aAST.getLineNo(), methodEndAST.getLineNo(), aKey, fullArgs);
}

but be aware that second parameter in this call (and in LocalizedMessage ctor)
is column number, not line number and this may cause some interpretation problem
(e.g. in suppression filters)  Perhaps it's better to just write second line in
the message itself.

Anyway you can do this, if you are sure that this is what you want :)

Hope my suggestion helps you.

With best regards, Oleg.

Archit P wrote:

> Hi all,
>
> I am writing my custom checks for J2EE . While performing the check I
> want to log the error message such that it gives me the start and end of
> message definition. Till here it's fine. But while logging the message I
> cannot customize the log message because the check.java file has just
> two log methods specified which do not suit my needs.
>
> I tried to overload the logName method in MyCustomCheck.java class.
>
> What I did was this
>
> Public class MyCustomCheck extends AbstractInterfaceCheck
>      protected void logName(DetailAST aAST, String aKey, Object[] aArgs)
>      {
>          final DetailAST nameAST =
> aAST.findFirstToken(TokenTypes.IDENT);
>          final DetailAST methodEndAST =
> aAST.findFirstToken(TokenTypes.RPAREN);
>          final String name = nameAST.getText();
>          final Object[] fullArgs = new Object[aArgs.length + 1];
>          System.arraycopy(aArgs, 0, fullArgs, 1, aArgs.length);
>          fullArgs[0] = name;
>          LocalizedMessages localMessages = new LocalizedMessages();
>          localMessages.add(new LocalizedMessage(
>                  nameAST.getLineNo(),
>                  methodEndAST.getLineNo(),
>                  getMessageBundle(),
>                  aKey,
>                  aArgs,
>                  getSeverityLevel(),
>                  this.getClass()));
>          setMessages(localMessages);
>      }
> }
>
> This method gets executed but it does not log error. I think I didn't
> get the logging mechanism of checkstyle.
> Please help me with that.
>
> Regards,
>
> Archit
>
>
> MASTEK
> "Making a valuable difference"
> Mastek in NASSCOM's 'India Top 20' Software Service Exporters List.
> In the US, we're called MAJESCO
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Opinions expressed in this e-mail are those of the individual and not that of Mastek Limited, unless specifically indicated to that effect. Mastek Limited does not accept any responsibility or liability for it. This e-mail and attachments (if any) transmitted with it are confidential and/or privileged and solely for the use of the intended person or entity to which it is addressed. Any review, re-transmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. This e-mail and its attachments have been scanned for the presence of computer viruses. It is the responsibility of the recipient to run the virus check on e-mails and attachments before opening them. If you have received this e-mail in error, kindly delete this e-mail from all computers.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> from IBM. Find simple to follow Roadmaps, straightforward articles,
> informative Webcasts and more! Get everything you need to get up to
> speed, fast. <a href="http://ads.osdn.com/?ad_idt77&alloc_id492&op=click">http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
> _______________________________________________
> Checkstyle-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/checkstyle-user
>



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user